0% found this document useful (0 votes)
29 views

Python Exam

The document shows Python code examples demonstrating basic concepts like variables, conditionals, loops, lists, dictionaries, functions and more. It includes examples of printing values, iterating through lists and dictionaries, using built-in functions like append(), enumerate(), capitalize() and upper(), and defining simple functions.

Uploaded by

Shrutii Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Python Exam

The document shows Python code examples demonstrating basic concepts like variables, conditionals, loops, lists, dictionaries, functions and more. It includes examples of printing values, iterating through lists and dictionaries, using built-in functions like append(), enumerate(), capitalize() and upper(), and defining simple functions.

Uploaded by

Shrutii Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

 z=2+2

print(z)

 sentence = '''this is a
sentence which goes
into multiple lines'''
print(sentence)

 print(sentence.capitalize())

 print(sentence.upper())

 score = 91
if score >= 90 and score <= 100:
grade = 'A'
else:

grade = 'B'

print(grade)

 score = 89

if score >= 90:

grade = 'A'
elif score >= 80:

grade = 'B'

elif score >= 70:


grade = 'C'
elif score >= 60:
grade = 'D'
else:
grade = 'F'
print(grade)

 #SEQUENCES: List, Tuple, set, Dictionary: L&T, S&D


#List - mutable
#to create: Literal syntax :[]
names =['Ramesh', 'Suresh']
type(names)

 dir(names)
# It gives us what operations can be done on the data.

 names.append('Dinesh')
print(names)
#To add names we use append

#set - Set operations: Union (|), intersection (&), difference (-), Xor(^)
digits = [0, 0, 1, 1, 2, 3, 4, 5]
type(digits)

#Iterations
#for loop
for x in [1,2,3,4]:
print(x)

 animals = ['cat','dog','bird']
print(animals)

 #List - Index and value


for index, value in enumerate (animals):
print(index, value)
result-
0 cat
1 dog
2 bird
 #To stop the process of looping - to break the loop - break
numbers = [3,5,9,-1,3,1]
result = 0
for item in numbers:
if item < 0:
break
result = result + item
print(result)

3
8
17
 numbers = [3,5,9,-1,3,1]
result = 0
for item in numbers:
if item >= 9:
continue
result = result + item
print(result)

3
8
7
10
11
 print(ages)

ages.get('Suresh')

for name in ages:


print(name)

for key,value in ages.items ():


print(key,value)

 def add_2 (x):


result = x+2
return (result)

add_2(100)

 def add_2_num(x,y):
result = x+y
return (result)

You might also like