<jupyter_start><jupyter_text>basic practice<jupyter_code>print('hello world')
exit()
#to restart the kernel
x='nirmal'
print('hello '+ x)
x = "Python is "
y = "awesome"
z =  x + y
print(z)
x=1
y=2
print(x+y)
x=1
y=' : one'
print(str(x)+y)
type(1+2j)
type(1)
type('nirmal')
type(1.1)
type(1.0000016555888888888888888888888888888888888888888888888888888888888888888888888846546856186498165364568848513126841670123)
type(-1.12)
type(-5j)
a='hello world'
print(len(a))
print(a[0])
print(a[-1])
a[0:]
a[0:4]
a[::-1]
a[::-1]<jupyter_output><empty_output><jupyter_text>reverse each word of string<jupyter_code>a='hello python world'
a=a.split(' ')
b=[]
for i in a:
    b.append(i[::-1])
print(*b)
a='hello how are you nirmal'
print(*([i[::-1] for i in a.split(' ')]))
a='hello how are you nirmal'
print(' '.join([i[::-1] for i in a.split(' ')]))
a='i am the sentence for sorting apple ball lion'
a=a.split(' ')
a.sort()
print(' '.join(a))
a='geeks for geeks'
a=a.split(' ')
a.sort()
print(*a)
a='   helo        nirmal'
a.strip()
# The strip() method removes any whitespace from the beginning or the end
a='helloh babu'
# a.lower()
# a.upper()
a.replace('h','J')
import pandas as pd
df = pd.read_excel('iris.xlsx').head()
df
type(df)
df.shape
lst=['hello','nirmal','python','cdac','vita']
for i in lst:
    print(i)
# using list comprehension
print([i for i in lst])
if 'nirmal' in lst:
    print('i am present in list')
len(lst)
lst.append('damna')
lst
lst.pop()
lst
lst.append(['damna','hajur'])
lst
lst.pop()
lst
lst.extend(['damna','hajur'])
lst
lst.pop()
lst
lst.insert(0,'zero')
lst
lst.remove('zero')
lst
lst.insert(0,'apple')
lst
lst.pop(0)
lst
lst.insert(-0,'lastitem')
lst
lst.pop(0)
lst
lst.insert(-1,'delete')
#note that list inserting with negative indexing is misleading
lst.pop(-2)
lst
lst.insert(-1,'deleter')
lst
lst.remove('deleter')
lst
lst.pop(0)
lst
lst.insert(len(lst),'finallyhere')
# to insert at the end of the list
lst
del lst[-1]
#it removes the list item from last index i.e -1
lst
del lst[0]
lst
del lst
lst
# since we deleted the list so you will get the error while trying to access the list, error was shown intentionally!
lst = ["apple", "banana", "cherry"]
lst.clear()
# The clear() method empties the list
lst
# note the difference between clear and delete
jpt = list(('a','b','c','d'))
jpt
type(jpt)
a=jpt.copy()
a
a.pop()
jpt
#note that  we made shallow  copy of the list jpt so changing list a doesnt reflect in list jpt
a
a.count('a')
jpt.index('b')
jpt.reverse()
jpt
for i in reversed(jpt):
    print(i)
jpt
print([' '.join(i for i in reversed(jpt))])
jpt.sort()
jpt
thistuple = ("apple", "banana", "cherry")
# thistuple[1] = "blackcurrant"
# The values will remain the same:
print(thistuple)
type(thistuple)
thistuple.count('cherry')
thistuple.index('apple')
setme={}
type(setme)
# note that this is defining empty dictionary rather than set
setme
setme=set()
type(setme)
setme
thisset = {"apple", "banana", "cherry"}
print(thisset)
thisset
# Note: Sets are unordered, so the items will appear in a random order.
if 'apple' in thisset:
    print('apple is present in the set')
for i in thisset:
    print(i)
print('cherry' in thisset)
thisset.add('mango')
thisset
thisset.update(['pineapple','pears','tears'])
# to add multiple items in the set
thisset
len(thisset)
thisset.remove('tears')
thisset
thisset.discard('pears')
thisset
thisset.remove('faltu')
# Note: If the item to remove does not exist, remove() will raise an error.
thisset.discard('faltu')
# Note: If the item to remove does not exist, discard() will NOT raise an error.
thisset
thisset.pop()
thisset
thisset.pop()
# pop removes randomly in case of set
thisset
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.difference(y) 
print(z)
dic={1:'one',2:'two',3:'three'}
type(dic)
dic[2]
for k,v in dic.items():
    print(k,v)
for val in dic.values():
    print(val)
for k in dic.keys():
    print(k)
dic[4]='four'
dic
print(dic.get('one'))
x=dic.get(1)
x
dic[1]='naya one'
dic
for i in dic:
    print(i)
# When looping through a dictionary, the return value are the keys of the dictionary
for i in dic:
    print(dic[i])
dic
if 2 in dic:
    print('i am inside dictionary')
len(dic)
dic.pop(1)
dic
dic.popitem()
dic
dic.popitem()
dic
del dic[2]
dic
print('true') if 1==1 else print('false')
print('true') if 12==1 else print('false') if 3==4 else print('bad idea')
i = 1
while i < 6:
  print(i)
  if i == 3:
    break
  i += 1
i = 0
while i < 6:
  i += 1 
  if i == 3:
    continue
  print(i)
for i in 'testing':
    print(i)
print(' '.join([i for i in 'testing']))
x=range(5)
for i in x:
    print(i)
x=[i for i in range(10)]
x
for x in range(6):
  print(x)
else:
  print("Finally finished!")
for i in range(2):
    for j in range(2):
        print(i,j)
example='snow world'
# example[3]='s'
print(example)
def my_function(country = "India"):
  print("I am from " + country)
my_function("Nepal")
my_function()
class hello:
    x=5
a=hello()
a.x
class person:
    def __init__(self,fname,lname):
        self.fname=fname
        self.lname=lname
obj=person('nirmal','silwal')
obj.lname

# Note: The __init__() function is called automatically every time the class is being used to create a new object
class person:
    def __init__(self,fname,lname):
        self.e=fname
#         self.fname=fname
        self.lname=lname
    def myfunc(a):
        print('hello '+ a.e +' ' + a.lname)
#         print('hello',self.fname,self.lname)

obj=person('nirmal','silwal')
obj.myfunc()
import math
a=math.factorial(100)
print(a)
len(str(a))
fact=lambda x: 1 if x==0 else x*fact(x-1)
print(fact(5))
# factorial using recursion
def fact(n):
    if n==0: return 1
    else: return(n*fact(n-1))
fact(5)
fact=lambda x: 1 if x==0 else x*fact(x-1)
print(fact(1))
# find the nth highest item from the array
lst=[3,1,5,7,8,50]
lst.sort()
lst.reverse()
print(lst[2])
a=('apple','ball','cat')
b=iter(a)
b
next(b)
next(b)
next(b)
st='banana'
st=iter(st)
st
next(st)
next(st)
for i in st:
    print(i)
# The for loop actually creates an iterator object and executes the next() method for each loop.
import platform
x = platform.system()
print(x)
# import platform
# x = dir(platform)
# for i in x:
#     print(i)
import datetime as dt
print(dt.datetime.now())
platform.architecture()
dates=dt.datetime.now()
dates.year
dates.day
dates.month
birthday='1997-03-08'
birthday=birthday.split('-')
birthday
import datetime
print(datetime.datetime.now().year - int(birthday[0]), datetime.datetime.now().month - int(birthday[1]),
      datetime.datetime.now().day - int(birthday[2]) )
datetime.datetime.now().day - int(birthday[2])
import datetime
birthday=datetime.datetime(1995,3,8)
((datetime.datetime.now()-birthday).days)/365
import json
x = {
  "name": "John",
  "age": 30,
  "city": "New York"
}

# convert into JSON:
y = json.dumps(x)
# the result is a JSON string:
print(y)
type(y)
import re
str = "hello world"
#Search for a sequence that starts with "he", followed by two (any) characters, and an "o":
x = re.findall("he..o", str)
print(x)
import mysql.connector as con
import pymongo<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16687737041719342, 'fraction_numerical': 0.03315072341621014, 'mean_word_length': 4.646312450436162, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '524147', 'license': 'MIT', 'n_tokens_mistral': 2928, 'n_tokens_neox': 2793, 'n_words': 827, 'path': 'General/practice.ipynb', 'repo_name': 'NirmalSilwal/Python-', 'size': '7119'}