PYTHON BASICS
B Y: A S I F S U R T I
PRINTING IN PYTHON
• print() is used to print information to the screen
• for example
• print(“Hello!”) prints Hello on the screen.
• Try this code!
• print(“Hello!”)
• print(“Hi!!!”) prints Hi on the screen
• Please do a few more print statements
• e.g. print (1000)
• print(“Good day!”)
• print(“My name is Dawood”)
DIFFERENT KINDS OF DATA
• Data is all around us
• Someone’s home address is data:
• 402N Indu heights,
• Malleswaram
• Pin code 400009
• Salary of sports persons can be another data:
• Raman earns 10,000 per month
• Aly earns 5000 a month
DIFFERENT KINDS OF DATA
• Students information:
• Name: Ibrahim
• Roll No: 20
• Class 6
• The name is made up of characters A-Z and is called a string
• Name is of datatype <string> . In python strings are
represented with quotes (“ “)
• “Ibrahim”, “Jaenil”, “Tanay” etc.
PYTHON STRING VARIABLES
• Variable is used to store a data item
• Open a console:
• = operator is used to assign values to variables
• name = “asif”,
• last_name = “surti”
• brand = “apple”
• and so on
• Exercise: Please define 5 variables and assign values to it and print them.
QUOTES
• Quotes can be different types
• “Dhaval”
• ‘Tanay’
PYTHON STRING VARIABLES
• a = “Hello “
• b = “Tuesday”
• car_name = “Maruti” etc.
• There are rules for variables name
• Start with a letter [A-Z, _ ]
• Don’t start with a number
• No spaces allowed
• Alphanumeric characters and _ allowed
• Instead of a space use an underscore _
FUN WITH STRINGS
• Type this code:
• “Hello World!”.upper()
• What does it print?
PYTHON – NUMERIC VARIABLES
• Numeric variables can hold numeric data
• example
• a = 20.0001
• roll_no = 36
• weight = 23.3 etc.
• Variables can also be used to hold numbers
PYTHON – NUMERIC VARIABLES
• num1 = 20
• print(num1)
• days_in_week = 7
FLOATING DATA TYPE
• Numbers with decimal point are called floating point numbers
• temperature = 22.2 # deg C
• speed = 16.5 # km/hr
• height = 23.2 # cms
• # a= 100
ARITHMETIC WITH NUMBERS
• Operators
• + # Addition
• - # Subtraction
• * # Multiplication
• / # Division
• % # Modulus operator returns the remainder
• ** # Exponentiation
• Examples
• # Number of hours in 1 month
• print(24*30)
• # Average of 10 numbers
• print( (5+20+12+23+45+12+43+23+12+34)/10 )
ARITHMETIC WITH NUMBERS
• Brackets can be used just as we use in mathematical equations
• e.g.
• a = (10+2)*2 -> a is 24
• print(a)
RULES ABOUT VARIABLE NAMES
• variables should always start with a letter e.g. a, B etc., it
cannot start with a number!
• Please check if the following are good variable names or not…
• 1day
• day1
• height
FORMATTED PRINTING
• How do you print the value of a variable?
• Lets say a is 10. How do you print a is 10?
• a = 10
• print(f”a is {a}”)
• today = “Tuesday”
• print(f”Weekday is {today}”)
COMMENTS IN PYTHON
• Comments are meant for the user to understand code.
• They are like description of how the program works
• They are ignored by the computer and do not run
• hello gives an error
• # hello does not give an error
• # assign variable a to 20
• a = 20
• # This statement prints my name
• print (“Asif”)
COMPARISON OPERATORS
• Comparison operators help us to make comparisons
• e.g. >, <, >=, <=
• e.g. 10>20
• 15 < 30
• Is 20 equal to 20? is written as
• 20 == 20
• What do you think is the result of 10>20
• Try it in Python!
LOGICAL OPERATORS
• A computer can make decisions!
• Every child aged more than five goes to secondar school else
they go to primary school
• age = 7
• if age>5:
• school = “secondary”
• else:
• school = “primary”
• print(f“School is {school}”)
TYPE OF DATATYPE
• name = “Lala” is a name, its type is string
• earning = 123.345 is todays earnings.
• Python keeps tracks the type of data type
• type(name) shows string
• type(earning) shows a float
ADDING STRINGS
• It is possible to add strings
• first_name = “asif”
• last_name = “surti”
• full_name = first_name+last_name
• print(full_name)
OTHER DATA TYPES
• “Snehal” is a string data type
• 123.465 is a numeric data type
• What if we want to make a variable that stores multiple data
items?
• We can use a list to store multiple data items
• student_detail = [“Azra”, 6, 36, “3B”]
LISTS
• The order of a list is important
• e.g. num1 = [1,2,3]
• num2 = [1,3,2]
• num1==num2
• See what the computer prints!
LISTS
• Accessing List elements
• num2 = [100, 200 ,300]
• what is the second element?
• element = num[2]
• But remember elements starts with 0 rather than 1 !
LISTS
• Accessing multiple elements
• num_list = [100, 200 ,300, 400, 500, 600, 700]
• Let us pick elements 2nd , 3rd and 4th
• nums = num_list[2:5]
• Lets say we need the first 3 elements
• We can access lists like so:
• num_list[:3]
• It prints the first 3 elements:
• [100, 200 , 300]
LISTS
• Lists are MUTABLE
• This means that elements can be added or removed from the
Lists
• my_desserts = [“ice_cream”, “chips”, “candy”]
• more_desserts = [‘gulab_jamun’, ‘laddoo’]
• all_desserts = my_desserts + more_desserts
MEMBERSHIP OPERATORS
ASSIGNMENT
T U RT L E M O D U L E
ASSIGNMENT - PRINTING VARIABLES
• assign a variable that hold the number of days Ahmad works.
• assign another variable that holds his salary as the amount he
charges for every day he works
• print the amount he has earned.
• Hint: If Ahmed worked for 6 days and charges Rs 20 per day,
your code should print out Ahmad earns Rs 120.
• Assign a variable called time_of day
• Print a message Good {time_of_day}
• E.g. if time_of_day =“afternoon”, your code should print Good
afternoon!
ASSIGNMENT - PRINTING VARIABLES
• Write code to print the value of the equation
• x = (12+ 2)*32 + 2*5 + 10/2
• Write code to print your name and age
• age = 7
• name = “Jaenil”
• the Output should be
• My name is Jaenil and I am 7 years old