-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearningpy
More file actions
160 lines (104 loc) · 5.45 KB
/
learningpy
File metadata and controls
160 lines (104 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
print("Hello world")
This will just print whatever is inside the quotation when you run the program.
print("Hello")
print("I am a computer")
This, when it is run, shows that sequencing matters. It will show "hello" before "I am a computer".
To create a variable in Python you give it a name and make it equal to a value.
This is known as assignment. For example:
my_variable = "some useful data"
my_name = "Victoria"
print(my_name)
When using print you put the name of the variable between the () instead of text.
The print instruction will retrieve the value from the my_name variable and display it.
Then...
print("My name is" + my_name)
input is another command
input() uses the function input to display a prompt and capture the user’s response.
users_name = input("enter your name")
print("hello" + users_name + ", welcome")
boolean function: Used for storing values which can only ever be true or false True or False
float = Similar to integer but for non whole (decimal) numbers (1.234)
list = Used to store many items of data in an order e.g. a list a peoples names ["Martin", "Rik", "Hitesh"]
Note: the double == operator means “is this equal to?”
Opposed to a single = which follows a variable and means “make this equal to”.
1==1 ; True
! = is the operator for is not equal to,
which means this statement means is 1 not equal to 2. != is the opposite of the == operator.
1 != 2
!= is the operator for is not equal to, which means this statement means
is 1 not equal to 2. != is the opposite of the == operator.
you can use <= test if something is less than or equal to:
1 <=1
#True!
evaluation: determining the value of an expression, e.g. True or False.
selection: enabling computers to make choices.
==, !=, <, >: different ways to compare numbers in Python.
if, then, elif, else: making code run depending whether a statement is true or false.
and, or: applying logic operations to conditions.
scope: defining what code belongs to what part of the program.
indentation: adding spaces to the start of lines of code in Python so that it understand scope.
Get the middle three characters from the phrase variable.
phrase[1:4]
with phrase = "hello"
if its phrase[0], then you will have an output of "h". the order starts from zero, and presents those characters.
phrase[1:4] runs "ell"
when you do :, The element at index 4 is “o”, but “o” isn’t output.
The end_index in a range is up to, but not including.
0 1 2 3 4
h e l l o
- - - >
Challenge: Get the last two characters from the phrase variable.
Tip: If you want to get a range which is from the start or up to the end of a list, you can leave the start or end index blank. E.g.
phrase[:3] - runs "hel"
phrase[3:] - runs "lo"
-------------------------------------------------------
my_list = [1, 2, 3, 4, 5]
If you want to create an empty list, which you will append items to later in your program, you can use empty square brackets: [].
my_list = []
Note: Lists can contain all different types of data: integers, strings, and anything else. They can even contain other lists.
names = ["martin", "laura", "hitesh", "caitlyn", "renee"]
if you input this and then
names[1]
you will output "laura"
Append a new name to the end of your list of names.
names.append("lauren")
- this adds "lauren" to the end of the list
you can type "names" to make sure that its at the end of the list now.
names.insert(2, "dan")
^ this allows you to insert "dan" into where 2 should be, between laura and hitesh.
As a last step let’s remove the name “martin” from the list.
names.remove("martin")
^ this removes the name, wherever it is.
To loop through each of the items in the list, you will use a "for" loop. for loops exist in most programming languages, and their purpose is to allow code to be run a set number of times – in our case, equal to the number of things in our shopping list.
The syntax of a for loop in Python looks like this:
for element in list:
print("do this")
for #display_shopping.py
- count + 1 is evaluated first: the computer calculates the current value of count plus 1.
Next the computer sets the value of count to the result of step 1.
count is now equal to ‘whatever it was before’, plus 1.
Now let’s print out how many items are in the shopping list.
At the end of your program, not indented under the for, print the value of count.
Create a variable called count at the top of the display_shopping program and set it to zero:
- count = 0
By using range we can simplify the example program above:
for number in range(5):
print(number)
Note: - range(5) will create a list of five numbers from 0 to 4, not six numbers from 0 to 5.
while True: is the “usual” way of creating an infinite loop in Python and
although it looks a bit strange, it makes sense when you understand whats happening.
First consider this program:
keep_running = True
while keep_running == True:
input("press enter")
The variable keep_running will always be equal to True and as it is never changed to False this program will run forever and is infinite.
If keep_running is always True the same program could be written as follows replacing the keep_running variable with True:
while True == True:
input("press enter")
<<<<<<< HEAD
conditional loops: while this, do that
infinite loops: repeat this forever
abstraction: why it is useful to abstract coding into reusable pieces
functions: how to create re-usable code which can be called from your program
=======
>>>>>>> 27cdcbab36b9865e0ff52ed9222ef03f21c4180e