Introduction to Python
Comprehensive slides for ALL python found here.
Python Exercises
Part I: Working with numbers
Perform the following basic operations in Python:
- Add two numbers
- Multiply two numbers
- Assign a number to a variable and then print the variable
- Assign two numbers to two different variables, then assign the product of those two variables to a third.
- Print the third variable.
Part II: Logical comparisons
Perform the following basic operations in Python:
- Use python to determine if 2 is greater than 5
- Define two variables: x = 1 and y = 7. Use python to determine if x is less than y.
- Again use python to determine if x is less than y, but assign the result to a variable. Print the variable.
- Define the variable
x = "words"
. This is a string variable. Use the python functionlen()
to get the length of this variable. Hint:len(x)
. - Now, use python to determine if the length of
x
equals 12.
Part II: If statements
-
Define a numeric variable, and use an
if/else
statement to determine if the number is greater than zero. Your code should print a sentence indicating if the number is greater than zero or not. -
Modify the above
if/else
statement to write anif/elif/else
statement to determine if the variable is greater than, less than, or equal to zero. Again, print a sentence indicating the number’s value relative to zero. -
Define two numeric variables, and use
if/elif/else
statement to determine which variable is larger (hint: they might be equal!). Again, print a sentence indicating which value is larger. This sentence should include both variable values. -
Define a variable
animal = "python"
. This type of variable is a string, meaning it is made of characters and defined with quotation marks. Write anif/elif/else
statement to determine if the there are more than 10 letters in the variableanimal
(Hint: use thelen()
function!). Have your code print an informative message. -
In Texas, you can be a member of the elite “top 1%” if you make at least $423,000 per year. Alternatively, in Hawaii, you can be a member once you start making at least $279,000 per year! Finally, if you live in New York, you need to earn at least $506,000 a year to make the cut. Andrew is CEO of Big Money Company, and he earns $425,000 per year, and Stacey is CEO of Gigantic Money Company with an annual salary of $700,000. Use a series of
if
statements to determine, and print, whether Andrew and Stacey would be considered top 1%-ers in Texas, Hawaii, and New York each. For this task, you should:- Define specific variables for the elite thresholds
- Define specific variables for each person
- Compare the variables to one another (as opposed to directly comparing numbers)