1
+ '''
2
+ This file includes practice code that should refresh
3
+ your memory on basic Python concepts. If anything here
4
+ looks totally out of wack, ask an instructor for help!
5
+
6
+ Remove the triple quotes around each code block to run it
7
+ '''
8
+ # ~~ Variables and I/O ~~
9
+ '''
10
+ x = 9
11
+ x += 2 # x is now 11
12
+ print(x)
13
+
14
+ a = "strings can be"
15
+ b = " added "
16
+ c = "like this!"
17
+ print(a + b + c)
18
+
19
+ # Get input from the user, click into the terminal to respond
20
+ name = input("Hi there, what is your name? ")
21
+ print("Thanks for coming, " + name + "!")
22
+ '''
23
+
24
+ # ~~ If Statements and Logic ~~
25
+ '''
26
+ a = True
27
+ b = False
28
+
29
+ # Not 'flips' a boolean variable
30
+ print("a and b flipped are:")
31
+ print('a: ' + str(not a))
32
+ print('b: ' + str(not b))
33
+
34
+ # An if statement can have two branching paths
35
+ # Don't forget the colons!
36
+ if a:
37
+ print("a is true")
38
+ else:
39
+ print("a is false")
40
+
41
+ # Add more outcomes with elif
42
+ if a:
43
+ print("a is still true")
44
+ elif b:
45
+ print("a is false but b is true")
46
+ else:
47
+ print("nothing is true")
48
+
49
+ # and, not, and or make if statements more complex
50
+ if a or b:
51
+ print("One of a and b are true")
52
+ if a and b:
53
+ print("Both a and b are true")
54
+ if not (a or b):
55
+ print("Neither a nor b are true")
56
+ '''
57
+
58
+ # ~~ Loops ~~
59
+ '''
60
+ # There are two flavors of loops: for and while
61
+
62
+ # For loops execute a set number of times
63
+ print("Numbers from 1-10 (for loop)")
64
+ for i in range(11):
65
+ print(i, end=" ")
66
+ print()
67
+ # While loops execute as long as a condition is true
68
+ i = 0
69
+ print("Numbers from 1-10 (while loop)")
70
+ while i <= 10:
71
+ print(i, end=" ")
72
+ i += 1 # What if this was multiplication by 2?
73
+ print()
74
+ # A more complex while loop
75
+ # 'break' tells Python to end the loop prematurely
76
+ print("Some more numbers")
77
+ i = 1
78
+ while i < 6:
79
+ print(i, end=" ")
80
+ if i == 3:
81
+ break
82
+ i += 1
83
+
84
+ print()
85
+ '''
86
+
87
+ # ~~ Functions ~~
88
+ '''
89
+ # Functions collect code with a common purpose in one block
90
+ def star_staircase(steps):
91
+ """
92
+ I'm a comment that says what the function does
93
+
94
+ It makes a stair case of stars, like so:
95
+ star_staircase(3) ->
96
+ *
97
+ **
98
+ ***
99
+ """
100
+
101
+ for i in range(steps):
102
+ print(" " * (steps - i - 1), end="")
103
+ print("*" * (i+1))
104
+
105
+ star_staircase(3)
106
+ star_staircase(6)
107
+
108
+ # star_staircase doesn't return a value, but this one does
109
+ def pig_latin(string):
110
+ """
111
+ Translates string to pig latin under the following rules:
112
+ - If first letter is a vowel, simply add 'ay'
113
+ - If first letter is consonant, put it at the end of the word and add
114
+ 'ay'
115
+ """
116
+ vowels = 'aeiou'
117
+ ay = 'ay'
118
+ if string[0].lower() not in vowels:
119
+ string += string[0]
120
+ string = string[1:]
121
+
122
+ return string.lower().capitalize() + ay
123
+
124
+ print(pig_latin("Hello"))
125
+ print(pig_latin("Please"))
126
+ '''
0 commit comments