Lecture Python Basics
Lecture Python Basics
print (len(str1))
str2= str1[-5:-1]
print (str2)
print(str1.endswith("aan"))
• str1= "pakistan is is my homeland"
• print (str1.capitalize())
• print (str1.replace("pakistan", "lahore"))
• print (str1.find("is"))
• print (str1.count("is"))
• a = input("enter your name")
• print(a)
• print(len(a))
• print(str1.find("pa"))
If elif age = 19 light = "orange" marks = 30
if(age > 18): if(light == "green"): if(marks >=90):
else print("hi") print("run") print("Grade A")
elif(light == "red"): elif(90>marks>=80):
print("stop") print("Grade B")
g
age = 93
num= int(input("enter a
num2= int(input("enter 2nd number"))
#print (num2)
• Tup = (1,1,2,1,3,1)
• Tup.index() returns index of first occurrence
• Tup.count() counts total occurrences
movie=[]
a = input("1st movie
name")
b = input("2nd movie
name")
c = input("3rd movie
name")
print(a)
print(b)
print(c)
movie.append(a)
movie.append(b)
movie.append(c)
Palindro
me
GRADE =
list1 = [1,2,3,2,1] ("A","B","C","A","B", "A")
print(GRADE.count("A"))
#print(list1)
copy_list1=list1.copy(
)
copy_list1.reverse()
if(copy_list1 == list1):
print("palindrome")
else:
print("not
grade = ["A", "B", "C", "A", "B",
palindrome")
"A"]
grade.sort()
print(grade)
Dictionary and Set
• Dictionary and Set are data structures
• Dictionaries are used to store data values in key:value
pairs , they are unordered , mutable (changeable) &
duplicate keys are not allowed.
info =
{
"key" : "value",
"name" : "Awais",
"roll number" : 9024,
"age" : 35,
"Qualification" : "PhD Scholar",
"CGPA" : 3.79,
"research area" : ["ML", "Bioinformatics", "DL", "AI"],
"institutes" : ("UET", "GCU","PIEAS")
}
print(info)
print(info["name"])
print(info["Qualification"])
info = {
"key" : "value",
"name" : "awais",
"roll number" : 9024,
"age" : 35,
"Qualification" : "PhD Scholar",
"CGPA" : { "BS":"2.94", "MS":"3.31", "PhD":"3.79"},
"research area" : ["ML", "Bioinformatics", "DL", "AI"],
"institutes" : ("UET", "GCU","PIEAS")
}
print(info["CGPA"]["PhD"])
• Dict.keys() print all keys
• Dict.values() print all values
• Dict.items() print all key value pairs as tuples
• Dict.get(“key”) print the key according to values
• Dict.update(new dict)
#print(info.keys())
#print(info.values())
#print(info.items())
#print(info.get("institutes"))
#pairs = list(info.items())
#print(pairs[1])
#info.update({"city" : "Lahore"})
#print(info)
• Collection of distinct objects
Sets in Python
• Immutable & unique items
• Num , int , float , str , tuple
• List and dict can not be added in set
num = set()
• Set.add(element) set1 = {1,2,3}
num.add(2) set2 = {2,3,4}
• Set.remove(element) num.add(2000) print(set1.union(set2))
print(set1.intersection(se
• Set.clear() empties the num.add(20) t2))
set num.add(200)
num.remove(200
• set.pop() deleted a randon 0)
value #num.clear()
• Set.union(set2) num.pop()
num.pop()
• se,.intersection(set2) num.pop()
print(num)
dictionary = {"words" : "meaning",
"computer" : "ELECtronic machine",
"mobile" : "2 DIMENTIONAL RADIO",
"university" : "educational degree awarding
institute",
"mr. chips" : ["literature book", "12th english"]
print(dictionary)
Practice : enter 3 subject marks by
the user and print these in a
dictionry
physics = input("enter marks of first
subject")
print([physics])
num = 1
def factorial(n):
return 1 if n == 0 else n * factorial(n - 1)
def is_prime(n):
return n > 1 and all(n % i for i in range(2,
int(n**0.5) + 1))
def fibonacci(n):
a, b = 0, 1
for _ in range(n): a, b = b, a + b
return a
def reverse_string(s):
return s[::-1] from math import gcd
def find_gcd(a, b):
return gcd(a, b)
def find_max(lst):
return max(lst) def is_sorted(lst):
return lst == sorted(lst)
def is_palindrome(s):
return s == s[::-1]
def
remove_duplicates(lst):
return list(set(lst))