Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

My First Python Codes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

My First Python Codes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

5/3/24, 5:32 PM My First Python Codes

FEB 3, 2024

PRINT FUNCTION
In [1]: print ("Hello world!")

Hello world!

In [2]: print ("hello everyone??")

hello everyone??

In [3]: print (1234567890)

1234567890

In [4]: print ("1234567890")

1234567890

In [5]: print ("HELLO gyus I am Haniyah Abdul Samad!!!!!")

HELLO gyus I am Haniyah Abdul Samad!!!!!

In [7]: print ("how are you fine? me too............")

how are you fine? me too............

In [8]: print ("fueuuehh123")

fueuuehh123

In [ ]: print ("code" 29 "first!")

In [10]: print ("code", 29, "first!")

code 29 first!

In [1]: print ("hiufefehduuvfy3932j u8911jj119199iwjenueueuduehwbwjjw")

hiufefehduuvfy3932j u8911jj119199iwjenueueuduehwbwjjw

Feb 10, 2024


In [4]: print("Haniyah samad", 15, "october", 29)
print("Haniyah Samad", 15, "October", 29)

Haniyah samad 15 october 29


Haniyah Samad 15 October 29

In [5]: print("Name" , " ", "Marks(100)")


print("Haniyah", " ", 100)
print("Salahuddin", " ", 99)

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 1/25


5/3/24, 5:32 PM My First Python Codes
Name Marks(100)
Haniyah 100
Salahuddin 99

In [6]: print("haniyah samad", 15 "october", 29, sep = " **** ")

Cell In[6], line 1


print("haniyah samad", 15 "october", 29, sep = " **** ")
^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

In [3]: print ("Haniyah Samad", 15, "October", 29, sep = " **** ")

Haniyah Samad **** 15 **** October **** 29

In [4]: print ("Haniyah Samad", 15, "October", 29, sep = " :)) ")

Haniyah Samad :)) 15 :)) October :)) 29

In [7]: print ("Haniyah Samad", 15, "October", 29, sep = "\n")

Haniyah Samad
15
October
29

In [8]: print("Haniyah samad", 15, "october", 29, end = " Next Statement ")
print("Haniyah Samad", 15, "October", 29)

Haniyah samad 15 october 29 Next Statement Haniyah Samad 15 October 29

In [9]: print("Haniyah samad", 15, "october", 29, end = " *** Next *** ")
print("Haniyah Samad", 15, "October", 29)

Haniyah samad 15 october 29 *** Next *** 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

In [1]: print (a)

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[1], line 1
----> 1 print (a)

NameError: name 'a' is not defined

In [2]: print ("a")

In [14]: print ("value of a is", a)

value of a is 10

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 2/25


5/3/24, 5:32 PM My First Python Codes

In [15]: a = 12.2
print ("value of a is", a)

value of a is 12.2

In [21]: type(a)

float
Out[21]:

In [24]: country = "pakistan"

In [25]: print ("country name is", country )

country name is pakistan

In [26]: type (country)

str
Out[26]:

In [27]: # Variable name can be anything, but it should not start with a number.

In [28]: name = "Inayah"

In [30]: print (name)

Inayah

In [31]: abc = "Inayah"


print (abc)

Inayah

In [32]: abc123 = "Inayah"


print (abc123)

Inayah

In [30]: name = "Inayah"

In [5]: Hani = "Inayah"


print (Hani)

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 combination)

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 3/25


5/3/24, 5:32 PM My First Python Codes

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))

Type of variable a is <class 'int'>


Type of varialbe b is <class 'float'>
Type of variable c is <class 'str'>

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

In [10]: Full_Name = First_Name + " " + Middle_Name + " " + Last_Name

print ("Full_Name is", Full_Name)

Full_Name is Haniyah Abdul Samad

In [3]: # Addition of Integers

num1 = 70
num2 = 25

sum = num1 + num2

print ("sum of", sum)

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 4/25


5/3/24, 5:32 PM My First Python Codes
sum of 95

In [4]: print ("sum of" , num1, "and", num2 , "is", sum)

sum of 70 and 25 is 95

In [6]: print (sum)

95

In [8]: print ("Sum:", sum)

Sum: 95

In [9]: print ("Sum of", "num1", "and", "num2", "is", "sum")

Sum of num1 and num2 is sum

In [15]: # Subtraction of Integers

num1 = 70
num2 = 25

Difference = num1 - num2

print ("Difference of", num1, "and", num2, "is", Difference)

Difference of 70 and 25 is 45

In [17]: print ("Difference is", Difference)

Difference is 45

In [18]: # Multiplication of Integers

num1 = 8
num2 = 7

Product = num1 * num2

print ("Product of", num1, "and", num2, "is", Product)

Product of 8 and 7 is 56

In [19]: print (Product)

56

In [20]: print ("Product is", Product)

Product is 56

In [21]: # Multiplication of Integers

num1 = 8
num2 = 7

#Product = num1 * num2

print ("Product of", num1, "and", num2, "is", num1*num2)

Product of 8 and 7 is 56

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 5/25


5/3/24, 5:32 PM My First Python Codes

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)

quotient of 50 and 5 is 10.0

In [6]: # FLOAT

num1 = 50
num2 = 4

Quotient = num1 / num2


print ("quotient of", num1, "and", num2, "is", Quotient)

quotient of 50 and 4 is 12.5

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

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 6/25


5/3/24, 5:32 PM My First Python Codes
# 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

In [14]: Bank_Balance = 2400


Withdaw_Amount = 500

if (Bank_Balance > Withdaw_Amount):


print("Transaction Successful.")
print("Remaining Balance:" , Bank_Balance - Withdaw_Amount)

Transaction Successful.
Remaining Balance: 1900

In [15]: Haniyah = 15
Musab = 8

if (Haniyah > Musab):


print ("Haniyah is senior.")

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 7/25


5/3/24, 5:32 PM My First Python Codes
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

In [17]: Bank_Balance = 20000


Withdaw_Amount = 50000

if (Bank_Balance > Withdaw_Amount):


print("Transaction Successful.")
print("Remaining Balance:" , Bank_Balance - Withdaw_Amount)
else:
print ("Insufficient Balance")

Insufficient Balance

In [18]: # Issue: Need of >= oprater

Bank_Balance = 20000
Withdaw_Amount = 20000

if (Bank_Balance > Withdaw_Amount):


print("Transaction Successful.")
print("Remaining Balance:" , Bank_Balance - Withdaw_Amount)
else:
print ("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)

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 8/25


5/3/24, 5:32 PM My First Python Codes
else:
print ("second number", num2 , "is greater than first number", num1)

second number 10 is greater than first number 10

In [20]: num1 = 100


num2 = 100

if (num1 > num2):


print ("First number", num1, "is greater than second number", num2)
elif (num2 > num1):
print ("Second number", num2, "is greater than First number", num1)
else:
print ("Both numbers are equal.")

Both numbers are equal.

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

if (Marks > 90):


print ("A* Grade.")

elif (Marks > 80):


print ("A Grade.")

elif (Marks > 70):


print ("B Grade.")

elif (Marks > 60):


print ("C Grade.")

elif (Marks > 50):


print ("D Grade.")

else:
print ("F Grade.")

F Grade.

SIMPLE CALCULATOR
In [12]: num1 = 88
operater = "*"
num2 = 50

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 9/25


5/3/24, 5:32 PM My First Python Codes

if (operater == "+"):
add = num1 + num2
print ("Sum of" , num1 , "and" , num2 , "is" , add)

elif (operater == "-"):


subt = num1 - num2
print ("Difference between", num1, "and", num2, "is", Subt)

elif (operater == "*"):


Product = num1 * num2
print ("Product of", num1, "and", num2, "is", Product)

elif (operater == "/"):


div = num1 / num2
print ("Quotient of", num1, "and", num2, "is", div)

Product of 88 and 50 is 4400

March 2nd , 2023


In [16]: # Task: Make a program for mobile password unlock.

savedPassword = "1122"

userpin = input ("Type your 4 digit pincode ")

if (savedPassword == userpin):
print ("correct PIN. screen unlocked")
else:
print ("Incorrect PIN")

Type your 4 digit pincode 1234


Incorrect PIN

In [25]: #Task: Find out either "1234" (str) == 1234 (int)

code = 1234

userInput = (input ("Enter 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)

In [23]: # Use of int() function

savedPassword = "1122"

userpin = int(input("Type your 4 digit pincode "))

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

Type your 4 digit pincode 1234


Incorrect PIN

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 = "abc@gmail.com"
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.")

Enter your email id:abc@gmail.com


Enter your password:hani123
Login Successful.

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)

elif (operater == "-"):


subt = num1 - num2
print ("Difference between", num1, "and", num2, "is", subt)

elif (operater == "*"):


Product = num1 * num2
print ("Product of", num1, "and", num2, "is", Product)

elif (operater == "/"):


div = num1 / num2
print ("Quotient of", num1, "and", num2, "is", div)

Enter 1st number:748


+ , - , * , /*
Enter 2nd number:646
Product of 748 and 646 is 483208

ATM MACHINE CODE


In [ ]: # Account Detailes
Account1_name = "Haniyah Samad"

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 11/25


5/3/24, 5:32 PM My First Python Codes
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.")
print ("Your remaining Balance is" , Account2_Balance)
else:
print ("Insufficient Balance.")
else:
print ("Invalid Balance.")

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 12/25


5/3/24, 5:32 PM My First Python Codes
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.")

Mar 16, 2024


In [ ]: # Save all siblings names.

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 , .......]

In [1]: # Save all siblings names


# Creat a list of siblings

Siblings = ["Haniyah" , "Salahuddin" , "Shaheedu" , "Zaeemu"]


print ("List of Siblings:" , Siblings)

List of Siblings: ['Haniyah', 'Salahuddin', 'Shaheedu', 'Zaeemu']

In [2]: # Creat a List of 9 random numbers

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

[29, 24, 17, 10, 1, 21, 9, 2, 13]

In [3]: Account1 = ["Haniyah" , "20081029" , "3456" , 90000]


Account2 = ["Haniyah" , "20081029" , "3456" , 90000]

In [4]: print ("Account1:" , Account1)


print ("Account2:" , Account2)

Account1: ['Haniyah', '20081029', '3456', 90000]


Account2: ['Musab', '498765', '9876', 80000]

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 ......

HOW TO ACCESS LIST ELEMENTS?


In [12]: # Name ATM PIN Balance
Account1 = ["Haniyah" , "20081029" , "3456" , 90000]
Account2 = ["Musab" , "498765" , "9876" , 80000]
# Index 0 1 2 3

In [14]: # Access list elements


# Syntax
# ListName[index no]

print ("Account Holder Nmae:" , Account1)


print ("Account Holder Nmae:" , Account2)

Account Holder Nmae: ['Haniyah', '20081029', '3456', 90000]


Account Holder Nmae: ['Musab', '498765', '9876', 80000]

In [17]: # Task:
# Print Account Holder Names with Account Balance

print ("Account1 :" , Account1[0] , ":" , Account1[3])


print ("Account2 :" , Account2[0] , ":" , Account2[3] )

Account1 : Haniyah : 90000


Account2 : Musab : 80000

MAR 17, 2024

Length of the List

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 14/25


5/3/24, 5:32 PM My First Python Codes

In [20]: # Syntax:
# Len(ListName)

Siblings = ["Haniyah" , "Salahuddin" , "Shaheedu" , "Zaeemu" , "Inayah"]


print ("Length of the list is" , len(Siblings))

Length of the list is 5

In [21]: numbs = [1,2,3,4,5,67,89,56,43]


print ("Length :" , len(Siblings))

Length : 5

Functions of List
In [18]: Siblings = ["Haniyah" , "Salahuddin" , "Shaheedu" , "Zaeemu"]

In [22]: # Siblings.append ("value") # Add a new value


# Siblings.clear () # Clear the list
# Siblings.copy () # Create a copy of list
# Siblings.count () # It will count how many times a specific value is in the list.
# Siblings.index () # Give index number of any specific value.
# Siblings.insert () # Add a new value at specific index.
# Siblings.pop () # It will delete and return the value.
# Siblings.remove () # It will remove the value
# Siblings.reverse () # Reverse the order of the list
# Siblings.sort () # Sort the list in descending order

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)

In [24]: Siblings = ["Haniyah" , "Salahuddin" , "Shaheedu" , "Zaeemu"]

# Task: Add "Ammar" In the list of siblings and print the list.

Siblings.append("Ammar")
print (Siblings)

['Haniyah', 'Salahuddin', 'Shaheedu', 'Zaeemu', 'Ammar']

In [25]: Siblings = ["Haniyah" , "Salahuddin" , "Shaheedu" , "Zaeemu"]

# Task: Add "Naimah" In the list of siblings and print the list.

Siblings.append("Naimah")
print (Siblings)

['Haniyah', 'Salahuddin', 'Shaheedu', 'Zaeemu', 'Naimah']

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 15/25


5/3/24, 5:32 PM My First Python Codes

MAR 24, 2024

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]: Siblings = ["Haniyah" , "Salahuddin" , "Shaheedu" , "Zaeemu"]

In [3]: # Task: Add "Naimah" at the start of the list

Siblings.insert(0 , "Naimah")
print ("Siblings List:" , Siblings)

Siblings List: ['Naimah', 'Haniyah', 'Salahuddin', 'Shaheedu', 'Zaeemu']

In [4]: # Task: Add "Inayah" at index 4

Siblings.insert(4 , "Inayah")
print ("Siblings list:" , Siblings)

Siblings list: ['Naimah', 'Haniyah', 'Salahuddin', 'Shaheedu', 'Inayah', 'Zaeemu']

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)

['Samreen', 'Sarah', 'Zurwah', 'Saqia', 'END']

CLEAR
In [7]: # IT will remove all the values from the list
# It will make the list empty

In [8]: print(Friends)

['Samreen', 'Sarah', 'Zurwah', 'Saqia', 'END']

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 16/25


5/3/24, 5:32 PM My First Python Codes

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 [26]: print ("Main List" , friends)


print ("Copied List" , CopyFriends)

Main List ['Hareem', 'Hala', 'Inayah', 'Hiba']


Copied List ['Hareem', 'Hala', 'Inayah', 'Hiba']

In [27]: # Task: Append a new value "Naimah" into the main list and print both lists

friends.append("Naimah")

In [29]: print ("Main List" , friends)


print ("Copied List" , CopyFriends)

Main List ['Hareem', 'Hala', 'Inayah', 'Hiba', 'Naimah']


Copied List ['Hareem', 'Hala', 'Inayah', 'Hiba']

In [30]: # Task: Append a new value "Mirha" into the copied list and print both list

CopyFriends.append("Mirha")

In [31]: print ("Main List" , friends)


print ("Copied List" , CopyFriends)

Main List ['Hareem', 'Hala', 'Inayah', 'Hiba', 'Naimah']


Copied List ['Hareem', 'Hala', 'Inayah', 'Hiba', 'Mirha']

Copy By Reference
In [32]: friends = ["Hareem" , "Hala" , "Inayah" , "Hiba"]
CopyFriends = friends

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 17/25


5/3/24, 5:32 PM My First Python Codes

In [33]: print ("Main List" , friends)


print ("Copied List" , CopyFriends)

Main List ['Hareem', 'Hala', 'Inayah', 'Hiba']


Copied List ['Hareem', 'Hala', 'Inayah', 'Hiba']

In [34]: # Task: Append a new value "Naimah" into the main list and print both lists

friends.append("Naimah")

In [35]: print ("Main List" , friends)


print ("Copied List" , CopyFriends)

Main List ['Hareem', 'Hala', 'Inayah', 'Hiba', 'Naimah']


Copied List ['Hareem', 'Hala', 'Inayah', 'Hiba', 'Naimah']

In [36]: # Task: Append a new value "Mirha" into the copied list and print both list

CopyFriends.append("Mirha")

In [37]: print ("Main List" , friends)


print ("Copied List" , CopyFriends)

Main List ['Hareem', 'Hala', 'Inayah', 'Hiba', 'Naimah', 'Mirha']


Copied List ['Hareem', 'Hala', 'Inayah', 'Hiba', 'Naimah', 'Mirha']

Count
In [ ]: # It will count, how many time any specific value occurs in the list

In [1]: friends = ["Sarah" , "Haniayh" , "Sana" , "Haniayh" , "Zara" , "Fatimah"]

In [2]: # Count how many, times "Haniyah" is in the friends list

friends.count("Haniayh")

2
Out[2]:

In [3]: # Count how many, times "Sana" is in the friends list

friends.count("Sana")

1
Out[3]:

EXTEND
In [4]: # It is use to add multiple values together in the list.

In [5]: friends = ["Hareem" , "Hala" , "Inayah" , "Hiba"]

# Task: add "Naimah" , "Mirha" , "Irha" , "Maham" in the friends list.

# friends.append("Naimah")

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 18/25


5/3/24, 5:32 PM My First Python Codes
# friends.append("Mirha")
# friends.append("Irha")
# friends.append("Maham")

friends.extend(["Naimah" , "Mirha" , "Irha" , "Maham"])

print (friends)

['Hareem', 'Hala', 'Inayah', 'Hiba', 'Naimah', 'Mirha', 'Irha', 'Maham']

In [22]: friends = ["Hareem" , "Hala" , "Inayah" , "Hiba"]

# Task: add "Naimah" , "Mirha" , "Irha" , "Maham" in the friends list.

new_friends = ["Naimah" , "Mirha" , "Irha" , "Maham"]

friends.extend(new_friends)

print (friends)

['Hareem', 'Hala', 'Inayah', 'Hiba', 'Naimah', 'Mirha', 'Irha', 'Maham']

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")

2
Out[33]:

In [34]: friends.index("Ali")

1
Out[34]:

In [35]: friends.extend(["Usman" , "Nial" , "Mikael" , "Atiq" , "Ali"])

In [36]: friends

['Usman', 'Ali', 'Usama', 'Salman', 'Usman', 'Nial', 'Mikael', 'Atiq', 'Ali']


Out[36]:

In [37]: # Find index number of "Usman"

friends.index("Usman")

0
Out[37]:

In [38]: friends.count("Usman")

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 19/25


5/3/24, 5:32 PM My First Python Codes
2
Out[38]:

In [39]: # Find the index number of 2nd occurrence of name "Usman"

friends.index("Usman" , friends.index("Usman")+1)

4
Out[39]:

In [40]: friends = ['Ali', 'Usama', 'Salman', 'Usman', 'Nial', 'Mikael', 'Atiq', 'Ali' , "Usman

In [41]: # Find the index number of 2nd occurrence of name "Usman"

friends.index("Usman" , friends.index("Usman")+1)

8
Out[41]:

In [42]: # Find index number of "Ali"

friends.index("Ali")

0
Out[42]:

In [43]: # Find the index number of 2nd occurrence of name "Ali"

friends.index("Ali" , friends.index("Ali")+1)

7
Out[43]:

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)

['Ali', 'Usama', 'Salman', 'Usman', 'Nial', 'Mikael', 'Atiq', 'Ali']


Enter a name to find out it's index number:Ali
2 Values found
Index no of first occurence is 0
Index no of 2nd occurence is 7

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 20/25


5/3/24, 5:32 PM My First Python Codes

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.

In [46]: students = ["Ibrahim" , "Abaan" , "Haniyah"]

# Task: Remove the last value and print it


# Print the list.

RemovedValue = students.pop()
print ("Removed Value is" , RemovedValue)
print ("Updated list is" , students)

Removed Value is Haniyah


Updated list is ['Ibrahim', 'Abaan']

In [48]: friends = ["Usman" , "Usama" , "Asad" , "Abeera" , "Ibrahim"]

# Task: Remove the value and print it


# Print the list

RemovedValue = friends.pop(3)
print ("Removed Value is" , RemovedValue)
print ("Updated list is" , friends)

Removed Value is Abeera


Updated list is ['Usman', 'Usama', 'Asad', 'Ibrahim']

In [50]: friends = ["Usman" , "Usama" , "Asad" , "Abeera" , "Ibrahim"]

# Task: Remove the value "Ibrahim" and print it


# Print the list

i = friends.index("Ibrahim")
RemovedValue = friends.pop(i)
print ("Removed Value is" , RemovedValue)
print ("Updated list is" , friends)

Removed Value is Ibrahim


Updated list is ['Usman', 'Usama', 'Asad', 'Abeera']

In [51]: Section_A = ["Saad" , "Sadiq" , "Burair"]


Section_B = ["Umair" , "Fatimah"]

# 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

print ("Section_A:" , Section_A)


print ("Section_B:" , Section_B)

Section_A: ['Saad', 'Sadiq']


Section_B: ['Umair', 'Fatimah', 'Burair']

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 [54]: friends = ["Usman" , "Usama" , "Asad" , "Abeera" , "Ibrahim"]


print ("Friends list:" , friends)friends =

Friends list: ['Usman', 'Usama', 'Asad', 'Abeera', 'Ibrahim']

In [55]: # Task: Remove "Asad" from the list.

friends.remove("Asad")
print ("Updated List:" , friends)

Updated List: ['Usman', 'Usama', 'Abeera', 'Ibrahim']

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)

['Usman', 'Usama', 'Asad', 'Abeera', 'Ibrahim']


Enter a name to remove from the list: Haniyah
Value not found!
Updated List: ['Usman', 'Usama', 'Asad', 'Abeera', 'Ibrahim']

REVERSE
In [59]: # It will reverse the order of the list

friends = ["Usman" , "Usama" , "Asad" , "Abeera" , "Ibrahim"]


friends.reverse()

print (friends)

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 22/25


5/3/24, 5:32 PM My First Python Codes
['Ibrahim', 'Abeera', 'Asad', 'Usama', 'Usman']

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 [64]: nums = [9,56,8,94,2,34,56,7,834,786]


nums.sort()
print ("Sorted list:" , nums)

Sorted list: [2, 7, 8, 9, 34, 56, 56, 94, 786, 834]

In [66]: # Sort the list in descending order

nums = [9,56,8,94,2,34,56,7,834,786]
nums.sort(reverse = True)
print ("Sorted list:" , nums)

Sorted list: [834, 786, 94, 56, 56, 34, 9, 8, 7, 2]

In [67]: # Sort the list in descending order

nums = [9,56,8,94,2,34,56,7,834,786]
nums.sort()
nums.reverse()
print ("Sorted list:" , nums)

Sorted list: [834, 786, 94, 56, 56, 34, 9, 8, 7, 2]

APR 21, 2024


In [ ]: # 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+" , "A+" , "O+"]

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")

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 23/25


5/3/24, 5:32 PM My First Python Codes

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 == "2"):


name_input = input("Enter a name to delete:")
if (name_input in Names):
i = Names.index(name_input)
Names.
print(name_input , ":" , Blood)
else:
print ("Name is not in the list!")

elif (option == "3"):


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.

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])

[9, 12, 15, 19, 24, 28, 31]

In [5]: # Task: Print 5th to 10th value


print(nums[4 : 10])

[12, 15, 19, 24, 28, 31]

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 24/25


5/3/24, 5:32 PM My First Python Codes

In [7]: # Task: Print 1st to 9th value


print(nums[0 : 9])

[2, 4, 7, 9, 12, 15, 19, 24, 28]

In [9]: # Task: Print 1st to 9th value


print(nums[ : 9])

[2, 4, 7, 9, 12, 15, 19, 24, 28]

In [11]: # Task: Print 3rd value to last value


print(nums[2 : ])

[7, 9, 12, 15, 19, 24, 28, 31, 39, 46, 53, 65]

In [13]: # Task: Print 3rd and last value only

print ("Frist Value" , nums[0])

print ("Last Value" , nums[-1])

Frist Value 2
Last Value 65

In [14]: #index 0 1 2 3 4..............


nums = [2 , 4 , 7 , 9 , 12 , 15 , 19 , 24 , 28 , 31 ]
#index ...................-4 -3 -2 -1

In [16]: # Print 4th Last value

print(nums[-4])

19

In [18]: # Print from the 4th to last value

print(nums[3 : -3 + 1])

[9, 12, 15, 19, 24]

In [19]: # Print last 5 values

print(nums[-5 : ])

[15, 19, 24, 28, 31]

localhost:8888/nbconvert/html/My First Python Codes.ipynb?download=false 25/25

You might also like