Biocomputing Fall 2018, Rowan University

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 function len() 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

  1. 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.

  2. Modify the above if/else statement to write an if/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.

  3. 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.

  4. Define a variable animal = "python". This type of variable is a string, meaning it is made of characters and defined with quotation marks. Write an if/elif/else statement to determine if the there are more than 10 letters in the variable animal (Hint: use the len() function!). Have your code print an informative message.

  5. 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)