Python Basics 0#

  • In this notebook we will go over some of the basics of Python.

  • **After this workshop, we recommend that you work through Python Basics 1-5, which will go over these concepts, (as well as others) in more detail, with some hands on acitivites for practice. **

Expressions and Operators#

The simplest form of Python programming is an expression using an operator. An expression is a simple mathematical statement like:

1 + 1

The operator in this case is +, sometimes called “plus” or “addition”. Try this operation in the code box below. Remember to click the “Run” button or press Ctrl + Enter (Windows) or shift + return (OS X) on your keyboard to run the code.

# Type the expression in this code block. Then run it.

Python can handle a large variety of expressions. Let’s try subtraction in the next code cell.

# Type an expression that uses subtraction in this cell. Then run it.

We can also do multiplication (*) and division (/). While you may have used an “×” to represent multiplication in grade school, Python uses an asterisk (*). In Python,

2 × 2

is written as

2 * 2

Try a multiplication and a division in the next code cell.

# Try a multiplication in this cell. Then try a division.
# What happens if you combine them? What if you combine them with addition and/or subtraction?

When you run, or evaluate, an expression in Python, the order of operations is followed. This means that expressions are evaluated in this order:

  1. Parentheses

  2. Exponents

  3. Multiplication and Division (from left to right)

  4. Addition and Subtraction (from left to right)

Python can evaluate parentheses and exponents, as well as a number of additional operators you may not have learned in grade school.

# Try operations in this code cell.
# What happens when you add in parentheses?

Data Types (Integers, Floats, and Strings)#

All expressions evaluate to a single value. In the above examples, our expressions evaluated to single numerical value. Numerical values come in two basic forms:

  • integer

  • float (or floating-point number)

An integer, what we sometimes call a “whole number”, is a number without a decimal point that can be positive or negative. When a value uses a decimal, it is called a float or floating-point number. Two numbers that are mathematically equivalent could be in two different data types. For example, mathematically 5 is equal to 5.0, yet the former is an integerwhile the latter is a float.

Of course, Python can also help us manipulate text. A snippet of text in Python is called a string. A string can be written with single or double quotes. A string can use letters, spaces, line breaks, and numbers. So 5 is an integer, 5.0 is a float, but ‘5’ and ‘5.0’ are strings. A string can also be blank, such as ‘’.

Familiar Name

Programming name

Examples

Whole number

integer

-3, 0, 2, 534

Decimal

float

6.3, -19.23, 5.0, 0.01

Text

string

‘Hello world’, ‘1700 butterflies’, ‘’, ‘1823’

The distinction between each of these data types may seem unimportant, but Python treats each one differently. For example, we can ask Python whether an integer is equal to a float, but we cannot ask whether a string is equal to an integer or a float.

To evaluate whether two values are equal, we can use two equals signs between them. The expression will evaluate to either True or False.

# Run this code cell to determine whether the values are equal
42 == 42.0
True
# Run this code cell to compare an integer with a string
15 == 'fifteen'
False
# Run this code cell to compare an integer with a string
15 == '15'
False

When we use the addition operator on integers or floats, they are added to create a sum. When we use the addition operator on strings, they are combined into a single, longer string. This is called concatenation.

# Combine the strings 'Hello' and 'World'
'hello' + 'world'
'helloworld'

Notice that the strings are combined exactly as they are written. There is no space between the strings. If we want to include a space, we need to add the space to the end of ‘Hello’ or the beginning of ‘World’. We can also concatenate multiple strings.

# Combine three strings

When we use addition operator, the values must be all numbers or all strings. Combining them will create an error.

# Try adding a string to an integer
'55' + 23
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[10], line 2
      1 # Try adding a string to an integer
----> 2 '55' + 23

TypeError: can only concatenate str (not "int") to str

Here, we receive the error can only concatenate str (not "int") to str. Python assumes we would like to join two strings together, but it does not know how to join a string to an integer. Put another way, Python is unsure if we want:

‘55’ + 23

to become

‘5523’

or

78

We can multiply a string by an integer. The result is simply the string repeated the appropriate number of times.

# Multiply a string by an integer

Variables#

A variable is like a container that stores information. There are many kinds of information that can be stored in a variable, including the data types we have already discussed (integers, floats, and string). We create (or initialize) a variable with an assignment statement. The assignment statement gives the variable an initial value.

# Initialize an integer variable and add 22 
new_integer_variable = 5
new_integer_variable + 22

The value of a variable can be overwritten with a new value.

# Overwrite the value of my_favorite_number when the commented out line of code is executed. 
# Remove the # in the line "#my_favorite_number = 9" to turn the line into executable code.

my_favorite_number = 7
my_favorite_number = 9
my_favorite_number
# Overwriting the value of a variable using its original value
cats_in_house = 1
cats_in_house = cats_in_house + 2
cats_in_house
# A shorthand version
cats_in_house += 2
cats_in_house
# Initialize a string variable and concatenate another string
new_string_variable = 'Hello '
new_string_variable + 'World!'

You can create a variable with almost any name, but there are a few guidelines that are recommended.

Variable Names Should be Descriptive#

If we create a variable that stores the day of the month, it is helpful to give it a name that makes the value stored inside it clear like day_of_month. From a logical perspective, we could call the variable almost anything (hotdog, rabbit, flat_tire). As long as we are consistent, the code will execute the same. When it comes time to read, modify, and understand the code, however, it will be confusing to you and others. Consider this simple program that lets us change the days variable to compute the number of seconds in that many days.

# Compute the number of seconds in 3 days
days = 3
hours_in_day = 24
minutes_in_hour = 60
seconds_in_minute = 60

days * hours_in_day * minutes_in_hour * seconds_in_minute

We could write a program that is logically the same, but uses confusing variable names.

hotdogs = 60
sasquatch = 24
example = 3
answer = 60

answer * sasquatch * example * hotdogs

This code gives us the same answer as the first example, but it is confusing. Not only does this code use variable names that are confusing, it also does not include any comments to explain what the code does. It is not clear that we would change example to set a different number of days. It is not even clear what the purpose of the code is. As code gets longer and more complex, having clear variable names and explanatory comments is very important.

Variable Naming Rules#

In addition to being descriptive, variable names must follow 3 basic rules:

  1. Must be one word (no spaces allowed)

  2. Only letters, numbers and the underscore character (_)

  3. Cannot begin with a number

# Which of these variable names are acceptable? 
# Comment out the variables that are not allowed in Python and run this cell to check if the variable assignment works. 
# If you get an error, the variable name is not allowed in Python.

$variable = 1
a variable = 2
a_variable = 3
4variable = 4
variable5 = 5
variable-6 = 6
variAble = 7
Avariable = 8

Variable Naming Style Guidelines#

The three rules above describe absolute rules of Python variable naming. If you break those rules, your code will create an error and fail to execute properly. There are also style guidelines that, while they won’t break your code, are generally advised for making your code readable and understandable. These style guidelines are written in the Python Enhancement Proposals (PEP) Style Guide.

The current version of the style guide advises that variable names should be written:

lowercase, with words separated by underscores as necessary to improve readability.

If you have written code before, you may be familiar with other styles, but these notebooks will attempt to follow the PEP guidelines for style. Ultimately, the most important thing is that your variable names are consistent so that someone who reads your code can follow what it is doing. As your code becomes more complicated, writing detailed comments with # will also become more important.

The print() and input() Functions#

Many different kinds of programs often need to do very similar operations. Instead of writing the same code over again, you can use a function. Essentially, a function is a small snippet of code that can be quickly referenced. There are three kinds of functions:

  • Native functions built into Python

  • Functions others have written that you can import

  • Functions you write yourself

For now, let’s look at a few of the native functions. One of the most common functions used in Python is the print() function which simply prints a string.

# A print function that prints: Hello World!
print('Hello World!')

We could also define a variable with our string 'Hello World!' and then pass that variable into the print() function. It is common for functions to take an input, called an argument, that is placed inside the parentheses ().

# Define a string and then print it
our_string = 'Hello World!'
print(our_string)

There is also an input() function for taking user input.

# A program to greet the user by name
print('Hi. What is your name?') # Ask the user for their name
user_name = input() # Take the user's input and put it into the variable user_name
print('Pleased to meet you, ' + user_name) # Print a greeting with the user's name

We defined a string variable user_name to hold the user’s input. We then called the print() function to print the concatenation of ‘Pleased to meet you, ‘ and the user’s input that was captured in the variable user_name. Remember that we can use a + to concatenate, meaning join these strings together.

Here are couple more tricks we can use. You can pass a string variable into the input function for a prompt and you can use an f string to add the variable into the print string without use the + operator to concatenate both strings.

# A program to greet the user by name
# Passing the prompt into the input() function
# prints it automatically before taking the input
user_name = input('Hi. What is your name? ')

An f string starts with a letter f and can automatically concatenate variables enclosed in curly braces, like {variable_name}.

# Using an f string to automatically concatenate
# Without using the plus (+) operator
print(f'Pleased to meet you, {user_name}')

We can concatenate many strings together, but we cannot concatenate strings with integers or floats.

# Concatenating many strings within a print function
print('Hello, ' + 'all ' + 'these ' + 'strings ' + 'are ' + 'being ' + 'connected ' + 'together.')
# Trying to concatenate a string with an integer causes an error
print('There are ' + 7 + 'continents.')

The str(), int(), and float() functions#

We can transform one variable type into another variable type with the str(), int(), and float() functions. Let’s convert the integer above into a string so we can concatenate it.

# Converting an integer into a string
print('There are ' + str(7) + ' continents.')
# Convert the variable `number` to an integer
# Then add 10 to it and print `number`
number = '5'

Mixing strings with floats and integers can have unexpected results. See if you can spot the problem with the program below.

# A program to tell a user how many months old they are
user_age = input('How old are you? ') # Take the user input and put it into the variable user_age
number_of_months = user_age * 12 # Define a new variable number_of_months that multiplies the user's age by 12

print('That is more than ' + number_of_months + ' months old!' ) # Print a response that tells the user they are at least number_of_months old

In order to compute the variable number_of_months, we multiply user_age by 12. The problem is that user_age is a string. Multiplying a string by 12 simply makes the string repeat 12 times. After the user gives us their age, we need that input to be converted to an integer. Can you fix the program?

Python Basics 2 flow control statements#

Description: This lesson describes the basics of flow control statements including:

Python Libraries Used:


Flow Control Statements#

In Python Basics 1, you learned about expressions, operators, variables, and a few native Python functions. We wrote programs that executed line-by-line, starting at the top and running to the bottom. This approach works great for simple programs that may execute a few tasks, but as you begin writing programs that can do multiple tasks you’ll need a way for your programs to decide which action comes next. We can control when (or if) code gets executed with flow control statements. If a program is a set of steps for accomplishing a task, then flow control statements help the program decide the next action.

Flow control statements work like a flowchart. For example, let’s say your goal is to hang out and relax with friends. There are a number of steps you might take, depending on whether your friends are available or you feel like making some new friends.

Flowchart to hangout with friends

Each diamond in our flowchart represents a decision that has to be made about the best step to take next. This is the essence of flow control statements. They help a program decide what the next step should be given the current circumstances.

Boolean Values#

One way we to create flow control statements is with boolean values that have two possible values: True or False. In our example above, we could consider a “Yes” to be “True” and a “No” to be “False.” When we have the data we need to answer each question, we could store that answer in a variable, like:

  • are_friends_available = False

  • make_new_friends = True

  • new_friend_available = True

This would allow us to determine which action to take next. When we assign boolean values to a variable, the first letter must be capitalized:

# Note, the first letter of a boolean value must always be capitalized in Python
are_friends_available = false
print(are_friends_available)
# The boolean values **True** and **False** cannot be used for variable names. 
# Treating the boolean value True as a variable will create an error
True = 7
# But we can store Boolean values in a variable

futbol_is_life = False
tacos_are_life = True

Comparison Operators#

Now that we have a way to store integers, floats, strings, and boolean values in variables, we can use a comparison operator to help make decisions based on those values. We used the comparison operator == in Python Basics 1. This operator asks whether two expressionsare equal to each other.

# Comparing two values with the comparison operator ==
67 == 67
# Note, a comparison operator uses ==
# Do not confuse with variable assignment statement which uses a single =
67 = 67

There are additional comparison operators that can help us with flow control statements.

Operator

Meaning

==

Equal to

!=

Not equal to

<

Less than

>

Greater than

<=

Less than or equal to

>=

Greater than or equal to

# Use the "Not equal to" operator
# Use the "equal to" operator with a string

A string cannot be equal to a float or an integer.

# Try using the "equal to" operator to compare a string with an integer

But an integer can be equal to a float.

# Try using the "equal to" operator to compare an integer with a float

We can use the comparison operator with variables.

# Using a comparison operator on a variable
# Create a variable number_of_dogs and assign the value of zero


# Check whether number_of_dogs is greater than or equal to 1

Boolean Operators (and/or/not)#

We can also use Boolean operators) (and/or/not) to create expressions that evaluate to a single Boolean value (True/False).

Using the Boolean Operator and#

The and operator determines whether both conditions are True.

# If condition one is True AND condition two is True
# What will the evaluation be?
True and True
# If condition one is True AND condition two is False
# What will the evaluation be?
True and False

In order for an and expression to evaluate to True, every condition must be True. Here is the “Truth Table” for every pair:

Expression

Evaluation

True and True

True

True and False

False

False and True

False

False and False

False

Since and expressions require all conditions to be True, they can easily result in False evaluations.

Using the Boolean Operator or#

The or operator determines whether any condition is True.

# Is expression one True OR is expression two True?
True or False
# Is condition one True OR is condition two True?
False or False

An or expression evaluates to True if any condition is True. Here is the “Truth Table” for every pair:

Expression

Evaluation

True or True

True

True or False

True

False or True

True

False or False

False

Since or expressions only require a single condition to be True, they can easily result in True evaluations.

Using the Boolean Operator not#

Thenot operator only operates on a single expression, essentially flipping True to False or False to True.

# The not operator flips a True to False
not False

Combining Boolean and Comparison Operators#

We can combine Boolean operators and comparison operators to create even more nuanced Truth tests.

# Evaluating two conditions with integers at once
(3 < 22) and (60 == 34) # What does each condition evaluate to?
# Evaluating two conditions with integers at once
(3 == 45) or (3 != 7) # What does each condition evaluate to?

So far, we have evaluated one or two conditions at once, but we could compare even more at once. (In practice, this is rare since it creates code that can be difficult to read.) Boolean operators also have an order of operations like mathematical operators. They resolve in the order of not, and, then or.

Writing a Flow Control Statement#

The general form of a flow control statement in Python is a condition followed by an action clause:

In this condition:
       perform this action

Let’s return to part of our flowchart for hanging out with friends.

Flowchart showing if homework is yes then do assignment

We can imagine a flow control statement that would look something like:

if have_homework == True:
        complete assignment

The condition is given followed by a colon (:). The action clause then follows on the next line, indented into a code block.

  • If the condition is fulfilled (evaluates to True), the action clause in the block of code is executed.

  • If the condition is not fulfilled (evaluates to False), the action clause in the block of code is skipped over.

Code Blocks#

A code block is a snippet of code that begins with an indentation. A code block can be a single line or many lines long. Blocks can contain other blocks forming a hierarchal structure. In such a case, the second block is indented an additional degree. Any given block ends when the number of indentations in the current line is less than the number that started the block.

Visualization of code block indentations

Since the level of indentation describes which code block will be executed, improper indentations will make your code crash. When using indentations to create code blocks, look carefully to make sure you are working in the code block you intend. Each indentation for a code block is created by pressing the tab key.

Types of Flow Control Statements#

The code example above uses an if statement, but there are other kinds of flow control statements available in Python.

Statement

Means

Condition for execution

if

if

if the condition is fulfilled

elif

else if

if no previous conditions were met and this condition is met

else

else

if no condition is met (no condition is supplied for an else statement)

while

while

while condition is true

for

for

execute in a loop for this many times

try

try

try this and run the except code if an error occurs

Let’s take a look at each of these flow control statement types.

if Statements#

An if statement begins with an expression that evaluates to True or False.

  • if True, then perform this action

  • if False, skip over this action

In practice, the form looks like this:

if this is True:
        perform this action

Let’s put an if statement into practice with a very simple program that asks the user how their day is going and then responds. We can visualize the flow of the program in a flowchart.

Flowchart of a good day program

Our program will use a single if statement. If the user types “Yes” or “yes”, then our program will send a response.

# A program that responds to a user having a good day
having_good_day = input('Are you having a good day? (Yes or No) ') # Define a variable having_good_day to hold the user's input in a string

if having_good_day == 'Yes' or having_good_day == 'yes':  # If the user has input the string 'Yes' or 'yes'
    print('Glad to hear your day is going well!') # Print: Glad to hear your day is going well!

Our program works fairly well so long as the user inputs ‘Yes’ or ‘yes’. If they type ‘no’ or something else, it simply ends. If we want to have our program still respond, we can use an else statement.

else Statements#

An else statement does not require a condition to evaluate to True or False. It simply executes when none of the previous conditions are met. The form looks like this:

else:
        perform this action

Our updated flowchart now contains a second branch for our program.

The program flowchart with two branches

# A program that responds to whether the user is having a good or bad day
having_good_day = input('Are you having a good day? (Yes or No) ') # Define a variable having_good_day to hold the user's input

if having_good_day == 'Yes' or having_good_day == 'yes': # If the user has input the string 'Yes' or 'yes'
    print('Glad to hear your day is going well!') # Print: Glad to hear your day is going well!
    
# Write an else statement here

Our new program is more robust. The new else statement still gives the user a response if they do not respond “Yes” or “yes”. But what if we wanted to add an option for when a user says “No”? Or when a user inputs something besides “Yes” or “No”? We could use a series of elif statements.

elif Statements#

An elif statement, short for “else if,” allows us to create a list of possible conditions where one (and only one) action will be executed. elif statements come after an initial if statement and before an else statement:

if condition A is True:
        perform action A
elif condition B is True:
        perform action B
elif condition C is True:
        perform action C
elif condition D is True:
        perform action D
else:
       perform action E

For example, we could add an elif statement to our program so it responds to both “Yes” and “No” with unique answers. We could then add an else statement that responds to any user input that is not “Yes” or “No”.

Flowchart showing three branches

# A program that responds to whether the user is having a good or bad day
having_good_day = input('Are you having a good day? (Yes or No) ') # Define a variable having_good_day to hold the user's input

if having_good_day == 'Yes' or having_good_day == 'yes': # If the user has input the string 'Yes' or 'yes'
    print('Glad to hear your day is going well!') # Print: Glad to hear your day is going well!

# Write an elif statement for having_good_day == 'No'


# An else statement that catches if the answer is not 'yes' or 'no'
else: # Execute this if none of the other branches executes
    print('Sorry, I only understand "Yes" or "No"') # Note that we can use double quotations in our string because it begins and ends with single quotes

The difference betweenelif and if?#

When an elif condition is met, all other elif statements are skipped over. This means that one (and only one) flow control statement is executed when using elif statements. The fact that only one elif statement is executed is important because it may be possible for multiple flow control statements to evaluate to True. A series of elif statements evaluates from top-to-bottom, only executing the first elif statement whose condition evaluates to True. The rest of the elif statements are skipped over (whether they are True or False).

In practice, a set of mutually exclusive if statements will result in the same actions as an if statement followed by elif statements. There are a few good reasons, however, to use elif statements:

  1. A series of elif statements helps someone reading your code understand that a single flow control choice is being made.

  2. Using elif statements will make your program run faster since other conditional statements are skipped after the first evaluates to True. Otherwise, every if statement has to be evaluated before the program moves to the next step.

  3. Writing a mutually exclusive set of if statements can be very complex.

In the previous code cell, try changing the elif to an if. Then answer the question with a “Yes.” You’ll receive two responses! Why do you think that is?

while Loop Statements#

So far, we have used flow control statements like decision-making branches to decide what action should be taken next. Sometimes, however, we want a particular action to loop (or repeat) until some condition is met. We can accomplish this with a while loop statement that takes the form:

while condition is True:
       take this action

After the code block is executed, the program loops back to check and see if the while loop condition has changed from True to False. The code block stops looping when the condition becomes False.

In the following program, the user will guess a number until they get it correct.

flowchart for number-guessing program

# A program that asks the user to guess a number

# The secret number is set here by the programmer.
secret_number = str(4) # We convert the integer to a string to compare easily with a user input string

# Ask the user to make a guess and take input in a string
guess = input('I am thinking of a number between 1 and 10. Can you guess it? ') # Take the user's first guess

# Check to see if the user guess matches our secret number
while guess != secret_number: # While the users guess does not equal secret_number
    guess = input('Nope. Guess again! ') # Allow the user to change the value of guess

print('You guessed the secret number, ' + secret_number) # Print a congratulations message with the secret number

Stopping Accidental Infinite Loops#

When using a while loop, it is possible to accidentally create an infinite loop that never ends. This happens because the while condition never becomes False.

If you accidentally write code that infinitely repeats, you can stop the execution by selecting Interrupt from the Kernel menu. (Alternatively, you can press the letter i twice on your keyboard.) It is also a good idea to remove the output of the rogue cell. You can do this from the Edit menu.

  • Clearing output from a single cell: EditClear Cell Output

  • Clearing output from all cells: EditClear Outputs of All Cells

Clearing current outputs.

# Run this infinite loop then interrupt the kernel
from time import sleep # Importing the sleep module so we can slow down our infinite loop

while True:
    print('Oh noes!')
    sleep(1) # A one second pause so our infinite loop doesn't make the notebook gigantic!

If you didn’t already, try clearing the output from the infinite loop code cell!

A Repeating while Loop#

In the program above, the while loop checked to see if the user guessed a particular number. We could also use a while loop to repeat a code block a particular number of times.

# A loop program that prints out 0, 1, 2
i = 0 # A variable to help count how many loops have been completed

while i < 3:
    print(i)
    i = i + 1 # We can also write an equivalent shortcut: i += 1

for Loop Statements with a range() Function#

An abbreviated way to write a while loop that repeats a specified number of times is using a for loop with a range() function. This loop takes the form:

for i in range(j):
       take this action

where i is a generic variable for counting the number of iterations and j is the number of times you want the code block to repeat.

The starting value of i is 0. After each loop, i increases by one until it reaches j. The loop then stops. The variable names i and j are merely conventions. Using a different name may make the purpose of your code clearer to readers.

# A `for` loop that prints the value of the current iteration, here called `i`. 
for i in range(3):
    print(i)

Try your hand at a repeating loop. Write a program that prints “What?” five times.

# A `for` loop that repeats 'What?'

Try changing the variable name i to something else. What effect does that have on the program?

Continue and Break Statements#

while loops and for loops can also use continue and break statements to affect flow control.

  • A continue statement immediately restarts the loop.

  • A break statement immediately exits the loop.

Let’s return to our secret number guessing program. We will write the same program workflow using continue and break statements.

Flowchart for secret number guessing program

# A program that asks the user to guess a number

# Initialize the variables `guess` and `secret_number`
guess = 0
secret_number = str(4) # The secret number is set here by the programmer. Notice it is turned into a string so it can be easily compared with user inputs.

# Ask the user to make a guess
print('I am thinking of a number between 1 and 10.') 

# Test whether the user guess matches `secret_number`
while True:
    guess = input('What is your guess? ')
    if guess == secret_number:
        break
    else:
        continue
        
        
# After loop ends, print a congratulations message with the secret number       
print('You guessed the secret number, ' + secret_number) 

Exception Handling with try and except#

When running code that may create an error, we can use try and except statements to stop a program from crashing.

# Try running the first code block, if there's an error then run the `except` code

try:
    user_number = input('Please enter a whole number: ')
    user_number = int(user_number)
    print('Thank you for entering a whole number.')

except:
    print('That is not a whole number.')

Coding Challenge! < / >

Using your knowledge of flow control statements, can you write a program that asks a user for their name, then prints out a response depending on whether they are old enough to drive? Check the end of this notebook for example solutions.


# Level 1 Challenge

# Ask the user for their name and store it in a variable `user_name`
# Ask the user for their age and store it in a variable `user_age`
# Write an if, elif, else statement that checks to see if the user is driving age.
# If the user is driving age, print out "user_name is old enough to drive."
# If the user is not driving age, print out "user_name is not old enough to drive."
# Level 2 Challenge

# Improve your flow control to consider whether the user has input a realistic age
# If the user inputs an age over 120, print "Is `user_name` a human or a turtle?"
# If the user inputs an age less than 5, print "`user_name` is not even ready for a bicycle!"
# Level 3 Challenge
# A program that checks to see if a user is old enough to drive.
# Verifies user has input a number and it is realistic.

# Find a solution to address when a user enters text that is not a number.
# Level 4 Challenge
# A program that checks to see if a user is old enough to drive.
# It checks to see whether the age entered is a valid number. 
# It also checks to see whether the age entered 
# is a realistic number (greater than 0 and less than 150)
# If the user enters an age that is not a number or unrealistic, it prompts for a new age

Attribution Creative Commons CC BY License

Created by Nathan Kelber and Ted Lawless for JSTOR Labs under Creative Commons CC BY License

Coding Challenge! Solutions#

  • See below

There are often many ways to solve programming problems. Here are a few possible ways to solve the challenges, but there are certainly more!

# Level 1 Solution
# A program that checks to see if a user is old enough to drive.

# Ask the user for their name and store it in a variable `user_name`
# Ask the user for their age and store it in a variable `user_age`
# Write an if, elif, else statement that checks to see if the user is driving age.
# If the user is driving age, print out "user_name is old enough to drive."
# If the user is not driving age, print out "user_name is not old enough to drive."

user_name = input('What is your name? ')
user_age = int(input('And what is your age? '))

if user_age >= 16:
    print(f'{user_name} is old enough to drive.')
elif user_age < 16:
    print(f'{user_name} is not old enough to drive.')
# Level 2 Solution
# A program that checks to see if a user is old enough to drive.
# Now with improved ability to check if the age number is realistic.

# Improve your flow control to consider whether the user has input a realistic age
# If the user inputs an age over 120, print "Is `user_name` a human or a turtle?"
# If the user inputs an age less than 5, print "`user_name` is not even ready for a bicycle!"

user_name = input('What is your name? ')
user_age = int(input('And what is your age? '))

if user_age > 120:
    print(f'Is {user_name} a human or a turtle?')
elif user_age < 5:
    print(f'{user_name} is not even ready for a bicycle!')
elif user_age >= 16:
    print(f'{user_name} is old enough to drive.')
elif user_age < 16:
    print(f'{user_name} is not old enough to drive.')
# Level 3 Solution (using continue/break)
# A program that checks to see if a user is old enough to drive.
# Verifies user has input a number and it is realistic.

# Find a solution to address when a user enters text that is not a number.

user_name = input('What is your name? ')

while True:
    user_age = input('What is your age? ')
    try:
        user_age = int(user_age)
        if user_age > 120:
            print(f'Is {user_name} a human or a turtle?')
        elif user_age < 5:
            print(f'{user_name} is not even ready for a bicycle!')
        elif user_age >= 16:
            print(f'{user_name} is old enough to drive.')
        elif user_age < 16:
            print(f'{user_name} is not old enough to drive.')
        break
    except:
        print('Please input a number for your age.')
        continue
# Level 4 Challenge
# A program that checks to see if a user is old enough to drive.
# It checks to see whether the age entered is a valid number. 
# It also checks to see whether the age entered 
# is a realistic number (greater than 0 and less than 150)
# If the user enters an age that is not a number or unrealistic, it prompts for a new age

# Get user's age
user_name = input('What is your name? ')

# Check that age is a realistic number
while True:
    user_age = input('What is your age? ')
    try:
        user_age = int(user_age)
        if user_age < 120 and user_age > 0:
            break    
        print('Please input a realistic number for your age.')
        continue
    except:
        print('Please input an actual number for your age.')
        continue

# Respond to user's age      
if user_age >= 16:
    print(f'{user_name} is old enough to drive.')
elif user_age < 16:
    print(f'{user_name} is not old enough to drive.')

    

Python Basics 3#

Description: This lesson describes the basics of lists and dictionaries including:

This is part 3 of 5 in the series Python Basics that will prepare you to do text analysis using the Python programming language.


In Python Basics 1, we learned about three data types: integers, floats, and strings. In this lesson, we will learn about two additional data types: lists and dictionaries. Lists and dictionaries help us store many values inside a single variable. This is helpful for a few reasons.

  • We can store many items in a single list or dictionary, making it easier to keep the data together

  • Lists and dictionaries only require a single assignment statement

  • Lists and dictionaries have additional capabilities that will make organizing our data easier

The fundamental difference between a list and a dictionary is that a list stores items in sequential order (starting from 0) while a dictionary stores items in key/value pairs. When we want to retrieve an item in a list, we use an index number or a set of index numbers called a slice as a reference. When we want to retrieve an item from a dictionary, we supply a key that returns the value (or set of values) associated with that key. Each of these approaches can be beneficial depending on what kind of data we are working with (and what we intend to do with the data).

Lists#

A list can store anywhere from zero to millions of items. The items that can be stored in a list include the data types we have already learned: integers, floats, and strings. A list assignment statement takes the form. my_list = [item1, item2, item3, item4…]

my_list = [item1, item2, item3, item4...]

# A list containing integers
my_favorite_numbers = [7, 21, 100]
print(my_favorite_numbers)
# A list containing strings
my_inspirations = ['Harriet Tubman', 'Rosa Parks', 'Pauli Murray']
print(my_inspirations)

Both my_favorite_numbers and my_inspirations have three items, but we could have also initialized them with no items my_favorite_numbers = [] or many more items.

List Index#

Each item has an index number that depends on their order. The first item is 0, the second item is 1, the third item is 2, etc. In the my_inspirations list, 'Pauli Murray' is item 2.

# Retrieving an item in a list
my_inspirations[2]

Try it! < / >

What happens if you change the index number to 1? What about 3? 2.0?


# Try out using different index numbers

Lists can also contain other lists. To retrieve a value from a list within a list, we use two indexes (or indices).

# Retrieving an item from a list within a list
my_inspirations = [['Harriet Tubman', 'Rosa Parks', 'Pauli Murray'], ['Martin Luther King Jr.', 'Frederick Douglass', 'Malcolm X']]
my_inspirations[0][2]

Try it! < / >

Can you change the index for my_inspirations to retrieve 'Malcolm X'?


# Try retrieving Malcolm X from the my_inspirations list

We can also select items from a list beginning from the end/right side of a list by using negative index numbers.

# Retrieving an item from the end of a list by using negative indices
my_inspirations = ['Harriet Tubman', 'Rosa Parks', 'Pauli Murray']
my_inspirations[-1]

It is not uncommon for lists to be hundreds or thousands of items long. It would be a chore to count all those items to create a slice. If you want to know the length of a list, you can use the len() function.

# Using the len() function to discover the number of items in the list
staff = ['Aaron Aston',
         'Brianna Barton',
         'Carla Cameron',
         'Delia Darcy',
         'Evelyn Elgin',
         'Frederick Federov',
         'Gaston Garbo']
len(staff)

Slicing a list#

We can also retrieve a group of consecutive items from a list using slices instead of a single index number. We create a slice by indicating a starting and ending index number. The slice is a smaller list containing all the items between our starting and stopping index number.

# Taking a slice of a list
staff = ['Aaron Aston',
         'Brianna Barton',
         'Carla Cameron',
         'Delia Darcy',
         'Evelyn Elgin',
         'Frederick Federov',
         'Gaston Garbo']
staff[1:3]

Notice in our slice that the second index in a slice is the stopping point. That is our return list contains staff[1] ('Brianna Barton') and staff[2] ('Carla Cameron'), but it does not include staff[3] ('Delia Darcy'). This can be confusing if you were expecting three items instead of two. One way to remember this is by subtracting the indexes in your head (3 - 1 = 2 items).

The staff list is 7 items long, meaning the whole list is within the slice staff[0:7]. When we take a slice of a list we can also leave out the first index number (0 is assumed) or the stopping index number (the last item is assumed).

# Taking a slice of a list without a beginning index
staff = ['Aaron Aston',
         'Brianna Barton',
         'Carla Cameron',
         'Delia Darcy',
         'Evelyn Elgin',
         'Frederick Federov',
         'Gaston Garbo']
staff[:5]

Try it! < / >

Can you take a slice from the staff list without using a an ending index number? What if we don’t use a starting or ending index?


# Try slicing the staff list without an ending index? What about no starting or ending index?

The in and not in Operators#

If we have a long list, it may be helpful to check whether a value is in the list. We can do this with the in and not in operators, which return a boolean value: True or False.

# Checking whether an item is in a list using the `in` operator

# Create a list called `staff`
staff = ['Tara Richards',
 'Tammy French',
 'Justin Douglas',
 'Lauren Marquez',
 'Aaron Wilson',
 'Dennis Howell',
 'Brandon Reed',
 'Kelly Baker',
 'Justin Howard',
 'Sarah Myers',
 'Vanessa Burgess',
 'Timothy Davidson',
 'Jessica Lee',
 'Christopher Miller',
 'Lisa Grant',
 'Ryan Chan',
 'Gary Carson',
 'Anthony Mitchell',
 'Jacob Turner',
 'Jennifer Bonilla',
 'Rachel Gonzalez',
 'Andrew Clark',
 'Richard Pearson',
 'Glenn Allen',
 'Jacqueline Gallagher',
 'Carlos Mcdowell',
 'Jeffrey Harris',
 'Danielle Griffith',
 'Sarah Craig',
 'Vernon Vasquez',
 'Anthony Burton',
 'Erica Bryant',
 'Patricia Walker',
 'Karen Brown',
 'Terri Walker',
 'Michelle Knight',
 'Kathleen Douglas',
 'Debbie Estrada',
 'Jennifer Brewer',
 'Taylor Rodriguez',
 'Lisa Turner',
 'Julie Hudson',
 'Christina Cox',
 'Nancy Patrick',
 'Rita Mosley',
 'Nicholas Gordon',
 'Wanda Vasquez',
 'Jason Lopez',
 'Anna Powers',
 'Tyler Perez']

# Check whether a name is in staff
'Patricia Walker' in staff

Try it! < / >

Is Rita McDowell in staff? What about Erica Bryant?


# Check if Rita McDowell and Erica Bryant are in staff

We can change the value of any item in a list using an assignment statement that contains the item’s index number.

# Changing the value of an item in a list
staff = ['Tara Richards', 'Tammy French', 'Justin Douglas']
print(staff)

# Changing an item using an assignment state with index number
staff[1] = 'Tammy Simons'
print(staff)

Now we know how to change an item in a list. And can use the in and not in operator to determine if an item is in the list. But if we don’t know the index number of the item, we won’t know which index number to change. We need a way to pass both the name of the list and the name of the item simultaneously to a function. To do that, we’ll use a special kind of function called a method.

List Methods#

A method is a kind of function. (Remember, functions end in parentheses.) Methods, however, act on objects (like lists) so they have a slightly different written form. We will take a look at five useful methods for working with lists.

Method Name

Purpose

Form

index()

search for an item in a list and return the index number

list_name.index(item_name)

append()

add an item to the end of a list

list_name.append(item_name)

insert()

insert an item in the middle of a list

list_name.insert(index_number, item_name)

remove()

remove an item from a list based on value

list_name.remove(‘item_value’)

sort()

sort the order of a list

list_name.sort()

The index() Method#

The index() method checks to see if a value is in a list. If the value is found, it returns the index number for the first item with that value. (Keep in mind, there could be multiple items with a single value in a list). If the value is not found, the index() method returns a ValueError.

# Using the index() method to return the index number of an item by passing a value
staff = ['Tara Richards',
 'Tammy French',
 'Justin Douglas',
 'Lauren Marquez',
 'Aaron Wilson',
 'Dennis Howell',
 'Brandon Reed',
 'Kelly Baker',
 'Justin Howard',
 'Sarah Myers',
 'Vanessa Burgess',
 'Timothy Davidson',
 'Jessica Lee',
 'Christopher Miller',
 'Lisa Grant',
 'Ryan Chan',
 'Gary Carson',
 'Anthony Mitchell',
 'Jacob Turner',
 'Jennifer Bonilla',
 'Rachel Gonzalez',
 'Andrew Clark',
 'Richard Pearson',
 'Glenn Allen']

# Find the index number of a list item
staff.index('Jessica Lee')

Try it! < / >

What is the index number of Lisa Grant in staff?


Lists can contain multiple identical items. If there are multiple identical items, we need to use some flow control to return all the indices. The enumerate() function allows us to keep track of the index and element at the same time. Notice that this for loop defines two variables. The first variable keeps track of the current index number in the loop, the second variable keeps track of the value for the current element.

# Getting all indices for items that repeat

staff = ['Tara Richards',
 'John Smith',
 'Justin Douglas',
 'Lauren Marquez',
 'John Smith']

# Use the enumerate function
for index, name in enumerate(staff):
    if name == 'John Smith':
        print(index)

The append() Method#

The append() method adds a value to the end of a list.


Try it! < / >

Can you add your name to staff?


# Using the append() method to add an item to the end of a list
staff = ['Tara Richards',
 'Tammy French',
 'Justin Douglas',
 'Lauren Marquez',
 'Aaron Wilson',
 'Dennis Howell',
 'Brandon Reed',
 'Kelly Baker',
 'Justin Howard',
 'Sarah Myers',
 'Vanessa Burgess',
 'Timothy Davidson',
 'Jessica Lee',
 'Christopher Miller',
 'Lisa Grant',
 'Ryan Chan',
 'Gary Carson',
 'Anthony Mitchell',
 'Jacob Turner',
 'Jennifer Bonilla',
 'Rachel Gonzalez',
 'Andrew Clark',
 'Richard Pearson',
 'Glenn Allen']

# Append a staff member to the list


# Prints `staff` in a vertical list format to check that the name was added. 
# The list() function will print our list in an easier to read format than the print() function.
list(staff) 

We can also add an item to the end of a list by using an assignment statement.

# Adding an item to a list using an assignment statement
# We are concatenating two lists

staff = staff + ['Dustin Henderson'] # Concatenate staff list with the list ['Dustin Henderson']
list(staff)

The insert() Method#

The insert() method is similar to append() but it takes an argument that lets us choose an index number to insert the new item.

# Using the insert() method to add an item at a specific index number
staff = ['Tara Richards',
 'Tammy French',
 'Justin Douglas',
 'Lauren Marquez',
 'Aaron Wilson',
 'Dennis Howell',
 'Brandon Reed',
 'Kelly Baker',
 'Justin Howard',
 'Sarah Myers',
 'Vanessa Burgess',
 'Timothy Davidson',
 'Jessica Lee',
 'Christopher Miller',
 'Lisa Grant',
 'Ryan Chan',
 'Gary Carson',
 'Anthony Mitchell',
 'Jacob Turner',
 'Jennifer Bonilla',
 'Rachel Gonzalez',
 'Andrew Clark',
 'Richard Pearson',
 'Glenn Allen']

staff.insert(5, 'Arya Stark') # Insert the name 'Arya Stark' at index 5 (The sixth name on the list)
list(staff) # Prints `staff` to check that the name was added at the right spot

Try it! < / >

Can you make your name the third item on staff?


# Add your name to the staff list


# Show the new staff list after your addition
list(staff)

The remove() Method#

The remove() method removes the first item from the list that has a matching value.

# Using the remove() method to remove the first item with a matching value
staff = ['Tara Richards',
 'Tammy French',
 'Justin Douglas',
 'Lauren Marquez',
 'Aaron Wilson',
 'Dennis Howell',
 'Brandon Reed',
 'Kelly Baker',
 'Justin Howard',
 'Sarah Myers',
 'Vanessa Burgess',
 'Timothy Davidson',
 'Jessica Lee',
 'Christopher Miller',
 'Lisa Grant',
 'Ryan Chan',
 'Gary Carson',
 'Anthony Mitchell',
 'Jacob Turner',
 'Jennifer Bonilla',
 'Rachel Gonzalez',
 'Andrew Clark',
 'Richard Pearson',
 'Glenn Allen']
# Write your remove statement under this comment
staff.remove('Richard Pearson')
# Print `staff` to check that the name was removed
list(staff) 

Try it! < / >

Can you remove Glenn Allen from staff?


# Remove Glenn Allen from the staff list


# Show the staff list again to confirm the change was made
list(staff)

Like the .index() method, the .remove() only works on the first item it finds in the list. If there are repeating items on the list, you can use some flow control to make sure you remove them all.

# Removing an item that appears multiple times on the list

staff = ['John Smith',
 'Lauren Marquez',
 'John Smith']

# Use a while loop for flow control
while 'John Smith' in staff:
    staff.remove('John Smith')

# List the staff members after the removal
list(staff)

If you know the value you wish to remove then the remove() method is the best option. If you know the index number of the item, you can use a del statement to delete list items.

# Using a `del` statement to delete a list item
staff = ['Tara Richards',
 'Tammy French',
 'Justin Douglas',
 'Lauren Marquez',
 'Aaron Wilson',
 'Dennis Howell',
 'Brandon Reed',
 'Kelly Baker',
 'Justin Howard',
 'Sarah Myers',
 'Vanessa Burgess',
 'Timothy Davidson',
 'Jessica Lee',
 'Christopher Miller',
 'Lisa Grant',
 'Ryan Chan',
 'Gary Carson',
 'Anthony Mitchell',
 'Jacob Turner',
 'Jennifer Bonilla',
 'Rachel Gonzalez',
 'Andrew Clark',
 'Richard Pearson',
 'Glenn Allen']
del staff[-1] # Delete the final item in the list. In this case, 'Glenn Allen'.
list(staff)

The sort() Method#

The sort() method sorts a list in alphabetical order, where strings with capital letters are sorted A-Z, then strings with lowercase letters are sorted A-Z.

# Using the `sort()` method to sort a list in alpha-numeric order
staff = ['Tara Richards',
 'Tammy French',
 'Justin Douglas',
 'Lauren Marquez',
 'Aaron Wilson',
 'Dennis Howell',
 'Brandon Reed',
 'Kelly Baker',
 'Justin Howard',
 'Sarah Myers',
 'Vanessa Burgess',
 'Timothy Davidson',
 'Jessica Lee',
 'Christopher Miller',
 'Lisa Grant',
 'Ryan Chan',
 'Gary Carson',
 'Anthony Mitchell',
 'Jacob Turner',
 'Jennifer Bonilla',
 'Rachel Gonzalez',
 'Andrew Clark',
 'Richard Pearson',
 'Glenn Allen']
staff.sort()
list(staff)

Try it! < / >

The sort() method can take the argument reverse=True. Try applying it to the sort() method above. What does it do?


# Try out sorting in reverse order

Iterate through a list with a for loop#

We can use a for loop to iterate through all the items in a list. The for loop will create a new temporary variable to store the current item in the list (no assignment statement required).

# Using a for loop with a list
for person in staff: # Whatever is after `for` becomes the temporary variable
    print(person)

Coding Challenge! < / >

Using your knowledge of flow control statements and lists, can you solve the following challenges? To complete these challenges, we will use a list called ranking based on FIFA’s rankings of the Women’s National Teams before the World Cup.


# Level 1 Challenge
# The data has some errors in it. Correct the following errors:
# Correct the spelling of Canada in the rankings
# Remove Uruguay from the rankings and add Australia at the bottom
# Add England to the rankings after Sweden

ranking = ['USA',
           'Germany',
           'Sweden',
           'France',
           'Uruguay',
           'Spain',
           'Canda',
           'Brazil',
           'Netherlands',]
# Level 2 Challenge
# Write a program that lets the user input any country and check if they are in the top ten ranking
ranking = ['USA',
 'Germany',
 'Sweden',
 'England',
 'France',
 'Spain',
 'Canada',
 'Brazil',
 'Netherlands',
 'Australia']
# Level 3 Challenge
# Write a program that lets a user input a country and receive their rank

ranking = ['USA',
 'Germany',
 'Sweden',
 'England',
 'France',
 'Spain',
 'Canada',
 'Brazil',
 'Netherlands',
 'Australia']
# Level 4 Challenge
# Write a program that lets a user input a country and return all the teams with a higher rank
ranking = ['USA',
 'Germany',
 'Sweden',
 'England',
 'France',
 'Spain',
 'Canada',
 'Brazil',
 'Netherlands',
 'Australia']

Dictionaries#

Like a list, a dictionary can hold many values within a single variable. We have seen that the items of a list are stored in a strictly-ordered fashion, starting from item 0. In a dictionary, each value is stored in relation to a descriptive key forming a key/value pair. Technically, as of Python 3.7 (June 2018), dictionaries are also ordered by insertion. In practice, however, the most useful aspect of a dictionary is the ability to supply a key and receive a value without reference to indices. Whereas a list is typed with brackets [], a dictionary is typed with braces {}. The key and/or value can be an integer, float, or string.

example_dictionary = {key1 : value1, key2 : value2, key3 : value3}

# An example of a dictionary storing names and occupations
contacts ={
 'Amanda Bennett': 'Engineer, electrical',
 'Bryan Miller': 'Radiation protection practitioner',
 'Christopher Garrison': 'Planning and development surveyor',
 'Debra Allen': 'Intelligence analyst',
 'Donna Decker': 'Architect',
 'Heather Bullock': 'Media planner',
 'Jason Brown': 'Energy manager',
 'Jason Soto': 'Lighting technician, broadcasting/film/video',
 'Marissa Munoz': 'Further education lecturer',
 'Matthew Mccall': 'Chief Technology Officer',
 'Michael Norman': 'Translator',
 'Nicole Leblanc': 'Financial controller',
 'Noah Delgado': 'Engineer, land',
 'Rachel Charles': 'Physicist, medical',
 'Stephanie Petty': 'Architect'}
from pprint import pprint # We import the pretty print function which prints out dictionaries in a neater fashion than the built-in print() function
pprint(contacts) # Use the pretty print function to print `contacts`

We can add a new key/value pair to our dictionary using an assignment statement.

# Adding the key 'Mirza, Rafia' with the value 'Digital Scholarship Librarian' to the dictionary contact
contacts['Rafia Mirza'] = 'Digital Scholarship Librarian'

pprint(contacts) # Use the pretty print function to print `contacts`

Try it! < / >

Can you add your name to the contacts dictionary?


Similar to deleting an item from a list, we can use a del statement to delete a key/value pair. We do not need to worry about duplicates because every key in a dictionary must be unique.

# Deleting a key/value pair from the dictionary contacts
del contacts['Bryan Miller'] 

pprint(contacts) # Use the pretty print function to print `contacts`

Dictionary Methods#

We’ll take a look at five useful methods for working with dictionaries: update(), keys(), values(), items(), and get().

Method Name

Purpose

Form

update()

add new key/value pairs to a dictionary

dict_name.update({key1:value1, key2:value2})

 

combine two dictionaries

dict_name.update(dict_name2)

keys()

check if a key is in a dictionary (True/False)

key_name in dict_name.keys()

 

Loop through the keys in a dictionary

for k in dict.keys():

values()

check if a value is in a dictionary (True/False)

value_name in dict_name.values()

 

Loop through the values in a dictionary

for v in dict.values():

items()

Loop through the keys and values in a dictionary

for k, v in dict.items():

get()

retrieve the value for a specific key

dict_name.get(key_name)

The update() Method#

The update() method is useful for adding many key/value pairs to a dictionary at once. The update() method accepts a single key/value pair, multiple pairs, or even other dictionaries.

# Add a single key/value pair to the dictionary contacts using the update() method
contacts.update(
    {'Rafia Mirza'': 'Digital Scholarship Librarian'}
)

pprint(contacts) # Use the pretty print function to print `contacts`
# Adding several key/value pairs to the dictionary contacts using the update() method
contacts.update(
    {"Matt Lincoln": "Software Engineer",
     'Ian DesJardins': 'Software Engineer',
    'Zhuo Chen': 'Text Analysis Instructor'}
)

pprint(contacts) # Use the pretty print function to print `contacts`

The keys() and values() Methods#

The keys(), values(), and items() methods are useful for when checking whether a particular key or value exists in a dictionary. We can pair them with in or not in operators to check whether a value is in our dictionary (just like we did with lists).

# Checking if a key is in the contacts dictionary
# Do I know a Noah Delgado?
'Noah Delgado' in contacts.keys()
# Checking if a value is in the contacts dictionary
# Do I know an Architect?
'Architect' in contacts.values()

The get() Method#

If we are sure a key exists, we can return the corresponding value using:

dict_name[key_name]

# Return a value for a particular key
contacts['Noah Delgado']

However, if the key](https://constellate.org/docs/key-terms/#key-value-pair) is not found, the result will be a KeyError.

# Asking for a key that does not exist
contacts['Mickey Mouse']

The more robust approach is to use the get() method. If the key is not found, the None value will be returned. (Optionally, we can also specify a default message to return.)

dict_name.get('key_name', 'key_not_found_message')

# Using the get() method to retrieve the value for the key 'Marissa Munoz'
contacts.get('Marissa Munoz')

Combining keys(), values(), and items() with Flow Control Statements#

It is often usful to combine for loops with the keys(), values(), or items() methods to repeat a task for each entry in a dictionary. We have the following options:

  • .keys() iterates through only the dictionary keys

  • .values() iterates through only the dictionary values

  • .items() iterates through the keys and values

Just like a list for loop, a temporary variable will be created based on whatever name comes after for.

# Print every key in our contacts dictionary
for name in contacts.keys(): # The variable `name` could be any variable name we choose
    print(name)
# Print every value in our contacts dictionary
for occupation in contacts.values(): # The variable `occupation` here could be any variable name we choose
    print(occupation)

If we use the .items() method, we need to define two variable names. It is valid Python to define two variables at once.

# Define two variables at once in Python and assign two strings
word1, word2 = 'Python', 'Basics'

# Verify the variables have been properly assigned
print(word1)
print(word2)
# Print every key and value in our contacts dictionary
for name, occupation in contacts.items():
    print(f'{name} has the job: {occupation}')

Coding Challenge! < / >

Using your knowledge of flow control statements and dictionaries, can you write a program that lists all the architects in your contacts? Check the end of this notebook for possible solutions.


# Level 1 Challenge
# A challenge to search a contacts dictionary and output any individuals who are architects. The result
# should contain the name of any contacts who are architects.

contacts ={
 'Amanda Bennett': 'Engineer, electrical',
 'Bryan Miller': 'Radiation protection practitioner',
 'Christopher Garrison': 'Planning and development surveyor',
 'Debra Allen': 'Intelligence analyst',
 'Donna Decker': 'Architect',
 'Heather Bullock': 'Media planner',
 'Jason Brown': 'Energy manager',
 'Jason Soto': 'Lighting technician, broadcasting/film/video',
 'Marissa Munoz': 'Further education lecturer',
 'Matthew Mccall': 'Chief Technology Officer',
 'Michael Norman': 'Translator',
 'Nicole Leblanc': 'Financial controller',
 'Noah Delgado': 'Engineer, land',
 'Rachel Charles': 'Physicist, medical',
 'Stephanie Petty': 'Architect'}

# Write your solution here. Example solutions at the end of this notebook.
# Level 2 Challenge
# From the dictionary called contacts, create a Python list called `leads` containing the names of people 
# who are architects, translators, or media planners.

Coding Challenge! Solutions#

# Level 1 Solution
# Correct the errors in the ranking list

# Create the list
ranking = ['USA',
           'Germany',
           'Sweden',
           'France',
           'Uruguay',
           'Spain',
           'Canda',
           'Brazil',
           'Netherlands',]

# Correct the spelling of Canada
ranking[6] = 'Canada'

# Remove Uruguay and add Australia
if 'Uruguay' in ranking:
    ranking.remove('Uruguay')

ranking.append('Australia')

# Add England after Sweden
if 'Sweden' in ranking:
    england_index = ranking.index('Sweden') + 1
    ranking.insert(england_index, 'England')
# Level 2 Solution
# Write a program that lets the user input any country and check if they are in the top ten ranking

# Create the ranking list
ranking = ['USA',
 'Germany',
 'Sweden',
 'England',
 'France',
 'Spain',
 'Canada',
 'Brazil',
 'Netherlands',
 'Australia']

# Get the country from the user
country = input('Which country are you interested in? ')

if country in ranking:
    print(f'{country} is in the top ten!')
else:
    print(f'{country} is not in the top ten.')
# Level 3 Solution
# Write a program that lets a user input a country and receive their rank

# Create the ranking list
ranking = ['USA',
 'Germany',
 'Sweden',
 'England',
 'France',
 'Spain',
 'Canada',
 'Brazil',
 'Netherlands',
 'Australia']

# Get the country from the user
country = input('Which country are you interested in? ')

if country in ranking:
    rank = ranking.index(country) + 1
    print(f'{country} is ranked {rank}')
else:
    print(f'{country} is not in the top ten.')
# Level 4 Solution
# Write a program that lets a user input a country and return all the teams with a higher rank

# Create the ranking list
ranking = ['USA',
 'Germany',
 'Sweden',
 'England',
 'France',
 'Spain',
 'Canada',
 'Brazil',
 'Netherlands',
 'Australia']

# Get the country from the user
country = input('Which country are you interested in? ')

if country in ranking:
    index = ranking.index(country)
else:
    index = None
    print('That country is not in the top ten.')

if index != None:
    print('These countries have a higher rank:')
    for each_country in ranking[:index]:
        print(each_country)

Dictionaries Challenge Level 1#

# Level 1 Challenge Solution
# A challenge to search a contacts dictionary and output any individuals who are architects. The result
# should contain the name of any contacts who are architects.

contacts ={
 'Amanda Bennett': 'Engineer, electrical',
 'Bryan Miller': 'Radiation protection practitioner',
 'Christopher Garrison': 'Planning and development surveyor',
 'Debra Allen': 'Intelligence analyst',
 'Donna Decker': 'Architect',
 'Heather Bullock': 'Media planner',
 'Jason Brown': 'Energy manager',
 'Jason Soto': 'Lighting technician, broadcasting/film/video',
 'Marissa Munoz': 'Further education lecturer',
 'Matthew Mccall': 'Chief Technology Officer',
 'Michael Norman': 'Translator',
 'Nicole Leblanc': 'Financial controller',
 'Noah Delgado': 'Engineer, land',
 'Rachel Charles': 'Physicist, medical',
 'Stephanie Petty': 'Architect'}

# Write your solution here. Example solutions at the end of this notebook.

for name, occupation in contacts.items():
    if occupation == 'Architect':
        print(name)

Dictionaries Challenge Level 2#

# Level 2 Challenge
# From the dictionary called contacts, create a Python list called `leads` containing the names of people 
# who are architects, translators, or media planners.
leads = []

for name, occupation in contacts.items():
    if occupation == 'Architect' or occupation == 'Translator' or occupation =='Media planner':
        leads.append(name)

print(leads)

Python Basics 4#

Description: This lesson describes the basics of writing your own functions including:

This lesson concludes with a description of popular Python packages.

Python LibrariesUsed:


Functions#

We have used several Python functions already, including print(), input(), and range(). You can identify a function by the fact that it ends with a set of parentheses () where arguments can be passed into the function. Depending on the function (and your goals for using it), a function may accept no arguments, a single argument, or many arguments. For example, when we use the print() function, a string (or a variable containing a string) is passed as an argument.

Functions are a convenient shorthand, like a mini-program, that makes our code more modular. We don’t need to know all the details of how the print() function works in order to use it. Functions are sometimes called “black boxes”, in that we can put an argument into the box and a return value comes out. We don’t need to know the inner details of the “black box” to use it. (Of course, as you advance your programming skills, you may become curious about how certain functions work. And if you work with sensitive data, you may need to peer in the black box to ensure the security and accuracy of the output.)

Libraries and Modules#

While Python comes with many functions, there are thousands more that others have written. Adding them all to Python would create mass confusion, since many people could use the same name for functions that do different things. The solution then is that functions are stored in modules that can be imported for use. A module is a Python file (extension “.py”) that contains the definitions for the functions written in Python. These modules (individual Python files) can then be collected into even larger groups called packages and libraries. Depending on how many functions you need for the program you are writing, you may import a single module, a package of modules, or a whole library.

The general form of importing a module is: import module_name

You may recall from an eariler lesson, we imported the time module and used the sleep() function to wait 5 seconds.

# A program that waits five seconds then prints "Done"

import time # We import all the functions in the `time` module

print('Waiting 5 seconds...')
time.sleep(5) # We run the sleep() function from the time module using `time.sleep()`
print('Done')

We can also just import the sleep() function without importing the whole time module. The syntax is:

from module import function

# A program that waits five seconds then prints "Done"

from time import sleep # We import just the sleep() function from the time module


print('Waiting 5 seconds...')

sleep(5) # Notice that we just call the sleep() function, not time.sleep()
print('Done')

Writing a Function#

In the above examples, we called a function that was already written. However, we can also create our own functions!

The first step is to define the function before we call it. We use a function definition statement followed by a function description and a code block containing the function’s actions:

def my_function():
    """Description of what the functions does"""
    python code to be executed

After the function is defined, we can call on it to do us a favor whenever we need by simply executing the function like so:

my_function()

After the function is defined, we can call it as many times as we want without having to rewrite its code. In the example below, we create a function called complimenter_function then call it twice.

# Create a complimenter function
def complimenter_function():
    """prints a compliment""" # Function definition statement
    print('You are looking great today!')

After you define a function, don’t forget to call it to make it do the work!

# Give a compliment by calling the function
complimenter_function()

Ideally, a function definition statement should specify the data that the function takes and whether it returns any data. The triple quote notation can use single or double quotes, and it allows the string for the definition statement to expand over multiple lines in Python. If you would like to see a function’s definition statement, you can use the help() function to check it out.

# Examining the function definition statement for our function
# Note that the parentheses are not included with complimenter_function
help(complimenter_function)
# Try using help() to read the definition for the sleep function

Parameters vs. Arguments#

When we write a function definition, we can define a parameter to work with the function. We use the word parameter to describe the variable in parentheses within a function definition:

def my_function(input_variable):
    """Takes in X and returns Y"""
    do this task

In the pseudo-code above, input_variable is a parameter because it is being used within the context of a function definition. When we actually call and run our function, the actual variable or value we pass to the function]is called an argument.

# Change the complimenter function to give user-dependent compliment
def complimenter_function(user_name):
    """Takes in a name string, prints a compliment with the name"""
    print(f'You are looking great today, {user_name}!')
# Pass an argument to a function
complimenter_function('Sam')

Arguments can be passed in based on parameter order (positional) or they can be explicitly passed using an =. (This could be useful if we wanted to pass an argument for the 10th parameter, but we did not want to pass arguments for the nine other parameters defined before it.)

# Pass an argument with =
# user_name is the parameter, 'Sam' is the argument
complimenter_function(user_name='Sam')

In the above example, we passed a string into our function, but we could also pass a variable. Try this next. Since the complimenter_function has already been defined, you can call it in the next cell without defining it again.

# Ask the user for their name and store it in a variable called name
# Then call the complimenter_function and pass in the name variable

A variable passed into a function could contain a list or dictionary. The type of objects that should be passed in and returned can optionally be suggested through type hinting in Python.

# A list of names
list_of_names = ['Jenny', 'Pierre', 'Hamed']

def greet_a_list(names):
    """takes a list of names and prints out a greeting for each name"""
    for name in names:
        print(f'Hi {name}!')

greet_a_list(list_of_names)

The Importance of Avoiding Duplication#

Using functions makes it easier for us to update our code. Let’s say we wanted to change our compliment. We can simply change the function definition one time to make the change everywhere. See if you can change the compliment given by our complimenter function.

# Create a complimenter function that gives compliment
def complimenter_function(user_name):
    """Takes in a name string, prints a compliment with the name"""
    print(f'You are looking great today, {user_name}!')
# Give a new compliment by calling the function
name = input('What is your name? ')
complimenter_function(name)

friend = input('Who is your friend? ')
complimenter_function(friend)

By changing our function definition just one time, we were able to make our program behave differently every time it was called. If our program was large, it might call our custom function hundreds of times. If our code repeated like that, we would need to change it in every place!

Generally, it is good practice to avoid duplicating program code to avoid having to change it in multiple places. When programmers edit their code, they may spend time deduplicating (getting rid of code that repeats). This makes the code easier to read and maintain.


Coding Challenge! < / >

In the next cell, try writing a function that accepts a dictionary as an argument. Use a flow control statement to print out all the names and occupations for the contacts.


# A dictionary of names and occupations
contacts = {
 'Amanda Bennett': 'Engineer, electrical',
 'Bryan Miller': 'Radiation protection practitioner',
 'Chris Garrison': 'Planning and development surveyor',
 'Debra Allen': 'Intelligence analyst'}

# Define and then call your function here

Function Return Values#

Whether or not a function takes an argument, it will always return a value. If we do not specify that return value in our function definition, it is automatically set to None, a special value like the Boolean True and False that simply means null or nothing. (None is not the same thing as, say, the integer 0.) We can also specify return values for our function using a flow control statement followed by return in a code block.

If you don’t write a Return statement in your function, a None value will be returned. If you don’t write a Return statement in your function, a None value will be returned.

# Find out the returned value for the following function

def complimenter_function(user_name):
    """Takes in a name string, prints a compliment with the name"""
    print(f'You are looking great today, {user_name}!')

print(complimenter_function('Sam'))

Instead of automatically printing inside the function, the better approach is to return a string value and let the user decide whether to print it or do something else with it. Ideally, our function definition statement should indicate what goes into the function and what is returned by the function.

# Adding a return statement
def complimenter_function(user_name):
    """Takes in a name string, returns a compliment with the name""" # We are returning now
    return f'You are looking great today, {user_name}!'

compliment = complimenter_function('Sam')
print(compliment)

Returning the string allows the programmer to use the output instead of just printing it automatically. This is usually the better practice.

We can also offer multiple return statements with flow control. Let’s write a function for telling fortunes. We can call it fortune_picker and it will accept a number (1-6) then return a string for the fortune.

# A fortune-teller program that contains a function `fortune_picker`
# `fortune_picker` accepts an integer (1-6) and returns a fortune string

def fortune_picker(fortune_number): # A function definition statement that has a parameter `fortune_number`
    """takes an integer (1-6) and returns a fortune string"""
    if fortune_number == 1:
        return 'You will have six children.'
    elif fortune_number == 2:
        return 'You will become very wise.'
    elif  fortune_number == 3:
        return 'A new friend will help you find yourself.'
    elif fortune_number == 4:
        return 'A great fortune is coming to you.'
    elif fortune_number == 5:
        return 'That promising venture... it is a trap.'
    elif fortune_number == 6: 
        return 'Sort yourself out then find love.'

fortune = fortune_picker(3) # return a fortune string and store it in fortune
print(fortune)

In our example, we passed the argument 3 that returned the string 'A new friend will help you find yourself'. To change the fortune, we would have to pass a different integer into the function. To make our fortune-teller random, we could import the function randint() that chooses a random number between two integers. We pass the two integers as arguments separated by a comma.

# A fortune-teller program that uses a random integer

from random import randint # import the randint() function from the random module

def fortune_picker(fortune_number): # A function definition statement that has a parameter `fortune_number`
    if fortune_number == 1:
        return 'You will have six children.'
    elif fortune_number == 2:
        return 'You will become very wise.'
    elif  fortune_number == 3:
        return 'A new friend will help you find yourself.'
    elif fortune_number == 4:
        return 'A great fortune is coming to you.'
    elif fortune_number == 5:
        return 'That promising venture... it is a trap.'
    elif fortune_number == 6: 
        return 'Sort yourself out then find love.'

random_number = randint(1, 6) # Choose a random number between 1 and 6 and assign it to a new variable `random_number`
fortune = fortune_picker(random_number) # Return a fortune string

print('Your fortune is: ')
print(fortune)
    
while True:
    print('Would you like another fortune?')
    repeat_fortune = input()
    if repeat_fortune == 'yes' or repeat_fortune == 'Yes':
        random_number = randint(1, 6) 
        print(fortune_picker(random_number))
        continue
    else:
        print('I have no more fortunes to share.')
        break

Coding Challenge! < / >

Try writing a function that accepts user inputting a name and returns the person’s occupation. You can use the .get() method to retrieve the relevant occupation, such as:

contacts.get('Amanda', 'No contact with that name')

Remember, the second string will be returned if the name ‘Amanda’ is not in our dictionary.


# A program that returns the occupation when users supply a given name

# A dictionary of names and occupations
contacts = {
 'Amanda': 'Engineer, electrical',
 'Bryan': 'Radiation protection practitioner',
 'Christopher': 'Planning and development surveyor',
 'Debra': 'Intelligence analyst'}

# The function definition and program

Local and Global Scope#

We have seen that functions make maintaining code easier by avoiding duplication. One of the most dangerous areas for duplication is variable names. As programming projects become larger, the possibility that a variable will be re-used goes up. This can cause weird errors in our programs that are hard to track down. We can alleviate the problem of duplicate variable names through the concepts of local scope and global scope.

We use the phrase local scope to describe what happens within a function. The local scope of a function may contain a local variables, but once that function has completed the local variables and their contents are erased.

On the other hand, we can also create global variables that persist at the top-level of the program and also within the local scope of a function.

* In the global scope, Python does not recognize any local variable from within the program's functions
* In the local scope of a function, Python can recognize any global variables
* It is possible for there to be a global variable and a local variable with the same name

Ideally, Python programs should limit the number of global variables and create most variables in a local scope. This keeps confounding variables localized in functions where they are used and then discarded.

# Demonstration of global variable being used in a local scope
# The program crashes when a local variable is used in a global scope
global_string = 'global'

def print_strings():
    print('We are in the local context:')
    local_string = 'local'
    print(global_string)
    print(local_string)
    

print_strings()

The code above defines a global variable global_string with the value of ‘global’. A function, called print_strings, then defines a local variable local_string with a value of ‘local’. When we call the print_strings() function, it prints the local variable and the global variable.

# The function has closed, now the local string has been discarded
print('We are now in the global context: ')
print(global_string)
print(local_string)

After the print_strings() function completes, we try to print both variables in a global scope. The program prints global_string but crashes when trying to print local_string in a global scope.

It’s a good practice not to name a local variable the same thing as a global variable. If we define a variable with the same name in a local scope, it becomes a local variable within that scope. Once the function is closed, the global variable retains its original value.

# A demonstration of global and local scope using the same variable name
# print(string) returns two different results
string = 'global'

def share_strings():
    string = 'local'
    print(string)

share_strings()
# print the string variable in the global context
print(string)

Installing a Python Package in Juypter Lab#

If you would like to install a package that is not in Juypter Lab, we recommend using the pip installer with packages from the Python Package Index. In a code cell insert the following code:

!pip install package_name

for the relevant package you would like to install. The exclamation point indicates the line should be run as a terminal command.

Refer to the package’s documentation for guidance.

# Install Scrapy
!pip install scrapy

Exercise Solutions#

Here are a few solutions for exercises in this lesson.

# A dictionary of names and occupations
contacts = {
 'Amanda Bennett': 'Engineer, electrical',
 'Bryan Miller': 'Radiation protection practitioner',
 'Chris Garrison': 'Planning and development surveyor',
 'Debra Allen': 'Intelligence analyst'}

# Define and then call your function here

def print_contacts(contacts_names):
    """Prints out all the contacts in a contacts dictionary"""
    for name, occupation in contacts.items():
        print(name.ljust(15), '|', occupation)

print_contacts(contacts)
# A dictionary of names and occupations
contacts = {
 'Amanda': 'Engineer, electrical',
 'Bryan': 'Radiation protection practitioner',
 'Christopher': 'Planning and development surveyor',
 'Debra': 'Intelligence analyst'}


def occupation_finder(name):
    """Allows a user to find the occupation of a particular contact"""
    return contacts.get(name, 'No contact with that name')
    
while True:
    print('Enter a name to look up an occupation (or enter quit):')
    name = input()
    if name == 'quit':
        print('Shutting down..')
        break
    else:
        print(occupation_finder(name))
        continue

Python Basics 5#

Description: This notebook focuses on strings, preparing learners to use:

  • Escape characters

  • String methods


The print() function#

Often when working with strings, we use the print() function. A deeper understanding of print() will help us work with strings more flexibly.

Escape characters#

Python strings can use single or double quotes. If the string contains a single quote character, it may be beneficial to use double quotes. Try printing out the string in the next code cell:

# Print out a string using single or double quotes
string = 'Hello World: Here's a string.'
print(string)

An easy solution would be to use double quotes, such as:

string = “Hello World: Here’s a string.”

The use of double quotes keeps Python from ending the string prematurely. But what if your string contains both single and double quotes? Escape characters help us insert certain characters into a string. An escape character begins with a \. For example, we could insert a single quote into a string surrounded by single quotes by using an escape character.

# Print out a single quote in a Python string
string = 'There\'s an escape character in this string.'
print(string)

The backslash character \ in front of the single quote tells Python not to end the string prematurely. Of course, this opens a new question: How do we create a string with a backslash? The answer is another escape character using two backslashes.

# Print a backslash using an escape character
string = 'Adding a backslash \\ requires an escape character.'
print(string)

Another option is to use a raw string, which ignores any escape characters. A raw string simply starts with an r similar to an f string.

string = r'No escape characters \ here'
print(string)

Escape characters also do more than just allow us to add quotes and backslashes. They are also responsible for string formatting for aspects such as tabs and new lines.

Code

Result

\'

\\

\

\t

tab

\n

new line

# Print out a string with two lines
# Print out a string with a tab

Try it! < / >

Can you print a string with a new line? How about a tab?


The newline escape character \n can affect readability for many lines. Consider this string containing four lines of a Shakespeare sonnet.

string = 'Shall I compare thee to a summer’s day?\nThou art more lovely and more temperate:\nRough winds do shake the darling buds of May,\nAnd summer’s lease hath all too short a date;\n'
print(string)

A more readable option is to create a string with a triple quote (single or double). This string type can also automatically interpret new lines and tabs.

# Print out Shakespeare's Sonnet 18
string = """Shall I compare thee to a summer’s day?
Thou art more lovely and more temperate:
Rough winds do shake the darling buds of May,
And summer’s lease hath all too short a date;
Sometime too hot the eye of heaven shines,
And often is his gold complexion dimm'd;
And every fair from fair sometime declines,
By chance or nature’s changing course untrimm'd;
But thy eternal summer shall not fade,
Nor lose possession of that fair thou ow’st;
Nor shall death brag thou wander’st in his shade,
When in eternal lines to time thou grow’st:
    So long as men can breathe or eyes can see,
    So long lives this, and this gives life to thee."""

print(string)

Formatted strings (f-strings)#

An f-string can help us concatenate a variable inside of string. Consider this example where a print function must concatenate three strings:

# Greeting a user with a concatenated string
username = input('Hi. What is your name? ')

print('Hello ' + username + '!')

We used the + operator twice to concatenate username between the strings 'Hello ' and '!'. A simpler method would be to use an f-string. Similar to the way a raw string begins with an r r'string', the formatted string begins with an f f'string'. The variable to be concatenated is then included in curly brackets {}.

# Print the username inside a formatted string
print(f'Hello {username}!')

Using print() with a sep or end argument#

The print() function can accept additional arguments such as sep or end. These can help format a string appropriately for output. By default, the print function will print many objects separated by a comma.

# Print multiple objects with a single print() statement
string1 = 'Hello'
string2 = 'World'
string3 = '!'

print(string1, string2, string3)

Notice that the print() function defaults to include a single space separator between the objects it prints. We can change this default separator by using the sep parameter.

# Use a plus as a separator
print(string1, string2, string3, sep='+')

We can even remove the separator by specifying an empty string.

# Specify an empty string for no separation
print(string1, string2, string3, sep='')

The print print() function also concatenates a new line by default. The is specified in the default argument end='\n'.

# Two strings printed on separate lines
print('Hello')
print('World')

Try it! < / >

Keeping both print() functions above, can you print the outputs on the same line?


String slices and methods#

String slices#

The characters of a string can also be indexed and sliced like the items of a list.

# Using a string index
string = 'Python Basics'
string[0]
# Slicing a string
string = 'Python Basics'
string[0:6]

We can use flow control on a string the same way we would with a list.

# Use a for loop on the string
# To print each character except any letter 'o'
string = 'Hello World'

Try it! < / >

Can you use a for loop to print each character of the string Hello World without printing the letter ‘o’?


String methods#

There are a variety of methods for manipulating strings.

Method

Purpose

Form

.lower()

change the string to lowercase

string.lower()

.upper()

change the string to uppercase

string.upper()

.join()

joins together a list of strings

‘ ‘.join(string_list)

.split()

splits strings apart

string.split()

.replace()

replaces characters in a string

string.replace(oldvalue, newvalue)

.rjust(), .ljust(), .center()

pad out a string

string.rjust(5)

.rstrip(), .lstrip(), .strip()

strip out whitespace

string.rstrip()

All of the characters in a string can be lowercased with .lower() or uppercased with .upper().

# Lowercase a string
string = 'Hello World'
string.lower()

These methods do not change the original string, but they return a string that can be saved to a new variable.

# The original string is unchanged
print(string)

# The returned string can be assigned to a new variable
new_string = string.upper()
print(new_string)

A string can be split on any character, or set of characters, passed into .split(). By default, strings are split on any whitespace including spaces, new lines, and tabs.

# Splitting a string on white space
string = 'This string will be split on whitespace.'
string.split()
# Splitting a phone string based on the '-' character
phone_string = '313-555-3434'
phone_string.split('-')

Similarly, lists of strings can be joined together by passing them into .join(). A joining string must be specified before the .join(), even if it is the empty string ''.

# List of strings joined together
name_list = ['Sam', 'Delilah', 'Jordan']
', '.join(name_list)

The .strip() method will strip leading and trailing whitespace (including spaces, tabs, and new lines) from a string. Remember, these changes will not affect the original string, but they can be assigned to a new variable.

# Stripping leading and trailing whitespaces from a string
string = '    Python Basics '
string.strip()

It is also possible to only strip whitespace from the right or left of a string.

# Stripping leading whitespace from the leftside of a string
string = '    Python Basics '
string.lstrip()

Characters in a string can be replaced with other characters using the .replace() method.

# Replacing characters in a string with .replace()
string = 'Hello world'
string.replace('l', 'x')
# Removing characters from a string
# using .replace with an empty string
string = 'Hello! World!'
string.replace('!', '')

Finally, strings can be justified (or padded out) with characters leading, trailing, or both. By default, strings are justified with spaces but other characters can be specified by passing a second argument.

# Left justifying a string
string1 = 'Hello'
string2 = 'world!'

print(string1.ljust(10) + string2)
# Left justifying a string with pluses
string1 = 'Hello'
string2 = 'world!'

print(string1.ljust(10, '+') + string2)
# Right justifying a string
string1 = 'Hello'
string2 = 'world!'

print(string1 + string2.rjust(10))
# Center a string
string = 'Hello world!'

print('|' + string.center(20) + '|')
# Center a string
string = 'Hello world!'

print('|' + string.center(20, '+') + '|')
# Printing a dictionary of contacts in neat columns
contacts ={
 'Amanda Bennett': 'Engineer, electrical',
 'Bryan Miller': 'Radiation protection practitioner',
 'Christopher Garrison': 'Planning and development surveyor',
 'Debra Allen': 'Intelligence analyst'}

print('Name', 'Occupation')
for name, occupation in contacts.items():
    print(name, occupation)

Try it! < / >

Can you clean up the printing of the contacts dictionary?


Checking string contents#

There are a variety of ways to to verify the contents of a string. These return a Boolean True or False and are useful for flow control. For example, we can check if a particular set of characters is inside of a string with the in and not in operators. The result is a Boolean True or False.

# Check whether a set of characters can be found in a string
string = 'Python Basics'
'Basics' in string

The following string methods also return Boolean True or False values.

Method

Purpose

Form

.startswith(), .endswith()

returns True if the string starts/ends with another string

string.startswith(‘abc’)

.isupper(), .islower()

returns True if all characters are upper or lowercase

string.isupper()

.isalpha()

returns True if string is only letters and not blank

string.isalpha()

.isalnum()

returns True if string only letters or numbers but not blank

string.alnum()

.isdigit()

returnsTrue if string is only numbers and not blank

string.isdigit()

# Checking if a string starts 
# with a particular set of characters

string = 'Python Basics'
string.startswith('Python')
# Checking if a string is lowercased
string = 'python basics'
string.islower()
# Checking if a string is alphabet characters
string = 'PythonBasics'
string.isalpha()
# Checking if a string only
# alphabetic characters and numbers
string = 'PythonBasics5'
string.isalnum()
# Checking if a string is only numbers
string = '50'
string.isdigit()

The .isdigit() method checks each character to verify it is a digit between 0-9. It will return false if there is a negative (-) or decimal point (.) character.


Coding Challenge! < / >

Use flow control on the staff list below to print the name of every person with the first name ‘Patricia’ or the the last name ‘Mitchell’


# A list of staff members
staff = ['Tara Richards',
 'Tammy French',
 'Justin Douglas',
 'Lauren Marquez',
 'Aaron Wilson',
 'Dennis Howell',
 'Brandon Reed',
 'Kelly Baker',
 'Justin Howard',
 'Sarah Myers',
 'Vanessa Burgess',
 'Timothy Davidson',
 'Jessica Lee',
 'Christopher Miller',
 'Lisa Grant',
 'Ryan Chan',
 'Gary Carson',
 'Anthony Mitchell',
 'Jacob Turner',
 'Jennifer Bonilla',
 'Rachel Gonzalez',
 'Patricia Clark',
 'Richard Pearson',
 'Glenn Allen',
 'Jacqueline Gallagher',
 'Carlos Mcdowell',
 'Jeffrey Harris',
 'Danielle Mitchell',
 'Sarah Craig',
 'Vernon Vasquez',
 'Anthony Burton',
 'Erica Bryant',
 'Patricia Walker',
 'Karen Brown',
 'Terri Walker',
 'Michelle Knight',
 'Kathleen Douglas',
 'Debbie Estrada',
 'Jennifer Brewer',
 'Taylor Rodriguez',
 'Lisa Turner',
 'Julie Hudson',
 'Christina Cox',
 'Nancy Patrick',
 'Patricia Mosley',
 'Nicholas Gordon',
 'Wanda Vasquez',
 'Jason Lopez',
 'Anna Mitchell',
 'Tyler Perez']

Coding Challenge! < / >

Print all the first names in the staff list. The .split() method would be useful.


# Print all the first names in the staff list

Coding Challenge! < / >

Use flow control on the staff list to add all of the staff list to the colleagues list below. You will need to split the first and last names apart using the .split() method. You will also need to create a dictionary for each entry in the colleagues list and append the dictionary to colleagues list. Print out the first names of all colleagues.


colleagues = [
    {'first_name': 'Ada', 'last_name': 'Lovelace'},
    {'first_name': 'Charles', 'last_name': 'Babbage'}
]

# Print the first name of the first colleague
print(colleagues[0]['first_name'])

Coding Solutions#

Here are a few solutions for exercises in this lesson.


# Using a for loop on a string
string = 'Hello World'

for character in string:
    if character != 'o':
        print(character, end='')
# Printing a dictionary of contacts in neat columns
contacts ={
 'Amanda Bennett': 'Engineer, electrical',
 'Bryan Miller': 'Radiation protection practitioner',
 'Christopher Garrison': 'Planning and development surveyor',
 'Debra Allen': 'Intelligence analyst'}

print('Name'.ljust(22), 'Occupation')
print('|'.center(44, '-'))
for name, occupation in contacts.items():
    print(name.ljust(20), '|', occupation)
# Print all staff names with the first name Patricia or last name Mitchell
for name in staff:
   if name.startswith('Patricia') or name.endswith('Mitchell'):
        print(name)
# Print all the first names in the staff list
for name in staff:
    print(name.split()[0])
# Add the first and last names of the staff list to the colleagues list

colleagues = [
    {'first_name': 'Ada', 'last_name': 'Lovelace'},
    {'first_name': 'Charles', 'last_name': 'Babbage'}
]

# Add staff names to colleagues list
for name in staff:
    colleague_dict = {}
    first_name = name.split()[0]
    last_name = name.split()[1]
    colleague_dict['first_name'] = first_name
    colleague_dict['last_name'] = last_name
    colleagues.append(colleague_dict)

for entry in colleagues:
    print(entry['first_name'])
    
# Verify all the colleagues were added    
#from pprint import pprint
#pprint(colleagues)

Attribution Creative Commons CC BY License

Created by Nathan Kelber and Ted Lawless for JSTOR Labs under Creative Commons CC BY License