top of page

Python Basic Practice Exercise For Beginners | How to Learn Python Programming | Realcode4you

Type Casting in Python

x=34
y='November'
type(x)

output:

int


type(y)

output:

str


x='34'
type(x)

output:

str




Numeric Data and Operations

x=2
y=19
print(x+y)

output:

21


x-y
print(x*y)

output:

38


x/y

output:

0.10526315789473684


x^y , x**y
# ^ bitwise or
# do not use ^ for powers 
# always use **

output:

(17, 524288)


z=(x/y)**(x*y+3)
z

output:

8.190910549494282e-41


x+2,y/10,x-y,x**y

output:

8.190910549494282e-41


a=x+2
b=y/10
c=x-y
d=x**y
a,b,c,d

output:

(4, 1.9, -17, 524288)




Boolean Data and Operations

x=True
y=False
type(x)

output:

bool


x and y, x & y

output:

(False, False)


x or y , x | y 

output:

(True, True)


not x 

output:

False


not y

output:

True


a=34
a>34 

output:

False


(a>34) and (a=='lalit')

output:

False


  • checking equality : "=="

  • in-equality : "!="

  • greater than : ">"

  • less than : "<"

  • greater than or equal to : ">="

  • less than or equal to : "<="



String Data and Operations

x='Python'
y='Data'
type(x),type(y)

output:

(str, str)


len(x),len(y)

output:

(6, 4)


x+y

output:

'PythonData'


x*3

output:

'PythonPythonPython'


z=x+' and '+y
z

output:

'Python and Data'


type(z)

output:

str


z.lower()

output:

'python and data'


z

output:

'Python and Data'


z.upper()

output:

'PYTHON AND DATA'


z.capitalize()

output:

'Python and data'


z.rjust(20)

output:

' Python and Data'


z.ljust(20)

output:

'Python and Data '




Indexing

x='PythonIndex'
x

output:

'Python and data'


x[4]

output:

'o'


x[-5]

output:

'I'


x[2:7]

output:

'thonI'




Lists

x=[20,92,43,83,"lalit","a","c",45]
x[3:7]

output:

[83, 'lalit', 'a', 'c']


x[2:]

output:

[43, 83, 'lalit', 'a', 'c', 45]


x[:4]

output:

[20, 92, 43, 83]



x

output:

[20, 92, 43, 83, 'lalit', 'a', 'c', 45]


x[3]

output:

83






Flow Control In Python

x=12
if x>10:
   print('my value is larger than 10')
else:
    difference=10-x
    print('x is smaller than 10 by '+str(difference))

OUTPUT:

my value is larger than 10


x=[5,40,12,-10,0,32,4,3,6,72]
for value in x:
    if value>10:
        print('my value is larger than 10')
    else:
        difference=10-value
        print('x is smaller than 10 by '+str(difference))
        print('hello again')
    print('hello')
    
print('hello there')

OUTPUT:

x is smaller than 10 by 5
hello again
hello
my value is larger than 10
hello
my value is larger than 10
hello
x is smaller than 10 by 20
hello again
hello
x is smaller than 10 by 10
hello again
hello
my value is larger than 10
hello
x is smaller than 10 by 6
hello again
hello
x is smaller than 10 by 7
hello again
hello
x is smaller than 10 by 4
hello again
hello
my value is larger than 10
hello
hello there

cities=['Mumbai','London','Bangalore','Pune','Hyderabad']
for city in cities:
    num_chars=len(city)
    print('number of character in the name of city '+city+ ':'+ str(num_chars))

OUTPUT:

number of character in the name of city Mumbai:6
number of character in the name of city London:6
number of character in the name of city Bangalore:9
number of character in the name of city Pune:4
number of character in the name of city Hyderabad:9

x=10
if x>6:
    print('greater than 6')
    if x>7:
        print('greater than 7')
        if x>8:
            print('greater than 8')

OUTPUT:

greater than 6
greater than 7
greater than 8

# break , continue , pass
x=[[1,2],['a','b'],[34,67]]
for i,j in x:
    print(i,j)

OUTPUT:

1 2
a b
34 67

ctr=0
for city in cities:
    print(ctr,':',len(city))
    ctr+=1

OUTPUT:

0 : 6
1 : 6
2 : 9
3 : 4
4 : 9




Dictionaries

my_dict={'name':'lalit','city':'hyderabad','locality':'gachibowli','num_vehicles':2,3:78,4:[3,4,5,6]}
type(my_dict)

output:

dict


my_dict.keys()

output:

dict_keys(['name', 'city', 'locality', 'num_vehicles', 3, 4])


my_dict.values()

output:

dict_values(['lalit', 'hyderabad', 'gachibowli', 2, 78, [3, 4, 5, 6]])


my_dict['locality']

output:

'gachibowli'


my_dict[4]

output:

[3, 4, 5, 6]



my_dict['last_name']='sachan'
my_dict

output:

{'name': 'lalit',
 'city': 'hyderabad',
 'locality': 'gachibowli',
 'num_vehicles': 2,
 3: 78,
 4: [3, 4, 5, 6],
 'last_name': 'sachan'}

del my_dict['num_vehicles']
my_dict

output:

{'name': 'lalit', 'city': 'hyderabad', 'locality': 'gachibowli', 3: 78, 4: [3, 4, 5, 6], 'last_name': 'sachan'}


for itr in my_dict:
	print(itr)

output:

name city locality 3 4 last_name


for itr in my_dict:
	print(itr,my_dict[itr])

output:

name lalit city hyderabad locality gachibowli 3 78 4 [3, 4, 5, 6] last_name sachan


my_dict.items()

output:

dict_items([('name', 'lalit'), ('city', 'hyderabad'), ('locality', 'gachibowli'), (3, 78), (4, [3, 4, 5, 6]), ('last_name', 'sachan')])

for key,value in my_dict.items():
	print('key :', key , ' and value :',value)

output:

key : name  and value : lalit
key : city  and value : hyderabad
key : locality  and value : gachibowli
key : 3  and value : 78
key : 4  and value : [3, 4, 5, 6]
key : last_name  and value : sachan

Sets

names={'lalit','spanadan','deepta','manoj'}
type(names)

output:

set


'lalit' in names

output:

True


'harsh' not in names

output:

True


names.add('harsh')
names

output:

{'deepta', 'harsh', 'lalit', 'manoj', 'spanadan'}


names.add('harsh')

output:

{'deepta', 'harsh', 'lalit', 'manoj', 'spanadan'}


names.remove('lalit')
names

output:

{'deepta', 'harsh', 'manoj', 'spanadan'}


for name in names:
	print(name)

output:

manoj spanadan harsh deepta


a={1,2,3,4,5,6}
b={4,5,6,7,8,9}
c=a.union(b)
c

output:

{1, 2, 3, 4, 5, 6, 7, 8, 9}


a.intersection(b)

output:

{4, 5, 6}


a.difference(b)

output:

{1, 2, 3}
b.difference(a)

output:

{7, 8, 9}


a.symmetric_difference(b)

output:

{1, 2, 3, 7, 8, 9}




Functions

x=12
fib_series=[1,1]
ctr=2
while ctr<x:
    fib_series.append(fib_series[-1]+fib_series[-2])
    ctr+=1

fib_series

output:

[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]


def fib_series_creator(num_elements):
    fib_series=[1,1]
    ctr=2
    while ctr<num_elements:
        fib_series.append(fib_series[-1]+fib_series[-2])
        ctr+=1
        
    return(fib_series)

fib_series_creator(3)

output:

[1, 1, 2]



def mysum(x,y,z):
	return(2*x+3*y+4*z)
mysum(1,2,3)

output:

20


mysum(2,4)

output:



def mysum(x=1,y=10,z=100):
	return(2*x+3*y+4*z)
mysum(1,2,3)

output:

20



Args and kwargs

# * is an unpacking operator

# we can make use of it to create a function which can take variable number of argument

# many of these function can also be written where they are built for taking a list as an argument


def mymax_1(x):

m=-float('inf')

for elem in x:

if elem>m:

m=elem

return m


mymax_1([3,4,5,-10,55,2,1])


Output:

55

def mymax_2(*x):

m=-float('inf')

for elem in x:

if elem>m:

m=elem

return m


mymax_2(3,4,5,-10,55,2,1)


output:

55

mymax_2(3,4,5,-10,55,2,1,99,-100,54,67)

Output:

99


mymax_2(*[3,4,5,-10,55,2,1,99,-100,54,67])

output:

99


# a more useful purpose of unpacking operator is that 
# you can use unpacking operator to pass your argument as a list to a function which  takes
# fixed number of arguments . This you dont have to manually extract them into individual elements and 
# then pass to the function . here is an example 
def combine_name(first,last):
    name=first+' '+last
    return(name)
names=[['lalit','sachan'],['rajkumar','rao'],['hardik','pandya']]

for x in names :

print(combine_name(x[0],x[1]))


Output:

lalit sachan
rajkumar rao
hardik pandya

Understanding Global and Local Scoping

a=20

b=10


def exp1(a,b):
    a=7
    b=3 
    global c
    c=100
    print(a,b)
a,b
exp1(a,b)
c

class experiment():

def init(self,a,b):

self.A=a

self.B=b

def do_something(self,x):

return(self.A*x+self.B*x)




Class

c1_name='lalit'
c1_balance=100
c1_last_wd=10
c2_name='pravin'
c2_balance=200
c2_last_wd=50
class customer():  
    def init(self,name,balance,last_wd):
        self.name=name
        self.balance=balance
        self.last_wd=last_wd
    def withdraw(self,amount):
        if(amount>self.balance):
            print('insufficient balance')
        else:
            self.balance-=amount
            self.last_wd=amount
    def deposit(self,amount):
        self.balance += amount
        return(self.balance)

A Bad Usage of Class


Decorators in Python


Inheritance





Comments


bottom of page