My First Python Codes - Jupyter Notebook
My First Python Codes - Jupyter Notebook
My First Python Codes - Jupyter Notebook
FEB 3, 2024
PRINT FUNCTION
In [1]: print ("Hello world!")
Hello world!
hello everyone??
1234567890
1234567890
fueuuehh123
code 29 first!
hiufefehduuvfy3932j u8911jj119199iwjenueueuduehwbwjjw
Name Marks(100)
Haniyah 100
Salahuddin 99
In [3]: print ("Haniyah Samad", 15, "October", 29, sep = " **** ")
In [4]: print ("Haniyah Samad", 15, "October", 29, sep = " :)) ")
Haniyah Samad
15
October
29
In [8]: print("Haniyah samad", 15, "october", 29, end = " Next Statement ")
print("Haniyah Samad", 15, "October", 29)
In [9]: print("Haniyah samad", 15, "october", 29, end = " *** Next *** ")
print("Haniyah Samad", 15, "October", 29)
VARIABLE
In [10]: # Variable is just like a box in which you can store any type of data.
# Variables are used to store values.
In [1]: a = 10
10
value of a is 10
In [15]: a = 12.2
print ("value of a is", a)
value of a is 12.2
In [21]: type(a)
Out[21]: float
Out[26]: str
In [27]: # Variable name can be anything, but it should not start with a number.
Inayah
Inayah
Inayah
Inayah
DATA TYPES
In [35]: # Integers data types
# Integers are numbers (0,1,2,3,4,5,99,34567)
# Float data types
# Float data types are for decimals numbers (1.56 , 76.567)
# String data type
# string data type is for characters (any words or sentences or any combinatio
TYPES OF VARIABLES
In [7]: a = 12
b = 1.5
c = "Salahuddin"
print ("Type of variable a is", type(a))
print ("Type of varialbe b is", type(b))
print ("Type of variable c is", type(c))
OPERATERS
In [ ]: # ADD: +
# SUBTRACTION: -
# MULTIPLICATION: *
# Integers DIVISION: //
# Float DIVISION: /
ADDITION OF VARIABLES
In [1]: # Addition of Strings (known as concatination)
First_Name = "Haniyah"
Middle_Name = "Abdul"
Last_Name = "Samad"
# Task: Print Full Name.
Full_Name = First_Name + Middle_Name + Last_Name
print ("Full_Name is", Full_Name)
Full_Name is HaniyahAbdulSamad
sum of 95
sum of 70 and 25 is 95
95
Sum: 95
Difference of 70 and 25 is 45
Difference is 45
Product of 8 and 7 is 56
56
Product is 56
Product of 8 and 7 is 56
18 FEB, 2023
In [1]: # DIVISION
# "/" == FLOATING POINT DIVISION
# "//" == INTEGER DIVISION
In [5]: # FLOAT
num1 = 50
num2 = 5
Quotient = num1 / num2
print ("quotient of", num1, "and", num2, "is", Quotient)
In [6]: # FLOAT
num1 = 50
num2 = 4
Quotient = num1 / num2
print ("quotient of", num1, "and", num2, "is", Quotient)
In [7]: # INTEGER
num1 = 50
num2 = 5
Quotient = num1 // num2
print ("quotient of", num1, "and", num2, "is", Quotient)
quotient of 50 and 5 is 10
In [8]: # INTEGER
num1 = 50
num2 = 4
Quotient = num1 // num2
print ("quotient of", num1, "and", num2, "is", Quotient)
quotient of 50 and 4 is 12
In [10]: # TASK
# Take 8 different numbers in 8 different variables.
# Add first 2 numbers and print the sum.
# Subtract 3rd and 4th numbers and print the difference.
# Multiply 5th and 6th numbers and print the product.
# Divide (integer division) 7th & 8th numbers and print the Quotient.
# Note: print function should be in detail
# 8 variables
num1 = 68
num2 = 13
num3 = 43
num4 = 73
num5 = 23
num6 = 67
num7 = 13
num8 = 15
# Add
Sum = num1 + num2
print ("Sum of", num1, "and", num2, "is", Sum)
# Subtract
Difference = num3 - num4
print ("Difference between", num3, "and", num4, "is", Difference)
# Product
Product = num5 * num6
print ("product of", num5, "and", num6, "is", Product)
# Division
Quotient = num7 // num8
print ("Quotient of", num7, "and", num8, "is", Quotient)
Sum of 68 and 13 is 81
Difference between 43 and 73 is -30
product of 23 and 67 is 1541
Quotient of 13 and 15 is 0
IF STATEMENT
In [13]: Bank_Balance = 20000
Withdaw_Amount = 5000
if (Bank_Balance > Withdaw_Amount):
print("Transaction Successful.")
print("Remaining Balance:" , Bank_Balance - Withdaw_Amount)
Transaction Successful.
Remaining Balance: 15000
Transaction Successful.
Remaining Balance: 1900
In [15]: Haniyah = 15
Musab = 8
if (Haniyah > Musab):
print ("Haniyah is senior.")
Haniyah is senior.
24 FEB, 2023
if - else statement
In [16]: Bank_Balance = 20000
Withdaw_Amount = 5000
if (Bank_Balance > Withdaw_Amount):
print("Transaction Successful.")
print("Remaining Balance:" , Bank_Balance - Withdaw_Amount)
else:
print ("Insufficient Balance")
Transaction Successful.
Remaining Balance: 15000
Insufficient Balance
Insufficient Balance
if - elif statement
In [19]: # Issue (Wrong Example, need of elif statement)
# Task: Take 2 numbers in 2 different variables.
# Find out, which number is greater
num1 = 10
num2 = 10
if (num1 > num2):
print ("first number", num1, "is greater than second number", num2)
else:
print ("second number", num2 , "is greater than first number", num1)
25 FEB, 2023
else:
print ("F Grade.")
F Grade.
SIMPLE CALCULATOR
In [12]: num1 = 88
operater = "*"
num2 = 50
if (operater == "+"):
add = num1 + num2
print ("Sum of" , num1 , "and" , num2 , "is" , add)
Enter 1234.1234
1234 (str) is not equal to 1234 (int)
In [26]: # Make a program to get email and password from user and check either its corr
# Hint: (Save yor email & password as default in your code.)
saved_email = "[email protected]"
saved_password = "hani123"
input_email = input("Enter your email id:")
input_password = input("Enter your password:")
if (saved_email == input_email):
if (saved_password == input_password):
print ("Login Successful.")
else:
print ("Wrong password.")
else:
print ("Wrong email.")
SIMPLE CALCULATOR
In [ ]: # Account Detailes
Account1_name = "Haniyah Samad"
Account1_ATM = "20081029"
Account1_PIn = "3456"
Account1_Balance = 90000
Account2_name = "Muhammad Musab"
Account2_ATM = "498765"
Account2_PIn = "9876"
Account2_Balance =80000
Account3_name = "Naimah Baji"
Accont3_ATM = "202026"
Account3_PIn = "1234"
Account3_Balance = 70000
# Enter ATM card
user_ATM = input("Please Enter your ATM Card Number:")
# Account_1
if (user_ATM == Account1_ATM):
print (Account1_name)
user_PIn = input("Enter your 4-Digit PIn:")
if (user_PIn == Account1_PIn):
option = input ("Press 1 to check Balance , Press 2 to Withdraw Cash."
if (option == "1"):
print ("Your available Balance: Rs" , Account1_Balance)
elif (option == "2"):
Withdraw_Amount = int(input("Enter Amount to Withdraw:"))
if (Withdraw_Amount <= Account1_Balance):
Account1_Balance = Account1_Balance - Withdraw_Amount
print ("Withdraw Successful")
print ("Take Your ATM Card.")
print ("Take Your Cash.")
print ("Your remaining Balance is" , Account1_Balance)
else:
print ("Insufficient Balance.")
else:
print ("Invalid Balance.")
else:
print ("Wrong PIn Code.")
#Account 2
elif (user_ATM == Account2_ATM):
print (Account2_name)
user_PIn = input("Enter your 4-Digit PIn:")
if (user_PIn == Account2_PIn):
option = input ("Press 1 to check Balance , Press 2 to Withdraw Cash."
if (option == "1"):
print ("Your available Balance: Rs" , Account2_Balance)
elif (option == "2"):
Withdraw_Amount = int(input("Enter Amount to Withdraw:"))
if (Withdraw_Amount <= Account2_Balance):
Account2_Balance = Account2_Balance - Withdraw_Amount
print ("Withdraw Successful")
print ("Take Your ATM Card.")
print ("Take Your Cash.")
localhost:8888/notebooks/My First Python Codes.ipynb 16/51
5/17/24, 10:41 PM My First Python Codes - Jupyter Notebook
print ("Your remaining Balance is" , Account2_Balance)
else:
print ("Insufficient Balance.")
else:
print ("Invalid Balance.")
else:
print ("Wrong PIn Code.")
# Account 3
elif (user_ATM == Account3_ATM):
print (Account3_name)
user_PIn = input("Enter your 4-Digit PIn:")
if (user_PIn == Account3_PIn):
option = input ("Press 1 to check Balance , Press 2 to Withdraw Cash."
if (option == "1"):
print ("Your available Balance: Rs" , Account3_Balance)
elif (option == "2"):
Withdraw_Amount = int(input("Enter Amount to Withdraw:"))
if (Withdraw_Amount <= Account3_Balance):
Account3_Balance = Account3_Balance - Withdraw_Amount
print ("Withdraw Successful")
print ("Take Your ATM Card.")
print ("Take Your Cash.")
print ("Your remaining Balance is" , Account3_Balance)
else:
print ("Insufficient Balance.")
else:
print ("Invalid Balance.")
else:
print ("Wrong PIn Code.")
LIST
In [ ]: # List is used store multiple values in a single variable.
# We use [] to make a list.
# Syntax:
# ListName = [value1 , value2 , value3 , value4 , .......]
INDEX NUMBER
In [5]: # Index no is the address of each value in the list
# First value has index no 0, next value has index no 1, and so on.....
# List = [Value1 , Value2 , Value3 , Value4......]
# Index = 0 1 2 3 ......
In [17]: # Task:
# Print Account Holder Names with Account Balance
print ("Account1 :" , Account1[0] , ":" , Account1[3])
print ("Account2 :" , Account2[0] , ":" , Account2[3] )
Length : 5
Functions of List
In [18]: Siblings = ["Haniyah" , "Salahuddin" , "Shaheedu" , "Zaeemu"]
APPEND
INSERT
In [1]: # Difference between Append and Insert
# Append
# It will add the new value at the end of the list
# Syuntax: ListName.append(value)
# Insert
# It will add the new value at any given index number
# Syntax: Listname.insert(index no , value)
In [2]: # Task:
# Create a list of your 3 Friends.
# Add a new friend name "Samreen" at first place in previous list.
# Add "END" at the last of the list.
# Print the list
Friends = ["Sarah" , "Zurwah" , "Saqia"]
Friends.insert(0 , "Samreen")
Friends.append("END")
print (Friends)
CLEAR
In [7]: # IT will remove all the values from the list
# It will make the list empty
In [8]: print(Friends)
In [10]: # Task: Remove all the lines from the Friends list
Friends.clear()
print ("List:" , Friends)
List: []
COPY
In [23]: # Copy by value
# After creating a new copy of of list, any changes in main list will not effe
# Syntax: CopiedListName = MainListName.Copy()
# Copy by refference
# After creating a copy of list, any changes in main list will also gets chang
# CopiedlistName = MainListName
Copy By Value
localhost:8888/notebooks/My First Python Codes.ipynb 21/51
5/17/24, 10:41 PM My First Python Codes - Jupyter Notebook
In [27]: # Task: Append a new value "Naimah" into the main list and print both lists
friends.append("Naimah")
In [30]: # Task: Append a new value "Mirha" into the copied list and print both list
CopyFriends.append("Mirha")
Copy By Reference
In [32]: friends = ["Hareem" , "Hala" , "Inayah" , "Hiba"]
CopyFriends = friends
In [34]: # Task: Append a new value "Naimah" into the main list and print both lists
friends.append("Naimah")
In [36]: # Task: Append a new value "Mirha" into the copied list and print both list
CopyFriends.append("Mirha")
Count
In [ ]: # It will count, how many time any specific value occurs in the list
Out[2]: 2
Out[3]: 1
EXTEND
In [4]: # It is use to add multiple values together in the list.
31 MAR, 2024
INDEX
In [33]: # Return index number of the value
friends = ["Usman" , "Ali" , "Usama" , "Salman"]
# Find out index number of "Usama"
friends.index("Usama")
Out[33]: 2
In [34]: friends.index("Ali")
Out[34]: 1
In [36]: friends
Out[36]: ['Usman', 'Ali', 'Usama', 'Salman', 'Usman', 'Nial', 'Mikael', 'Atiq', 'Al
i']
Out[37]: 0
In [38]: friends.count("Usman")
Out[38]: 2
Out[39]: 4
In [40]: friends = ['Ali', 'Usama', 'Salman', 'Usman', 'Nial', 'Mikael', 'Atiq', 'Ali'
Out[41]: 8
Out[42]: 0
Out[43]: 7
In [44]: # Example:
friends = ['Ali', 'Usama', 'Salman', 'Usman', 'Nial', 'Mikael', 'Atiq', 'Ali']
print (friends)
user_input = input("Enter a name to find out it's index number:")
c = friends.count(user_input)
if (c == 0):
print ("Value not found!")
elif (c == 1):
i = friends.index(user_input)
print ("Index no of" , user_input , "is" , i)
elif (c == 2):
print ("2 Values found")
i1 = friends.index(user_input)
print ("Index no of first occurence is" , i1)
i2 = i = friends.index(user_input , i1 + 1)
print ("Index no of 2nd occurence is" , i2)
Pop Function
In [45]: # Pop function is used to remove any value from the list
# It will return you the value
# By defualt, it remves the last value
# It can also remove a value by any index number
# Syntax:
# Variable = ListName.pop() // remove the last value
# Variable = LisrName.pop(index no) // removes the value at given index number
REMOVE
In [52]: # remove function is use to remove any value from the list.
# It will remove Specific value given as parameter.
# It will not return the Value
# Syntax:
# ListName.remove(value)
In [57]: # Task:
friends = ["Usman" , "Usama" , "Asad" , "Abeera" , "Ibrahim"]
# Take input a name from user and remove it from the list.
print (friends)
name = input("Enter a name to remove from the list: ")
if (name in friends):
friends.remove(name)
else:
print ("Value not found!")
print ("Updated List:" , friends)
REVERSE
In [59]: # It will reverse the order of the list
friends = ["Usman" , "Usama" , "Asad" , "Abeera" , "Ibrahim"]
friends.reverse()
print (friends)
In [61]: # Example
nums = [0,1,2,3,4,5,6,7,8,9]
nums.reverse()
print (nums)
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
SORT
In [62]: # It will sort the list items in ascending order
In [5]: TAsk: Give 1-10 options to the user to select any, and perform the task.
# 1. Create a list of friends(Add your name too).
# 2. Take input a new name from the user and add it in the list.
# 3. Delete your name from the list.
# 4. Take input 3 more names from the user and add it in the list.
# 5. Take input a name from the user and delete it.
# 6. Take input name from the user and find out it's index number.
# 7. Create a copy of the list.
# 8. Sort the list in descending order.
# 9. Print the friends list.
# 10. Print the copied list.
print("1. Create a list of your friends")
print("2. Take input a new friends name from the user and add it in the list."
print("3. Delete your name from the list.")
print("4. Take input 3 more names from the user and add it in the list.")
print("5. Take input a name from the user and delete it.")
print("6. Take input name from the user and find out it's index number.")
print("7. Create a copy of the list.")
print("8. Sort the list in descending order.")
print("9. Print the friends list.")
print("10. Print the copied list.")
option = int(input("Select any option from 1 - 10: "))
if (option == 1):
friends = ["Usman" , "Usama" , "Salman" , "Mubashar"]
print("Task 1: Friends List:" , friends)
elif (option == 2):
new_name = input("Enter a new friend name: ")
friends.append(new_name)
print("Task 2: Friends List:" , friends)
elif (option == 3):
friends.remove("Mubashar")
print("Task 3: Friends List:" , friends)
elif (option == 4):
name1 = input("Enter 1st name: ")
name2 = input("Enter 2nd name: ")
name3 = input("Enter 3rd name: ")
friends.extend([name1 , name2, name3])
print("Task 4: Friends List:" , friends)
elif (option == 5):
name = input("Enter a name to delete: ")
if (name in friends):
friends.remove(name)
else:
print("Value not found!")
print("Task 5: Friends List:" , friends)
elif (option == 6):
name = input("Enter a name to find it's index number: ")
localhost:8888/notebooks/My First Python Codes.ipynb 31/51
5/17/24, 10:41 PM My First Python Codes - Jupyter Notebook
if (name in friends):
i = friends.index(name)
print("Task 6: Index No:" , i)
else:
print("Value not found!")
elif (option == 7):
copy_friends = friends.copy()
print("Task 7: Copied List:" , copy_friends)
elif (option == 8):
friends.sort(reverse = True)
print("Task 8: Friends List:" , friends)
elif (option == 9):
print("Task 9: Friends List:" , friends)
elif (option == 10):
print("Task 10: Copied List:" , copy_friends)
else:
print("Invalid Option")
File <tokenize>:70
print("Task 8: Friends List:" , friends)
^
IndentationError: unindent does not match any outer indentation level
In [51]: # Task:
# Create a Blood group directory (having 3 names and their Blood Group)
# and give user an option.
# Press 1 to search Blood Group by name.
# Press 2 to delete a contact by name.
# Press 3 to Blood Group of any contact by name.
# Press 4 to print all the details.
# Press 5 to add any new name and it's Blood Group .
# Hint: Create a separate list of Names and Blood Groups.
Names = ["Abaan" , "Haniyah" , "Ali"]
Blood_Groups = ["B+" , "O-" , "A+"]
print ("Press 1 to search Blood Group by name")
print ("Press 2 to delete a contact by name")
print ("Press 3 to change Blood Group of any contact by name")
print ("Press 4 to print all the details")
print ("Press 5 to add any new name and it's Blood Group")
option = input("Select any option (1 - 5)")
if (option == "1"):
name_input = input("Enter a name:")
if (name_input in Names):
i = Names.index(name_input)
Blood = Blood_Groups[i]
print(name_input , ":" , Blood)
else:
print ("Name is not in the list!")
elif (option == "4"):
print (Names , ":" , Blood_Groups)
elif (option == "5"):
localhost:8888/notebooks/My First Python Codes.ipynb 34/51
5/17/24, 10:41 PM My First Python Codes - Jupyter Notebook
new_name = input("Enter new contact name: ")
new_BG = input("Enter Blood Group:")
Names.append(new_name)
Blood_Groups.append(new_BG)
print (Names , ":" , Blood_Groups)
else:
("Wrong option selected!")
SLICING
In [1]: nums = [2,4,7,9,12,15,19,24,28,31,39,46,53,72]
# Task: print 4th to 10th value of above list with index number.
print(nums[3] , nums[4] ,nums[5] , nums[6] , nums[7] , nums[8] , nums[9])
9 12 15 19 24 28 31
In [3]: # Slicing will give you all the values of list within its range
# Syntax: ListName[starting index : ending index + 1]
# Slicing will give you the value of starting index
# But, it will not give you value of ending index (will end before it, use + 1
nums = [2,4,7,9,12,15,19,24,28,31,39,46,53,65]
# Task: Print 4th to 10th value of above list with index number.
print(nums[3 : 9 + 1])
[7, 9, 12, 15, 19, 24, 28, 31, 39, 46, 53, 65]
Frist Value 2
Last Value 65
19
TUPLE
Out[13]: 2
In [33]: # Task: Take input a name from user and find it's index number.
name = input("Enter a name to get it's index no:")
Tuple_Names.index(name)
Out[33]: 2
# elif Account[1]
localhost:8888/notebooks/My First Python Codes.ipynb 39/51
5/17/24, 10:41 PM My First Python Codes - Jupyter Notebook
# elif Account[2]
# else
In [27]: Accounts[0]
In [29]: Accounts[2]
In [30]: Accounts[0][1]
Out[30]: '20081029'
In [31]: Accounts[1][0]
In [32]: Accounts[0][0]
Loops
In [35]: # Loop is used to repeat any instruction/s multiple times.
# Repitation depends on range of the Loop
1 Haniyah
2 Haniyah
3 Haniayh
4 Haniyah
5 Haniyah
FOR LOOP
In [38]: # It will repeat itself until a specific range gets false.
# Syntax:
# for variable in range(repitation value):
# instruction/s
Haniyah
Haniyah
Haniyah
Haniyah
Haniyah
0
1
2
3
4
0 Haniyah
1 Haniyah
2 Haniyah
3 Haniyah
4 Haniyah
1 Haniyah
2 Haniyah
3 Haniyah
4 Haniyah
5 Haniyah
In [3]: # Take input 5 numbers from user and check each number either it's even/odd
for a in range(5):
num = int(input(f"Enter {a+1} number: "))
if (num % 2 == 0):
print(f"Number {num} is even.")
else:
print(f"Number {num} is odd.")
Enter 1 number: 3
Number 3 is odd.
Enter 2 number: 6
Number 6 is even.
Enter 3 number: 45
Number 45 is odd.
Enter 4 number: 78
Number 78 is even.
Enter 5 number: 23
Number 23 is odd.
In [5]: # Take input a number from user and print it's multiples/tables(upto 10)
# Format: if num is 8 , then print 8 x 1 = 8 ,......
num = int(input("Enter a number to print it's multiples: "))
#print(num*1)
#print(num*2)
#print(num*3)
for a in range(10):
print(f"{num} x {a+1} = {num*(a+1)}")
MAY 11 , 2024
In [7]: # Range of for loop can be a value, list, string, tuple, etc........
Student 1 : 90
Student 2 : 83
Student 3 : 75
Student 4 : 100
Student 5 : 23
Student 6 : 45
In [2]: num = 1
for a in [90, 82, 67, 45, 76, 23,]:
print("Student" , num , ":" , a)
num = num + 1
Student 1 : 90
Student 2 : 82
Student 3 : 67
Student 4 : 45
Student 5 : 76
Student 6 : 23
P
a
k
i
s
t
a
n
In [6]:
for a in "Pakistan":
print(a)
P
a
k
i
s
t
a
n
Pakistan
China
In [11]: # Add all numbers from 1 tp 100 and print the sum.
sum = 0
for a in range(101):
sum = sum + a
print("Sum =" , sum)
Sum = 5050
WHILE LOOP
In [12]: # It will repeat itself until a specific condition gets false:
# Syntax
# while(condition):
# instruction/s
While Loop
Outside while loop.
In [17]: num = 0
while(num < 11):
print(num)
num = num + 1
0
1
2
3
4
5
6
7
8
9
10
if (operater == "+"):
sum = num1 + num2
print ("Sum =" , sum)
BREAK STATEMENT
localhost:8888/notebooks/My First Python Codes.ipynb 46/51
5/17/24, 10:41 PM My First Python Codes - Jupyter Notebook
In [3]: # Task: You have a students list having only your name as the first value.
# Use while loop
# Ask the user to either Enter a new student name or "Q" to quite:
# If input is "Q" , end the loop. Else append the value in list.
students = ["Haniyah"]
while(True):
user_input = input("Enter a new student name or 'Q' to quite:")
if (user_input == 'Q'):
break
else:
students.append(user_input)
print(students)
MAY 12 , 2024
In [4]: # It will skip that particular iteration of loop
print(students)
1
2
7
45
143
23
96
13
45
463
UPPER CASE
In [8]: # ALL LETTERS CAPITAL
country = input("Enter your country name:")
print("Your country name is" , country.upper())
print(students)
lower case
print(students)
Title case
In [14]: # Only First Letter Capital Of Each
country = input("Enter your country name:")
print("Your country name is" , country.title())
NESTED LOOP
In [15]: # Loop inside another loop
0
1
2
3
4
5
1
2
2
3
3
3
4
4
4
4
5
5
5
5
5
In [19]: # Task:
# Print
# * * *
# * * *
# * * *
for a in range(5):
print("* * *")
* * *
* * *
* * *
* * *
* * *
In [21]: # Task:
# Take input a number from user and print a square of "*"
# Square is a shape with equal sides.
# Hint: if user enters 3, then.....
# * * *
# * * *
# * * *
size = int(input("Enter size of square: "))
for a in range(size):
for b in range(size):
print("*" , end = " ")
print()
In [3]: #Print
# *
# * *
# * * *
# * * * *
# * * * * *
In [ ]: