Variables, Data Types and If statements Practice#

This notebook provides practice problems. It is divided into several parts:

  • Part 1 - most approachable place to start. Practices syntax and concepts

  • Part 2 - a step up from part 1. Integrates concepts from accross the lesson in more applied scenarios

  • Intermediate Reading - some suggested reading for a more in depth understanding of some of the foundational concepts discussed in the lesson. I would recommmend this only for folks that started SARP feeling competent with Python.

Practice Problems Part 1#

Assigning Variables#

  1. Create a variable called location and assign it the value: Quito, Ecuador. Print your variable.

  1. Create two variables that are numbers and print the result of one of them divided by the other.

  1. What do you expect to be the output of the following block of code?

my_fav_icecream = 'mint'
your_fav_icecream = 'blue moon'
my_fav_icecream = 'peanut butter cup'
print(my_fav_icecream)

Data Types#

  1. What are the data types of each of the following values?

  1. True

  2. 832

  3. 0.001

  4. 'giant kelp'

  5. "false"

  6. "5"

Check that you are correct using the type() function, as shown below.

type('cheese')
str

Booleans#

  1. What is a boolean? When might a boolean be useful?

  1. The following cell represents some data you are working with.

cloud_cover = 15
city = 'Oshkosh'
cfc_present = True
ozone = 3.8

Run the cell above, and use the variables to individually check for each of the following conditions:

  • cloud cover is less than 90

  • city is not Minneapolis

  • there are no cfcs present

  • ozone is greater than or equal to 2

  1. Explain the difference between = and ==.

Logical Operators#

  1. How is and different from or?

  1. Fill out the values for p and q and p or q in the following table, given each combination of the possible values for p and for q.

p

q

p and q

p or q

True

True

True

False

False

True

False

False

  1. For each of the following scenarios do you think it would make more sense to use and or or?

  • You have several images and you want to make sure that at least one of them has the wavelength you need.

  • You have an algorithm which requires three variables in a specific range and you want to check those three variables in your image to make sure the image will work.

  • In a particular dataset you are looking at invalid results have the value -999. You also know that values above 1000 are not suitable for your use case. You need to check the dataset to make sure that neither of those things are true.

  1. Write a statement using the following operators: and, !=

  1. Write a statement using the following operators: or, +

If statements#

  1. Write an if statement that uses the following elements:

  • if, <=, or

  1. You have species data with the variable family and you would like to assign the species to a group based on the family. The groups are:

  • Group A - pinaceae, lamiaceae

  • Group B - aceraceae, salicaceae

Write a statement that checks the familyvariable and informs the user if the species belongs in group A or group B.

Practice Problems Part 2#

Question 1#

Write some code to take the mean value of the three sun angles below.

sun_angle1 = 45
sun_angle2 = 32
sun_angle3 = 67
# Take the mean
# Output the mean to the user

Question 2#

A) Google “Python operators”. Find a webpage with some operators and use two of them below. If you are having a hard time finding a webpage you can use this one, which is the first page that shows up when I run the search.

B) Fill in the # Your code here space below to compute the area of a circle given the radius. You can approximate the value of pi with 3.14.

radius = 4
# Your code here

C) Calculate 2 to the 8th power. Assign it to a variable and print the output. Include a string in the print statement that says: 2 to the 8th power is:

Question 3#

What is the output of the following block of code?

updated_temp = 72
print(updated_temp)
updated_temp = updated_temp + 5
updated_temp = updated_temp + 2
updated_temp = updated_temp -9
print('updated_temp')

Question 4#

Use a comparison (less than, equal to, etc.) to determine if the result of 3 to the power of some number (the input variable exponent) is between 100 and 1000.

Question 4#

cloud_cover = 15
city = 'Oshkosh'
cfc_present = True
ozone = 3.8

A) Use the variables in the cell above and check that the following conditions are true:

  • Cloud cover is less than 90

  • city is not Minneapolis

  • There are no CFCs present

  • ozone is greater than or equal to 2 and above 0.5

Print a message to the user informing them if all of the conditions were true or not.

B) Using the same variables and conditions as in part A, change your code so that it checks if any of the conditions are true.

Question 5#

As a variable, wind is often expressed as two different numbers, which together make up the complete wind vector:

  1. wind speed

  2. wind direction

Wind speed is expressed in degrees and follows the following quadrant:

Note that 0 degrees is on top and the degrees increase going clockwise. Use this knowledge to work through the following problems.

A) Sometimes wind directions are conveyed as greater than 360 degrees. Numbers above 360 indicate a second loop around the quadrant. For example:

  • 450 degrees is the same as 90 degrees

  • 720 degrees is the same as 0 degrees

Given wind directions that are greater than 360, write some code to return the wind direction expressed between 0 and 360. Check your code works against both of the numbers below.

large_wind_direction = 512
large_wind_direction = 750

B) Write a statement to check if a given wind direction is coming from the west.

wind_direction = 145

C) Write a block of code that takes a wind direction and prints back to the user the direction the wind is coming from. You can use a range of values for each direction (i.e. “east” means wind coming from between 45 and 135 degrees).

C) Monsoon winds in some regions are identified by winds coming from the Northwest (+/- 30 degrees) at greater than or equal to 20 mph. Write a statement to evaluate if the wind direction/wind speed combination would qualify as a monsoon-type wind.

wind_speed = 22
wind_direction = 43

Question 6#

Given a temperature in fahrenheit, write an equation to convert the temperature to celsius. Print out a statement after converting to celsius informing the user whether or not the temperature has reached boiling point of water.

Intermediate Reading#

The following articles are suggested for learning about intermediate Pythonic nuances and data types. They are more advanced looks at some of the topics covered in the lecture.