Python Programming
# (For comment) This is my first program- will not display
Print (“Hello World”) or Print (‘Hello World’)
Variables - To store the value of memory locations of different data types (string, integer, float ,
boolean). Each variable have a unique name.
String Variable -
A series of characters . They can include no.s which we treat as characters.
Eg: name = “John” or ‘John’ # value of the variable is inside single or double quotes means it is a
string type variable
print (name) # print the value inside the variable = John.
You could use your variable along with some text by using f-string
Eg: print (f “Hello {name}”) # f or F means format
#This will print Hello John
Sample pgm -
Name = “John”
Mail-id = “[email protected]
Address = “EKM”
Print (f “Hello {Name}“)
Print (f “Your email is {Mail-id}”)
Print (f”Your Address is {EKM}”)
Integer Variable : A whole number
Eg: age= 16
quantity = 3
no_of_students=20
Output:
Print (f “You are {age} years old”)
Float - To hold the decimal nos
Eg: quantity = 10.5
Boolean - Hold either True or False
Eg: is_student = True # work if the person is under 19 otherwise False
Print (f”Are you a student?:{is_student}”)
Used with If stmt
Eg: is_student=True
for_sale=False
is_online=True
If is_student;
Print(“Your are a student”)
else
Print(“Your are not a student”)
If for_sale:
Print(“The item is for sale”)
else
Print(“The item is NOT available”)
If is_online:
Print(“You are online”)
else
Print(“You are offline”)
# Typecasting : The process of converting a variable from one datatype to
another.
# str(), int(), float(), bool()
name = “John”
age=16
quantity = 10.5
Is_student = True
quantity = int (quantity) # prints 10
Do it for others too
Can do operations with only same datatype.
# input () = A function that prompts the user to enter data. It returns the data
entered as a String.
name = input (“Enter the name:”)
age = input (“How old are you”)
Print (f “Hello {name}”)
Print(f “I am {age} years old”)
List
set
Tuple
2D List
or
Lists on tuples [()] or Tuple of tuples (())or Tuple made up of
sets or can be set made up of sets. Use as and how needed
2 D Collections - 2D tuples (eg: Quiz game)
Sending parameters as arguments in a function -
Name,age are parameters which are variables used in a function.
Object Oriented Programming
To better organize this we can place it into another Python File car.py
Call this file car.py from original pgm (From “name of module” import “name of the class”
Methods
Same applied for car2 and car3. Also call Car1. stop for all the other car2 and car3
also
Random
Iterables
An object or collection that can return its elements one at a time. It also allowing it to be iterated in a loop.