0% found this document useful (0 votes)
10 views4 pages

Learning Udemy

The document discusses various Python concepts like data types, functions, conditionals, loops, file handling and more with examples. It shows how to define and call functions, use conditional statements, iterate over lists and dictionaries, take user input, format strings and work with files.

Uploaded by

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

Learning Udemy

The document discusses various Python concepts like data types, functions, conditionals, loops, file handling and more with examples. It shows how to define and call functions, use conditional statements, iterate over lists and dictionaries, take user input, format strings and work with files.

Uploaded by

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

 Data Types

a = 10 #Integer
b = 'Hello' #String
c = [33, 33.5, 'Temperature'] #List
d = {'isb':32, 'rwp':36, 'gujarkhan':37} #Dictionary
e = (33, 33.5, 'Temperature') #Tuple

 Functions & Conditionals

def mean(marks):
if type(marks) == dict:
my_mean = sum(marks.values()) / len (marks)
else:
my_mean= sum (marks) / len (marks)
return my_mean

print(mean({'sam' : 2,'ahmad': 2}))

OR
def mean(marks):
if isinstance(marks, dict):
my_mean = sum(marks.values()) / len (marks)
else:
my_mean= sum (marks) / len (marks)
return my_mean

print(mean({'sam' : 2,'ahmad': 2}))

 User Inputs

def func(marks):
if marks >= 34:
return 'pass'
else:
return 'fail'

input = float( input('Enter your marks :'))


print(func(input))
 String Formating

name1 = input('Your first name :')


name2 = input('your surname :')
greeting = 'hello there %s %s !' %(name1, name2)
print(greeting)
OR
greeting2 = f'Hello again {name1} {name2} !'
print(greeting2)
OR
greeting3 = 'Hello {} {}'.format(name1,name2)
print(greeting3)

 Loops:
Lists
colors = [11, 34.1, 98.2, 43, 45.1, 54, 54]
for col in colors:
if isinstance(col,int):
print(col)
Dictionary
colors = {'red' : 11, 'yellow': 34.1, 'black' : 98.2}
for col in colors.items():
print(col)
for col in colors.keys():
print(col)
for col in colors.values():
print(col)

 Dictionary loop and string formatting

dict1 = {'John Smith' : '+37682929928' , 'Marry Simpons': '+423998200919'}


print(type(dict1))
for keys1, values1 in dict1.items():
print(f'{keys1} {values1}')
 While loops with if statement

password = ''
while password != 'dose':
password = input('Enter your password : ')
print (' You are in ')
OR
while True:
password = input('Enter your password : ')
if password == 'dose':
break
else:
continue
print('your are in ')

 First Program

def operation(temp):
interogatives = ('How','What','Why','When')
temp2 = temp.capitalize()
if temp2.startswith(interogatives):
return f'{temp2}?'
else:
return f'{temp2}.'

results = []

while True:
temp = input('Say : ')
if temp == '\end':
break
else:
results.append(operation(temp))

print(str.join(' ', results))

 List comprehension

def greater(user_list):
return [temp/2 for temp in user_list if isintance]

print(greater([-5, 3, -1, 101]))


 Processing files

with open(r'D:\Data science\random.txt', 'r') as file1:


temp =file1.read()
print(temp)
with open(r'D:\random.txt', 'a+') as file2: #a+_both operatation read & append
x = file2.write(temp[:4])
file2.seek(0) #move curser back to zero index
print(file2.read())
file2.seek(0) #move curser back to zero index
file2.write('\nboos\n')
print(file2.read())

You might also like