# Chapter 0 - You can define comments within the program and python will ignore them while executing.
# to add a comment, simply put a # in start and remaining text in that line will be commented.
#Printing a string
print("Hello World")
#Printing Multiple Strings
# Notice output and how it adds a space between strings
print("Hello","World")
#Displaying numbers and calculations
#Notice no quotation marks are required.
print(1)
print(2)
print(1/2)
print(2**3) #equivalent to 2 raised to power 3
# Printing number with string
print("Hello, My Age is",13)
#If you have noticed, every print statement is printing in a separate line. to change that, we use end keyword and pass space or tab or whatever you wish to append at the end of print
print("hello",end="")
print("world",end="\n")
#If you have noticed, every print statement is printing in a separate line. to change that, we use end keyword and pass space or tab or whatever you wish to append at the end of print
print("a tab",end="\t")
print("away",end="")
# Let's create some variables
name = "Ahsan" #Here we store value Ahsan in a variable called "name". Remember to put your strings between the quotation marks
age = 23 # We can store an integer value in the same way
weight = 60.5 # Here we are storing a floating point number in similar fashion
# Printing these varaibles
print(name)
print(age)
print(weight)
# I can modify variable at any time
name = "Ali"
#Printing name after modifying
print(name)
# Similarly I can print all of these variables using single print command
print("My name is",name,", and my age is",age," and I weigh",weight,"kgs")
#another way to print is using format. Note that this works in python 3.5
#notice there is a small f representing special format just before the string.
print(f"My name is {name}, and my age is {age} and I weigh {weight} KGs")
# you can also take quick input from user using input()
name = input("Hello, Please enter your name : ") #input function is used to take input. String passed is presented to user as a prompt
print(f"Hello {name}!") #print whatever name is given by user with a greeting!
# Lets find type for name, age and weight
print(type(name))
print(type(age))
print(type(weight))
#now lets say if I change the data in one of the variables. the type will change accordingly.
#Lets change weight to be a category, rather than numeric weight
weight = "Overweight"
#Now lets find out the type of same variable
print(type(weight))
centigrade = 37 #Declaring a variable and assigning value to it.
fahrenhiet = (centigrade * 9/5) + 32 #Notice how we easily used the centigrade variable directly in formula.
print(f"The temperature {centigrade} degrees centigrade is equivalent to {fahrenhiet} degrees Fahrenheit ")
centigrade = 37 #Declaring a variable and assigning value to it.
print(f"The temperature {centigrade} degrees centigrade is equivalent to {(centigrade * 9/5) + 32} degrees Fahrenheit ")
usd = 135.6
pkr = usd * 160
print(f"The ${usd} is equivalent to Rs. {pkr}" )
bank_balance = 400
deposit = 100
bank_balance = bank_balance + deposit
print(f"The bank balance after depositing ${deposit} is ${bank_balance}")
bank_balance = 400
deposit = 100
bank_balance +=deposit
print(f"The bank balance after depositing ${deposit} is ${bank_balance}")
productCost = 100
taxRate = 0.05
productCost = productCost + (productCost*taxRate)
print(f"The product cost after adding tax of {taxRate}% is {productCost}")
number1 = "21"
number2 = 21
print(type(number1)) #We cannot use that directly in our calculations. we can cast it,however, to integer but more on that later.
print(type(number2)) #We can use this directly in our calculations
#Addition
print("adding 2+4=",2+4)
#Subtraction
print("subtracting 2-4=",2-4)
#Division
print("Dividing 4/3=",4/3)
#Division and get integer value at answer
print("Dividing 4/3 and getting integer answer=",4//3)
#Multiplication
print("multiplying 2*4=",2*4)
#remainder (remainder is whats left once you divide 2 numbers for example 4 divided by 3 gives 1 as remainder)
print("Finding remainder of 4/3=",4%3)
#Power
print("2 raised to the power 3 equals=",2**3) #This is equivalent to 2 raised to power 2
raise = 3
import = 4
Rose = 3
rOse = 2
roSe = 1
rosE = 0
ROSE = -1
rose = -2
print(Rose)
print(rOse)
print(roSe)
print(rosE)
print(ROSE)
print(rose)
print("without parenthesis",1+2-4/5*9)
print("with parenthesis",1+2-4/(5*9))
print("without brakcets: ",1+2*5+9)
print("with brakcets: ",(1+2)*(5+9))
#Example 1
string1 = "Hello"
string2 = "World"
string = string1 + string2
print(string)
#Example 1
greeting = "Hello! "
name = "John"
punctuation = "!"
message = " Welcome to Python 101"
print(greeting+name+punctuation+message)
greeting = "Hello!"
name = "John"
punctuation = "!"
message = "Welcome to Python 101"
space = " "
print(greeting+space+name+punctuation+space+message)
if(condition):
perform these actions
and these as well
and these too if the condition is true
else:
perform these actions if condition is false
other lines of code
Notice the colon at the end of if condition and all actions below it are indented by one tab. last line is not indented. else statement is run only when the condition is False and if block is not executed.
Example below:
#Insert random numbers in numberone and two and see the if-else in action:
numberOne = 3
numberTwo = 2
if(numberOne>numberTwo):
print("Yes")
else:
print("No")
# Similarly another example. We can take decisions based on strings as well.
user = "Female"
if(user=="Male"):
print("He is a nice guy")
else:
print("She is a nice girl")
#Another Example. Using input to get amount and conversion direction to convert either from PKR to USD or USD to PKR
amount = int(input("Enter amount in PKR or USD: ")) #int() in start helps converting string input to integer value. We'll learn about it in type castings
conversionMode = int(input("Enter conversion direction \n 1 for USD to PKR \n any other digit for PKR to USD: "))
if(conversionMode==1):
print(f"Total amount for $ {amount} is Rs.{amount*160}")
else:
print(f"Total amount for PKR {amount} is ${amount/160}")
if(condition):
perform these actions
and these as well
and these too if the condition is true
and dont elif or else below.
elif(check this second condition):
perform these actions if second condition is true
and dont execute elif or else below
elif(check this third condition)
perform these actions if third condition is true
and dont execute elif or else below.
else:
perform these actions if no option is true
other lines of code
you can add as many elif blocks as you have conditions
Example below:
# We can now modify the above example and add an elif block to it.
user = input("Enter Gender Male/Female")
if(user=="Male"):
print("He is a nice guy")
elif(user=="Female"):
print("She is a nice girl")
else:
print("invalid option")
#we can quickly see the results as following:
print(2==3)
print(2!=3)
print(2<3)
print(2>3)
print(2<=3)
print(2>=3)
print("Head"=="Head")
print("Tails"!="Head")
print("Apples"=="Oranges")
#### Example:
a = True
b = False
c = True
print(a and b)
print(a or b and c)
print(a and b or c)
print(a and (b and c))
#Example
a = 30
# so it is less than 20 and less than 5 as well.
if(a<20 and a>5): #This condition will not be true as it will check for all conditions and it will mark it true only if all conditions are true
print("The and keyword is run")
elif(a<20 or a>5): #or keyword checks if any one of the conditions are true, it will work.
print("The or keyword is run")
else:
print("none")
# First, lets get username and password as input
username = input("Enter Username: ")
password = input("Enter Password: ")
# for now we use same input to get password. later we will import a library to take hidden password
# Next step is to check whether the username and password are correct.
if(username=="ahsan" and password=="1234"):
# Here all statements will be
age = int(input("enter your age")) #converting age to integer.
if(age<21):
print("You cannot get a driving license")
elif(age>=21 and age<=40):
print("You may proceed to get a driving license")
else:
print("Please arrange a health certificate before applying")
else:
print("invalid username/password")
#Run this program and enter hello.
if(input()=="hello"):
print("yes! you wrote hello")
else:
print("Sorry")
# Example:
# Lets define a list of city names
cities = ["Moscow","Islamabad","Lahore","Peshawar"]
# Printing complete list
print(cities)
# printing individual elements of list
print(cities[0])
print(cities[1])
print(cities[2])
print(cities[3])
print("The list of cities are ",cities)
print("The first city in list is ",cities[0])
# Adding Karachi and Quetta to the list
print(cities)
print("\nAfter adding Karachi: ")
cities.append("Karachi")
print(cities)
print("\nAfter adding Quetta: ")
cities.append("Quetta")
print(cities)
cities = cities + ["Faisalabad","Multan"]
print(cities)
cities.insert(2,"Gujrat")
print("after inserting Gujrat")
print(cities)
cities.insert(5,"Gujranwala")
print("after inserting Gujranwala")
print(cities)
#
print(cities[2:5])
print(cities[:5])
print(cities[5:])
del cities[9] #to delete multan
print("After deleting Multan: ")
print(cities)
del cities[0] #to delete multan
print("After deleting Moscow: ")
print(cities)
#Example
#Lets remove Quetta from the list of cities we are working with.
cities.remove("Quetta")
print("After removing Quetta:",cities)
city_name = cities.pop(1)
print(city_name)
print("cities list after popping out Gujrat ",cities)