Python
INTERNAL
Easy 1st program
• >> print (“Hello world!”) Hello world!
• >> a=3
b=4
>> print(a*b) 12
>> print(a-b) -1
>>print (“* * ” *2) ****
Variables start with lower case: variables can be integer/float/string/Booleans/lists/object
Keywords start with uppercase
e.g:
INTERNAL
Basic functions
• print() publishes text
• input() takes input from user
• int()/ float()/ bool() Converts a string to int/float/Boolean format
INTERNAL
INTERNAL
Printing multiple lines
Printing with index:
Name= “Purva Kulkarni”
Name1= Name[:] #Created copy of Name variable
Instruction output
print(name[0]) start of index P
print(name[1]) U
print(name[-1]) end of index I
print(name[0:3]) excludes last letter Pur
print(name[0:]) if end is not given Purva Kulkarni
print(name[-4:-1]) Arn
print(name[1:-1]) starts from index1 and Urva Kulkarn
excludes last index
print(name[Name1]) Purva Kulkarni
INTERNAL
Formatted string
#Print "Purva [Kulkarni] is a tester"
first= "Purva"
Normal code
last= "Kulkarni"
Purva [Kulkarni] is a tester
message= first + " [" + last + "] is a
tester"
print(message)
#Print "Purva [Kulkarni] is a tester"
first= "Purva"
last= "Kulkarni"
# message= first + " [" + last + "] is a Purva [Kulkarni] is a tester Formatted string
tester"
msg= f"{first} [{last}] is a tester"
print(msg)
INTERNAL
String Methods
• Difference in general purpose functions and string methods
first= "PURVA" C:\Users\Kulkaprv\AppData\Local\Programs\
last= "kulkarni" Python\Python313\[Link] C:\Users\
print([Link]()) Kulkaprv\Python\[Link]
print([Link]()) PURVA
print([Link]()) KULKARNI
print([Link]('PURVA' , 'Purva purva
Prasad')) Purva Prasad
print([Link]('P' , 'D')) DURVA
print([Link]('V')) 3
print([Link]('ni')) 6
print('kul' in last) True
print('Pu' in first) False
Process finished with exit code 0
• Method does not affect the original variable value
• Above shown functions are specific to strings that’s
Why they are called methods.
• Find method gives index number as output but “in” operator gives Boolean result and its
case sensitive.
INTERNAL
Arithmetic operations Augmented operator
Print (10+3)=13………Normal operation X= 10
Print(10/3)= 3.33335…..Normal division X= X+3…….. can be written as X +=3…….This
Print(10//3)= 3…….Division with integer output adds the previous x value to 3 and stores in x.
Print(10%3)= 1……..Reminder
Print(4**2)=16………e
Math functions
Order precedence (Order of operations) X=-2.9
X= (4+2)*2**3 Print(round(X))-3
Print(x) Print(abs(X)) 2.9…………….these were in built
48…………………..Order is: () then functions
exponentiation, multiplication/div, For additional math functions, use command:
add/subtract import math
X=2.9
Print([Link](X))3
Print([Link](X))2
……………….Check :
math — Mathematical functions — Python 3.13.1 d
ocumentation
INTERNAL
Modules list
>>help(‘modules’)
INTERNAL
IF statement
INTERNAL
INTERNAL
ogical and comparison Operators
INTERNAL
Weight conversion
INTERNAL
While Loop
i=1
while 1
i<=5: 2
print(i) 3
i +=1 4
print("done 5
")
done
INTERNAL
Building a car game
Requirement:
• At start it should give >
• After > if we type help in any case, it should give:
Start- to start the car
Stop- to stop the car
Quit- to exit
• If typed anything else, it will give “I don’t understand that..”
• If typed start, it gives “Car started”
• If typed stop, it gives “Car stopped”
• If typed quit, the program terminates
• Check next slide for solution
INTERNAL
INTERNAL