-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLists-and-Loops.py
More file actions
44 lines (32 loc) · 880 Bytes
/
Lists-and-Loops.py
File metadata and controls
44 lines (32 loc) · 880 Bytes
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
# Learning about lists
# list is a form of array
# array is a collection of things
letters = ["a", "b", "c", "d", "e"]
numbers = [1, 2, 3, 4, 5]
print(letters)
print(numbers)
# Retrieve item in list using indexing
# index = position
# Counting starts from 0 in python
# 0 1 2 3 4
# ["a", "b", "c", "d", "e"]
print(letters[3])
# What is I want to have ["b", "c"]
# Use slicing
# my_list[start_index : end_index + 1]
print(letters[1 : 3])
# Start from the start (slicing)
print(letters[:3])
# End at the end (slicing)
print(letters[1:])
# Loops
# ["a", "b", "c", "d", "e"]
# Use a for loop to loop through arrays
for placeholder in letters:
print(placeholder)
# while some_condition:
# run this code until some_condition is not valid
age = 12
while age < 18:
print("You cannot drink")
age += 1 # += means increment variable by some value