1
- # Model code for lesson one of the Python workshop.
2
- # I'll follow the structure of the workshop!
3
- # So I'll separate each lesson by
4
-
5
- ######################################################
6
-
7
- # Exercise 1 -- Variables
8
-
9
- a = 15 + 5 # 20
10
- b = 20 - a # 0
11
- c = 15 / 5 # 3
12
- d = 10 + c # 13
13
- total = a + b + c + d
14
- print (total ) # Should be 36
15
-
16
- print (a ) # Proving that variables are remembered by Python
17
-
18
- c = 35 / 5 # Increase c by 4
19
- total = a + b + c + d # Redefine total
20
- # Note that, even though d = 10 + c, we already defined d = 13 using c = 3.
21
- # Variables, once assigned, retain that value until changed. This is not algebra!
22
-
23
- print (total ) # Should now be 40
24
-
25
- # Or, alternatively...
26
- total = 40
27
- print ('Total is now' , total )
28
-
29
- ########################################################
30
-
31
- # Variables are cool, but I want to write a program!
32
-
33
- welcome = "Hello, world!" # Assign a string to the variable 'welcome'
34
- print (welcome ) # Print it out!
35
-
36
- ########################################################
37
-
38
- # Exercise 2 -- Customise your program
39
-
40
- name = input ("Hi! What's your name? " )
41
- fav_colour = input ("What's your favourite colour? " )
42
- print ("That's awesome " + name + "! My favourite colour is " + fav_colour + " too!" )
43
-
44
- ########################################################
45
-
46
- # Import Random
47
-
48
- print ("Two demonstration random values:" ) # For when the program is run
49
-
50
- import random # In your own programs, it's best practise to put all imports at the top
51
-
52
- value = random .randint (1 ,6 )
53
- print (value )
54
-
55
- value = random .randint (10 , 20 ) # Reassigning 'value' to be between 10 and 20
56
- print (value )
57
-
58
- ########################################################
59
-
60
- # Exercise 3 -- Roll the dice
61
-
62
- first_sides = int (input ("How many sides do you want on the first die? " ))
63
- roll_1 = random .randint (1 , first_sides )
64
-
65
- second_sides = int (input ("How many sides do you want on the second die? " ))
66
- roll_2 = random .randint (1 , second_sides )
67
-
68
- third_sides = int (input ("How many sides do you want on the third die? " ))
69
- roll_3 = random .randint (1 , third_sides )
70
-
71
- roll_total = roll_1 + roll_2 + roll_3
72
- print ("Your rolls were: " + str (roll_1 ) + "\n " + str (roll_2 ) + "\n " + str (roll_3 ))
73
- # \n is a line break, it puts the following text on another line
74
- print ("And your total is... " + str (roll_total ))
75
-
76
- ########################################################
77
-
78
- # Exercise 4 -- True or false?
79
-
80
- a = 5
81
- b = 10
82
- print (a > b ) # False
83
- print (a < b ) # True
84
- print (a + b == b + a ) # True
85
- print (a - b == b - a ) # False
86
- print (a * b == b * a ) # True
87
- print (a / b != b / a ) # True
88
-
89
- # Alternatively, print all of them in one statement!
1
+ # Model code for lesson one of the Python workshop.
2
+ # I'll follow the structure of the workshop!
3
+ # So I'll separate each lesson by
4
+
5
+ ######################################################
6
+
7
+ # Exercise 1 -- Variables
8
+
9
+ a = 15 + 5 # 20
10
+ b = 20 - a # 0
11
+ c = 15 / 5 # 3
12
+ d = 10 + c # 13
13
+ total = a + b + c + d
14
+ print (total ) # Should be 36
15
+
16
+ print (a ) # Proving that variables are remembered by Python
17
+
18
+ c = 35 / 5 # Increase c by 4
19
+ total = a + b + c + d # Redefine total
20
+ # Note that, even though d = 10 + c, we already defined d = 13 using c = 3.
21
+ # Variables, once assigned, retain that value until changed. This is not algebra!
22
+
23
+ print (total ) # Should now be 40
24
+
25
+ # Or, alternatively...
26
+ total = 40
27
+ print ('Total is now' , total )
28
+
29
+ ########################################################
30
+
31
+ # Variables are cool, but I want to write a program!
32
+
33
+ welcome = "Hello, world!" # Assign a string to the variable 'welcome'
34
+ print (welcome ) # Print it out!
35
+
36
+ ########################################################
37
+
38
+ # Exercise 2 -- Customise your program
39
+
40
+ name = input ("Hi! What's your name? " )
41
+ fav_colour = input ("What's your favourite colour? " )
42
+ print ("That's awesome " + name + "! My favourite colour is " + fav_colour + " too!" )
43
+
44
+ ########################################################
45
+
46
+ # Import Random
47
+
48
+ print ("Two demonstration random values:" ) # For when the program is run
49
+
50
+ import random # In your own programs, it's best practise to put all imports at the top
51
+
52
+ value = random .randint (1 ,6 )
53
+ print (value )
54
+
55
+ value = random .randint (10 , 20 ) # Reassigning 'value' to be between 10 and 20
56
+ print (value )
57
+
58
+ ########################################################
59
+
60
+ # Exercise 3 -- Roll the dice
61
+
62
+ first_sides = int (input ("How many sides do you want on the first die? " ))
63
+ roll_1 = random .randint (1 , first_sides )
64
+
65
+ second_sides = int (input ("How many sides do you want on the second die? " ))
66
+ roll_2 = random .randint (1 , second_sides )
67
+
68
+ third_sides = int (input ("How many sides do you want on the third die? " ))
69
+ roll_3 = random .randint (1 , third_sides )
70
+
71
+ roll_total = roll_1 + roll_2 + roll_3
72
+ print ("Your rolls were: " + str (roll_1 ) + "\n " + str (roll_2 ) + "\n " + str (roll_3 ))
73
+ # \n is a line break, it puts the following text on another line
74
+ print ("And your total is... " + str (roll_total ))
75
+
76
+ ########################################################
77
+
78
+ # Exercise 4 -- True or false?
79
+
80
+ a = 5
81
+ b = 10
82
+ print (a > b ) # False
83
+ print (a < b ) # True
84
+ print (a + b == b + a ) # True
85
+ print (a - b == b - a ) # False
86
+ print (a * b == b * a ) # True
87
+ print (a / b != b / a ) # True
88
+
89
+ # Alternatively, print all of them in one statement!
90
90
print (a > b , a < b , a + b == b + a , a - b == b - a , a * b == b * a , a / b != b / a )
0 commit comments