Stored Programs: Mastering Cyberspace: An Introduction To Practical Computing
Stored Programs: Mastering Cyberspace: An Introduction To Practical Computing
Mastering Cyberspace:
An Introduction to Practical Computing
1.
2.
3.
4.
5.
10/7/14
count = 15
DollarsPerLitre1 = 2.16
Number_of_Geese = 7 * 2 + 3
10/7/14
10/7/14
height = 10
width = 20
height
width
area = height * width
area
age = 1
age
age = age + 1
age
age = age + 1
age
10/7/14
Exercise on Variables
Exercise on Variables
>>> a = 2
>>> b = 7
>>> a
>>> a
>>> b
>>> a
2
>>> c
>>> c
15
>>> c
>>> c
15.0
>>> d
>>> c
24.0
>>> c = a * b + 1
>>> c
>>> c = float(c)
>>> c
>>> d = a + b
>>> c + d
10/7/14
10/7/14
= 2
= 7
= a * b + 1
= float(c)
= a + b
+ d
10
would print "Enter your name: ", wait for a user to type
something and hit enter, then place the result in name.
10/7/14
11
10/7/14
12
Input Exercise
Input Exercise
x is
x is
x is
x is
x is
x is
13
10/7/14
x is 15
x is '15.0'
x is 15.0
x is 15.0
14
x is '15'
Printing Exercise
10/7/14
x is 'Some words'
15
print(1 + 2 + 3)
print("1" + "2" + "3")
print([1, 2] * 2)
print(1 + 3 // 2)
x = 20
print(x % 7)
print(x // 7)
10/7/14
16
Printing Exercise
Commenting Programs
This is
a program that has
3 lines.
1 2 3 4
1,2,3,4
1234 1 2
1 2 3 4
print(1 + 2 + 3)
print("1" + "2" + "3")
print([1, 2] * 2)
print(1 + 3 // 2)
x = 20
print(x % 7)
print(x // 7)
6
123
[1, 2, 1, 2]
2
10/7/14
6
2
10/7/14
18
10/7/14
10/7/14
20
Combined Exercise
Exercise
10/7/14
21
10/7/14
22
23
10/7/14
24
if <condition>:
statements to execute if condition is True
else:
statements to execute if condition is False
Meaning
This includes colons after both the condition and the else.
Less than
Operator
Examples
<
0 < x, a < b
<=
>
x > 0, a > b
y >= 10, a >= b
>=
Equal to
==
z = "here", a == b
Not equal to
!=
z != [1, 2], a != b
10/7/14
Greater than
25
10/7/14
Write a program that asks the user to enter his or her age,
and then prints out a ticket based on that age:
Child tickets (below the age of 12) cost $5.00
Adult tickets (age 12 or above) cost $9.99
Tickets for children should look like the display on the left,
while those for adults should look like that on the right.
************
Child Ticket
Price: $5.00
************
26
Conditional Exercise
10/7/14
27
10/7/14
*************
Adult Ticket
Price: $9.99
*************
28
Conditional Exercise
Conditional Exercise
Write a program that asks the user to enter his or her age,
and then prints out a ticket based on that age:
Write a program that asks the user to enter his or her age,
and then prints out a ticket based on that age:
Tickets for children should look like the display on the left,
while those for adults should look like that on the right.
************
Child Ticket
Price: $5.00
************
10/7/14
Tickets for children should look like the display on the left,
while those for adults should look like that on the right.
age = int(input("What is your age? "))
print("************")
************
*************
if age < 12:
Child Ticket
Adult Ticket
print("Child
Ticket")
print("Price:
$5.00")
Price: $5.00
Price: $9.99
else:
************
*************
print("Adult Ticket")
print("Price: $9.99")
print("************")
*************
Adult Ticket
Price: $9.99
*************
29
10/7/14
Operator
Example
Logical AND
and
or
z == "here" or z == "there"
not
Logical OR
Logical NOT
30
31
10/7/14
32
10/7/14
34