Python My Notes
Python My Notes
")
# print("hello world")
# print("hello coders")
# print("hello guys")
# use small letters for print as python is case sensitive
# name="amaan zain"
# print(name)
# dont use variables in double quotes as it will be used as String
# String,integer,float,boolean
# a=3
# b=3.0
# c=True
# d="amaan"
# excersice
# first_name="tony"
# second_name="stark"
# age=51
# is_genius=True
# user input
# name=input("what is your name: ")
# print(name)
# print("your name is "+name)
# type conversion
# python accepts every input as string type so it is neccessory for
converting into other datatype
# old_age=input("enter your old age: ")
# new_age=old_age+3
# here problem arises as old_age is of string and instead of addittion
concatination takes place
# print(new_age)
# for solving this problem we need to use int( ) or float(
)or bool( )or str( )
old_age=input("enter your old age: ")
new_age=int(old_age)+3
print(new_age)
# print(name.replace("zain","12"))
print(5+2)
print(5-2)
print(5*2)
print(5/2)
print(5//2) # to prnt only the decimal part of decimal
print(5%2)
print(5**2) # to find power
print(5>2)
print(5<2)
print(5<=2)
print(5>=2)
print(5==2)
print(5!=2)
# simple if
# age=19
# if(age>18):
# print("you are an adult")
# print("you can vote")
# if else
# age=10
# if(age>18):
# print("you are an adult")
# print("you can vote")
# print("")
# else:
# print("you are minor")
# print("you cant vote")
# elif
age=10
if(age>18):
print("you are an adult")
print("you can vote")
elif(age>3 and age<=18):
print("you are a child")
else:
print("you are minor")
print("you cant vote")
print("thank you")
# while loop
i=1
while(i<=5):
print("amaan zain n")
i=i+1
# for loop
for item in range(5):
print("amaan zain n")
marks=[10,20,30,40,50]
extra=["amaan",20,400] # diffrent type can be used inside list
extra[0]=22 # replaces the value of list in a particular index
print(extra)
print(marks)
print(extra[2])
print(extra[-2]) # mminus is used to index from the end(-1 is the last
element)
print(marks[1:3]) # to find inbetween indux here 3 rd index is not
included
# functions in list
marks.append(20.5) # insert at the end
print(marks)
student=["ab","abab","ababab","amaan","abababab"]
for s in student:
if(s=="amaan"):
break # stops the loop
print(s)
print("**************************************************")
for s in student:
if(s=="abab"):
continue # stops the executing process and continue the next
process
print(s)
# functions
print(name.count("vvvv")) # count the number of times an instance occur
print(name.index("vvvv")) # return the first index of the instance
name={"amaan","aaaa","vvvv","bbbb","bbbf","vvvv","vvvv","vvvv","vvvv"} #
unique values are only accepted
print(name) # repeatation not printed as sets are unique
# print(name[0]) # returns error as indexing does not contain in
sets(unordered(no index))
for i in name:
print(i)# for printing we can use for loop
# in build
# int()
# str()
# module
# import math
# print(dir(math)) # to list the function in the imported module
#user defined
# syntax:
# def function_name(parameters):
# //do something
def sum(num1,num2):
print(num1+num2)