-
Input data from keyboard
# Input numerical data from keyboard
count = 0
total = 0
markslist = list()
while count<= 3:
newmark = input("Input mark out od 10 (" + str(count) + ") ") # Inut data from keyboard
markslist.append(newmark)
total = total + newmark
count = count + 1
print "count = ", count
print "total = ", total
print "Last newmark = ", newmark
print "markslist = ", markslist
# Input string from keyboard
Val = raw_input("Write your name: ")
print Val
-
if... elif... else
if comparison statement:
instructions
ifif comparison statement:
instructions
else:
instructions
example
a=raw_input ("Come ti chiami ? ")
if a == "Enrico":
print "Ciao", a
else:
print "Non ti conosco", a
example
a=input ("Password ? ")
if a == 1234:
print "PW OK", a
else:
print "PW FAIL", a
example
mood = "terrible"
speed = 110
if speed >= 80:
print 'License and registration please'
if mood == 'terrible' or speed >= 100:
print 'You have the right to remain silent.'
elif mood == 'bad' or speed >= 90:
print "I'm going to have to write you a ticket."
write_ticket()
else:
print "Let's try to keep it under 80 ok?"
UP
-
FOR and IN
Python's for and in constructs are extremely useful, and the first use of them we'll see is with lists.
The for construct:
for var in list
is an easy way to look at each element in a list (or other collection).
example
#!/usr/bin/python
squares = [1, 4, 9, 16]
sum = 0
for num in squares:
sum += num
print sum
example
#!/usr/bin/python
list = ['mamma', 'padre', 'cane']
if 'mamma' in list:
print 'Here is ' + list[0]
range
If you do need to iterate over a sequence of numbers, the built-in function range() comes in handy.
example
#!/usr/bin/python
## print the numbers from 0 through 9
for i in range(10):
print i
pi@rpi1 ~/PY $ python prova.py
0
1
2
3
4
5
6
7
8
9
pi@rpi1 ~/PY $
example
#!/usr/bin/python
a = ['Mary', 'had', 'a', 'little', 'lamb']
for i in range(len(a)):
print i, a[i]
pi@rpi1 ~/PY $ python prova.py
0 Mary
1 had
2 a
3 little
4 lamb
pi@rpi1 ~/PY $
while
A
while loop statement in Python programming language repeatedly executes
a target statement as long as a given condition is true.
while expression:
statement(s)
example
#!/usr/bin/python
count = 0
while (count < 9):
print 'The count is:', count
count = count + 1
print "Goodbye!"
UP
-
LISTs
Lists are very similar to arrays.
They can contain any type of variable, and they can contain as many variables as you wish.
List literals are written within square brackets [ ].
Lists work similarly to strings -- use the len() function and square
brackets [ ] to access data, with the first element at index 0.
Suppose that:
colors = ['green', 'white', 'red']
print colors[0] ## green
print colors[2] ## white
print len(colors) ## red
colors ----> green white red
index -----> 0 1 2
Is not possible copy a list in a string.
string = colors <---- is not possible.
example
mylist = []
mylist.append(10)
mylist.append(20)
mylist.append(30)
print(mylist[0]) # prints 10
print(mylist[1]) # prints 20
print(mylist[2]) # prints 30
# prints out 10,20,30
for x in mylist:
print x
List Methods
list.append(x)
Add an item to the end of the list; equivalent to a[len(a):] = [x].
list.extend(L)
Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.
list.insert(i, x)
Insert an item at a given
position. The first argument is the index of the element before which
to insert, so a.insert(0, x) inserts at the front of the list, and
a.insert(len(a), x) is equivalent to a.append(x).
list.remove(x)
Remove the first item from the list whose value is x. It is an error if there is no such item.
list.pop([i])
Remove the item at the
given position in the list, and return it. If no index is specified,
a.pop() removes and returns the last item in the list. (The square
brackets around the i in the method signature denote that the parameter
is optional, not that you should type square brackets at that position.
You will see this notation frequently in the Python Library Reference.)
list.index(x)
Return the index in the list of the first item whose value is x. It is an error if there is no such item.
list.count(x)
Return the number of times x appears in the list.
list.sort()
Sort the items of the list, in place.
list.reverse()
Reverse the elements of the list, in place.
-
Functions
# Define a function
def vistxt(testo):
print "Received: ", testo
# Call function vistxt
vistxt("Enrico")