0% found this document useful (0 votes)
7 views

Python codes

Uploaded by

adhil.offenso
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python codes

Uploaded by

adhil.offenso
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Python codes

Operators:

Arithmetic: =,-,*,/,**,//

10+5

8-3

2*2

4/2

2**3#power operator

1/2

1//2#integer division

17/3

17//3

7//3

Relational: <,<=,>,>=,==,!=#returns True or False

a=1

b=2

a<b

a>b

a==1

Logical: and or not# Used to combine to relational operator

a==1 and a<b#returns True if both conditions are True

a==1 or b==3#returns True if any of conditions is True

not a==1#Oposite of normal result

Assignment: =,+=,-=

a=1

a+=1#a=a+1

print(a)
Membership: in , not in# check membership

x=[1,2,3]

3 in x#returns true if value found in sequence

3 not in x

Identity: is, is not#"is" will return True if two variables point to the same object

a=1

a is 1

Data types

Mutable#Changable

list[]

set{}

dict{}

Immutable#Not changable

Numbers

str

tuple()

1.list:#Highly flexible, we can change any value

x=[1,2,3,2]

type(x)

x[0:2]

x[1]=5

print(x)

x.append(4)#add

x.count(2)
x.pop()#remove last value

del x[0]

x.append('hi')

x.extend('hi')

2.tuple#Immutable, once defined, we can't change the values

a=(1,2,3)

type(a)

a[0]#we can access values

a[1]=5#error

3.set#used for set operations

a={1,2,3}

b={3,4}

a.intersection(b)

a.union(b)

4.dictionary

student={1:'A',2:'B',3:'C',4:'D'}#syntax-- key:value

#rollno:name

#we can access student name using roll no of student

student[2]

student[2]='Z'

student
student={1:'A',2:'B',3:'C',4:'D',1:'y'}#Duplication of key is not possible

student.keys()

student.values()

student.items()

5.string

st="welcome"

len(st)

type(st)

st[:3]

st[1]='h'#error not possible

st2=' to the class'

st+st2

st.replace('e','x')

st.lower()

st.upper()

st.capitalize()

st="welcome to the class"

len(st)

st.split()#convert sentense into words

print(st)

st=" ".join(st)#joining words and create sentense

print(st)

6.Numbers

a=1

type(a)#int
b=1.1

type(b)#float

c=1+2j

type(c)#complex

#type conversion

a=1

float(a)

str(a)

#Decision statements

if

elif

else

#1

a=1

b=2

if a<b:

print("a is less than b")

#2

a=3

b=2

if a<b:

print("a is less than b")

#No result because condition is false

#3

a=3
b=2

if a<b:

print("a is less than b")

else:

print('b is less than a')

#4

a=3

b=3

if a<b:

print("a is less than b")

else:

print('b is less than a')

#5

a=3

b=3

if a<b:

print("a is less than b")

elif a==b:

print('b is less than a')

else:

print('same number')

#Looping statement

'''1.while#Entry controlled loop

syntax:

while condition:
statements

eg:print 1 to 10'''

a=1

while a<=10:

print(a)

a+=1

'''

2.Do..while#Exit controlled loop

#There is no inbuilt do..while loop in python

#we can execute the while loop in the format of do..while

#flow:

do

statements

while(condition)

syntax:

while True:

statements

condition

#eg: print 1 to 10'''

a=1

while True:

print(a)

a+=1
if a>10:

break

'''

3.For loop

syntax:

for var in sequence:

statements

eg: '''

for i in range(10):#0-9

print(i)

for i in range(1,11):#1-10

print(i)

#range(start,end,increment/decrement)

for i in range(1,10,2):#1,3,5,7,9

print(i)

for i in range(10,1,-2):#10,8,6,4,2

print(i)

#Function definition

'''syntax:

def name(arguments):

return statement'''

statements

def add(x,y):

return(x+y)
add(1,5)

1.Identify the odd and even numbers in the list

def identify(x):#X must be a list of values

odd=[]

even=[]

for i in x:

if i%2==0:

even.append(i)

else:

odd.append(i)

print('odd')

print([i for i in odd])

print('Even')

print([i for i in even])

identify([1,2,3,4,5,6])

#lambda- Anonymous function

import pandas as pd

x=pd.DataFrame({'x':[1,2,3,4,5],'y':[2,4,6,8,10]})

x.apply(lambda x:x.mean())

##########################################################################

2.Find square of given values

x=[1,2,3,4,5]
[i**2 for i in x]

or

for i in x:

print(i*i)

#User input

x = input("Enter 1st number")

y = input("Enter 2nd number")

z=x+y

print(z)

x = input("Enter 1st number")

a = int(x)

y = input("Enter 2nd number")

b = int(y)

z=a+b

print(z)

or

x = int(input("Enter 1st number"))

y = int(input("Enter 2nd number"))

z=x+y

print(z)

ch = input('enter a char')

print(ch)

ch = input('enter a char')
print(ch[0])

or

ch = input('enter a char')[0]

print(ch)

result = eval(input('enter a expr'))#2+6-1

print(result)

av = 5

x = int(input("How many Candies you want?"))

i=1

while i <= x:

if i>av:

print("Out of stock")

break

print("Candy")

i+=1

print("Bye")

for i in range(1,101):

if i%3==0:

continue

print(i)
print("Bye")

for i in range(1,101):

if i%3==0 or i%5==0:

continue

print(i)

print("Bye")

for i in range(1,101):

if i%3==0 and i%5==0:

continue

print(i)

print("Bye")

for i in range(1,101):

if(i%2!=0):

pass

else:
print(i)

print("Bye")

#Patterns

print("#")

print("# # # #")

print("# ")

print("# ")

print("# ")

print("# ")

print("# ",end="") #end-avoid newline

print("# ",end="")

print("# ",end="")

print("# ",end="")

for j in range(4):

print("# ",end="")

print()

for j in range(4):

print("# ",end="")

print()

for j in range(4):

print("# ", end="")


###################################

for i in range(4): ####

for j in range(4): ####

print("# ",end="") ####

####

print()

for i in range(4): #

for j in range(i+1): ##

print("# ",end="") ###

####

print()

for i in range(4): ####

for j in range(4 - i): ###

print("# ",end="") ##

print()

#############################

def fib(n):

a=0

b=1

if n == 1:

print(a)

else:

print(a)
print(b)

for i in range(2,n):

c=a+b

a=b

b=c

print(a+b)

fib(100)

#################

def fact(n):

f=1

for i in range(1,n+1):

f=f*i

return f

x=4

result = fact(x)

print(result)

########Factorial using Recursion########

def fact(n):

if(n==0):

return 1

return n * fact(n-1) #5*4!

result = fact(5)

print(result)
####for else#####

nums = [12,16,18,20,25]

for num in nums:

if num % 5 == 0:

print(num)

nums = [10,16,18,21,26]

for num in nums:

if num % 5 == 0:

print(num)

break

else:

print("not found")

########################

num = 10

for i in range(2,num):

if num % i == 0:

print("Not Prime")

break

else:

print("Prime")
a=5

b=2 #b=0

print(a/b)

print("Bye")

##################

a=5

b=2 #b=0

try:

print(a/b)

except Exception as e:

print("Hey,You cannot divide a number by zero",e)

print("Bye")

##################

a=5

b=2

try:

print("resource Open")

print(a/b)

k = int(input("Enter a number"))

print(k)

except ZeroDivisionError as e:

print("Hey, You cannot divide a Number by Zero" , e)

except ValueError as e:

print("Invalid Input")
except Exception as e:

print("Something went Wrong...")

finally:

print("resource Closed")

You might also like