# How do we know how to make a list of all the numbers from 1 to 20?
# We can use a for loop!
nums = []
for i in range(1,21):
nums.append(i)
print(nums)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
# Python has some nice built-in features to do this more easily. Specifically, we can use list comprehensions.
# List comprehensions are a way to create lists in Python. They are very useful and are used often in Python code.
# Here is an example of a list comprehension that does the same thing as the for loop above.
nums = [i for i in range(1,21)]
# We can also use list comprehensions to create lists of strings.
# Advanced example: we can use the chr() function to convert an integer to a character.
# The ord() function converts a character to an integer.
print(ord('a'))
print(ord('z'))
97 122
print(chr(97))
print(chr(98))
a b
# Here is an example of a list comprehension that creates a list of the first 10 letters of the alphabet.
letters = [chr(i) for i in range(ord('a'),ord('k'))]
# We can use range as usual to change the step size.
countdown = [n for n in range(10,0,-1)]
print(countdown)
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
# We can also use if statements in list comprehensions
lst = [x for x in range(10) if x%2 == 0]
print(lst)
[0, 2, 4, 6, 8]
More examples to practice...
# Making a list containing 10 2s?
lst = []
for i in range(10):
lst.append(2)
print(lst)
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
lst = [2 for i in range(10)]
print(lst)
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
# Multiple of 2
lst = [2*x for x in range(0,10)]
lst
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
# Make a new list modifying a different list
lst2 = [l+1 for l in lst]
lst2
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
# We can do the same thing for dictionaries
powers_of_2 = {x:2**x for x in range(10)}
even_powers_of_2 = {x:2**x for x in range(10) if x%2 == 0}
# Here's a handy example: let's make a dictionary that maps from a character's code to the character itself.
code2char = {i:chr(i) for i in range(ord('a'),ord('z')+1)}
print(code2char)
{97: 'a', 98: 'b', 99: 'c', 100: 'd', 101: 'e', 102: 'f', 103: 'g', 104: 'h', 105: 'i', 106: 'j', 107: 'k', 108: 'l', 109: 'm', 110: 'n', 111: 'o', 112: 'p', 113: 'q', 114: 'r', 115: 's', 116: 't', 117: 'u', 118: 'v', 119: 'w', 120: 'x', 121: 'y', 122: 'z'}
A few other topics we should talk about before we wrap up with standard Python stuff.
# Ternary Operator
# The ternary operator is a way to write if statements in one line.
# The syntax is: <value if true> if <condition> else <value if false>
# Example of a use of ternary:
# We want to print "even" if a number is even and "odd" if a number is odd.
def even_or_odd(n):
return "even" if n % 2 == 0 else "odd"
print(even_or_odd(7))
print(even_or_odd(42))
odd even
# We can use the split() function to split a string into a list of strings.
# We pass what "delimiter" we want to split on
sentence = "This is a sentence made up of some words."
words = sentence.split(" ")
print(words)
names_string = "Aku,Abraham,Binyam,Georg,Liya"
name_list = names_string.split(",")
print(name_list)
['Aku', 'Abraham', 'Binyam', 'Georg', 'Liya']
# We can do the opposite too.
# If we have a list, we can join the list together into a single string:
print(words)
print("-".join(words))
print(".".join(words))
print("ALEX".join(words))
['This', 'is', 'a', 'sentence', 'made', 'up', 'of', 'some', 'words.'] This-is-a-sentence-made-up-of-some-words. This.is.a.sentence.made.up.of.some.words. ThisALEXisALEXaALEXsentenceALEXmadeALEXupALEXofALEXsomeALEXwords.
# Printing using f-strings (formatted strings)
# We can use f-strings to print strings with variables in them.
# We put an f before the string and then put the variable in curly braces.
name = "Alex"
print(f"Hello, my name is {name}")
greeting = "Hi"
print(f"{greeting}, my name is {name}")
Hello, my name is Alex Hi, my name is Alex
# It doesn't have to be inside of a print statement.
# We can use f-strings to create strings.
name = "Alex"
announcement = f"Hello, my name is {name}"
print(announcement)
Hello, my name is Alex
# Going to uppercase or lowercase,
sample_str = "My name is Alex"
print(sample_str)
print(sample_str.upper())
print(sample_str.lower())
# We can also use the title() function to capitalize the first letter of each word.
print(sample_str.title())
# And many many more functions.
My name is Alex MY NAME IS ALEX my name is alex My Name Is Alex
Let's look at an error together.