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

Intro To Control Statements and Functions

The document introduces Python programming concepts like conditional statements, loops, functions, lists and generators. It shows examples of using if/else statements, for loops, range generator, list operations and defines functions. It also includes an exercise asking the user to write a code that collects ratings from 10 users on exam difficulty and calculates the average rating.

Uploaded by

api-296946581
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Intro To Control Statements and Functions

The document introduces Python programming concepts like conditional statements, loops, functions, lists and generators. It shows examples of using if/else statements, for loops, range generator, list operations and defines functions. It also includes an exercise asking the user to write a code that collects ratings from 10 users on exam difficulty and calculates the average rating.

Uploaded by

api-296946581
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

In [70]:

#if
x = int(raw_input("Please enter your age: "))
Please enter your age: 52
In [71]:
if x<0:
x = 0
print 'sanggol ka pa lang'
elif x ==0:
print 'you were just born'
elif x> 60:
print 'malapit ka na!'
else:
print 'Malaki ka na Jeff!'
Malaki ka na Jeff!
In [74]:
#for Statements
'Measure the length of some strings'
# a list of word
actions = ['hug','walk','run', 'sing', 'eat']
for w in actions:
#print w, len(w)
print w, len (w)
hug 3
walk 4
run 3
sing 4
eat 3
In [9]:
Presidential_candidates = ['Duterte', 'Roxas', 'Binay', 'Poe']
for i in Presidential_candidates:
print i
Duterte
Roxas
Binay
Poe

In [11]:
#list generator
range(5)
Out[11]:
[0, 1, 2, 3, 4]
In [12]:
range(5,10)
Out[12]:
[5, 6, 7, 8, 9]
In [78]:
#A list of numbers
range(-10,10,2)
Out[78]:
[-10, -8, -6, -4, -2, 0, 2, 4, 6, 8]
In [88]:
a = ['Mary', 'had', 'a', 'little', 'lamb']
print a[0]
print a[4]
#for i in range(len(a)):
#print i, a[i]
#for i in range (0, 5):
#print a[i]

a.append('Named Joseph')
print a
Mary
lamb
['Mary', 'had', 'a', 'little', 'lamb', 'Named Joseph']

In [91]:
#break and continue
for num in range(1,1001):
if num %2 == 0: #divisibility
print "Found an even number", num
continue
#print "Found a number", num
Found
Found
Found
Found
Found
Found
Found
Found
Found
Found
Found
Found
Found
Found
Found
Found
Found
Found
Found
Found

an
an
an
an
an
an
an
an
an
an
an
an
an
an
an
an
an
an
an
an

even
even
even
even
even
even
even
even
even
even
even
even
even
even
even
even
even
even
even
even

number
number
number
number
number
number
number
number
number
number
number
number
number
number
number
number
number
number
number
number

2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40

In [18]:
#Functions
def make_incrementor(n):
return lambda x: x + n
In [19]:
f = make_incrementor(42)
f(0)
Out[19]:
42

In [21]:
f(1)
Out[21]:
43
In [30]:
def f(a, L=[]):
L.append(a)
return L
print f(1)
print f(2)
print f(3)
[1]
[1, 2]
[1, 2, 3]
In [ ]:
#NOW THAT YOU'VE BEEN INTRODUCED TO THESE
#HERE IS AN EXERCISE
#ALSO AN EXERCISE FOR THE ART OF TROUBLESHOOTING

'Write a code where you will ask 10 users if the exam is easy.'
'Make them enter their response. Tell them that their response is invalid if not in r
'The responses range from 1-5 (1-very easy, 2-easy, 3- difficult, 4- very difficult)'
'Get the average rating.'
In [ ]:

In [ ]:

### response = []
n = 10
j = 0
sum_resp = 0
for i in range(0,n):
if j <10:
response_rating = int(raw_input("How would you rate the exam?: "))
if response_rating < 1:
response_rating = int(raw_input("Sorry. Invalid Response. Pls. give a rat
elif response_rating > 4:
response_rating = int(raw_input("Invalid Response. Pls. give a rating fro
else:
response.append(response_rating)
j = j + 1
sum_resp = sum_resp + response[j]
average = sum_resp/j
print "Average", average
elif j>10:
print "Average Score =", average
break
In [ ]:

You might also like