My First Python Codes
My First Python Codes
FEB 3, 2024
PRINT FUNCTION
In [1]: print ("Hello world!")
Hello world!
hello everyone??
1234567890
1234567890
fueuuehh123
code 29 first!
hiufefehduuvfy3932j u8911jj119199iwjenueueuduehwbwjjw
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 [11]: a = 10
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[1], line 1
----> 1 print (a)
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)
float
Out[21]:
str
Out[26]:
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)
TYPES OF VARIABLES
In [7]: a = 12
b = 1.5
c = "Salahuddin"
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"
Full_Name is HaniyahAbdulSamad
num1 = 70
num2 = 25
sum of 70 and 25 is 95
95
Sum: 95
num1 = 70
num2 = 25
Difference of 70 and 25 is 45
Difference is 45
num1 = 8
num2 = 7
Product of 8 and 7 is 56
56
Product is 56
num1 = 8
num2 = 7
Product of 8 and 7 is 56
18 FEB, 2023
In [1]: # DIVISION
In [5]: # FLOAT
num1 = 50
num2 = 5
In [6]: # FLOAT
num1 = 50
num2 = 4
In [7]: # INTEGER
num1 = 50
num2 = 5
quotient of 50 and 5 is 10
In [8]: # INTEGER
num1 = 50
num2 = 4
quotient of 50 and 4 is 12
In [10]: # TASK
# 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
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
24 FEB, 2023
if - else statement
In [16]: Bank_Balance = 20000
Withdaw_Amount = 5000
Transaction Successful.
Remaining Balance: 15000
Insufficient Balance
Bank_Balance = 20000
Withdaw_Amount = 20000
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
25 FEB, 2023
In [21]: # Task: find out the grades of students (Marks = 83)
# Grading Policy:
# greater than 90 = A*
# 81 - 90 = A
# 71 - 81 = B
# 61 - 70 = C
# 51 - 60 = D
# Less than 50 = F
Marks = 43
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)
savedPassword = "1122"
if (savedPassword == userpin):
print ("correct PIN. screen unlocked")
else:
print ("Incorrect PIN")
code = 1234
if (code == userInput):
print ("1234 (str) is equal to 1234 (int)")
else:
print ("1234 (str) is not equal to 1234 (int)")
Enter 1234.1234
1234 (str) is not equal to 1234 (int)
savedPassword = "1122"
if (savedPassword == userpin):
print ("correct PIN. screen unlocked")
else:
print ("Incorrect PIN")
localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 10/25
5/3/24, 5:32 PM My First Python Codes
In [26]: # Make a program to get email and password from user and check either its correct
# Hint: (Save yor email & password as default in your code.)
saved_email = "[email protected]"
saved_password = "hani123"
if (saved_email == input_email):
if (saved_password == input_password):
print ("Login Successful.")
else:
print ("Wrong password.")
else:
print ("Wrong email.")
SIMPLE CALCULATOR
In [1]: num1 = int(input("Enter 1st number:"))
operater = input("+ , - , * , /")
num2 = int(input("Enter 2nd number:"))
if (operater == "+"):
add = num1 + num2
print ("Sum of" , num1 , "and" , num2 , "is" , add)
#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.")
print ("Your remaining Balance is" , Account2_Balance)
else:
print ("Insufficient Balance.")
else:
print ("Invalid Balance.")
# 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.")
Sibling1 = "Haniyah"
Sibling2 = "Salahuddin"
Sibling3 = "Shaheedu"
Sibling4 = "Zaeemu"
LIST
In [ ]: # List is used store multiple values in a single variable.
# We use [] to make a list.
# Syntax:
# ListName = [value1 , value2 , value3 , value4 , .......]
numbs = [29 , 24 , 17 , 10 , 1 , 21 , 9 , 2 , 13 ]
print (numbs)
localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 13/25
5/3/24, 5:32 PM My First Python Codes
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.....
In [17]: # Task:
# Print Account Holder Names with Account Balance
In [20]: # Syntax:
# Len(ListName)
Length : 5
Functions of List
In [18]: Siblings = ["Haniyah" , "Salahuddin" , "Shaheedu" , "Zaeemu"]
APPEND
In [23]: # Append function is use to add any value in the list
# Append function will add the value at the enf of the list.
# Syntax:
# ListName.append(Value)
# Task: Add "Ammar" In the list of siblings and print the list.
Siblings.append("Ammar")
print (Siblings)
# Task: Add "Naimah" In the list of siblings and print the list.
Siblings.append("Naimah")
print (Siblings)
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)
Siblings.insert(0 , "Naimah")
print ("Siblings List:" , Siblings)
Siblings.insert(4 , "Inayah")
print ("Siblings list:" , Siblings)
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
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 effect copie
# Syntax: CopiedListName = MainListName.Copy()
# Copy by refference
# After creating a copy of list, any changes in main list will also gets changed in co
# CopiedlistName = MainListName
Copy By Value
In [24]: friends = ["Hareem" , "Hala" , "Inayah" , "Hiba"]
CopyFriends = friends.copy()
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
friends.count("Haniayh")
2
Out[2]:
friends.count("Sana")
1
Out[3]:
EXTEND
In [4]: # It is use to add multiple values together in the list.
# friends.append("Naimah")
print (friends)
friends.extend(new_friends)
print (friends)
31 MAR, 2024
INDEX
In [33]: # Return index number of the value
friends.index("Usama")
2
Out[33]:
In [34]: friends.index("Ali")
1
Out[34]:
In [36]: friends
friends.index("Usman")
0
Out[37]:
In [38]: friends.count("Usman")
friends.index("Usman" , friends.index("Usman")+1)
4
Out[39]:
In [40]: friends = ['Ali', 'Usama', 'Salman', 'Usman', 'Nial', 'Mikael', 'Atiq', 'Ali' , "Usman
friends.index("Usman" , friends.index("Usman")+1)
8
Out[41]:
friends.index("Ali")
0
Out[42]:
friends.index("Ali" , friends.index("Ali")+1)
7
Out[43]:
In [44]: # Example:
friends = ['Ali', 'Usama', 'Salman', 'Usman', 'Nial', 'Mikael', 'Atiq', 'Ali']
print (friends)
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.
RemovedValue = students.pop()
print ("Removed Value is" , RemovedValue)
print ("Updated list is" , students)
RemovedValue = friends.pop(3)
print ("Removed Value is" , RemovedValue)
print ("Updated list is" , friends)
i = friends.index("Ibrahim")
RemovedValue = friends.pop(i)
print ("Removed Value is" , RemovedValue)
print ("Updated list is" , friends)
# Task:
# Remove "Burair" from Section_A and add it in Section_B.
i = Section_A.index("Burair")
RemovedValue = Section_A.pop(i)
Section_B.append(RemovedValue)
localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 21/25
5/3/24, 5:32 PM My First Python Codes
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)
friends.remove("Asad")
print ("Updated List:" , friends)
In [57]: # Task:
# 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
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
nums = [9,56,8,94,2,34,56,7,834,786]
nums.sort(reverse = True)
print ("Sorted list:" , nums)
nums = [9,56,8,94,2,34,56,7,834,786]
nums.sort()
nums.reverse()
print ("Sorted list:" , nums)
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!")
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.
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
print(nums[-4])
19
print(nums[3 : -3 + 1])
print(nums[-5 : ])