Python Tutorial: Level 0 Exercises

  1. Create a script that prints your first name, one letter at a time. Save the script as "yourname.py"
  2. Run the script. Do any variables show up in the Variable Editor?

BACK TO TABLE OF CONTENTS

  1. Divide 5/2 (integer format) and 5.0/2.0 (float format). Does python output the same values for these? (You might get a different answer depending on the version of python you are in). If you got a different answer for the two operations, explain why.

  2. What does the modulo operator (%) do? Try it out with a few numbers in this format: "x % y" to get an idea.

  3. What do these operators do: ** and //? Try them out with a few numbers in this format: "x // y" to get an idea.

  4. Does python follow order of operations? Try it out with a few numbers in this format: "a + b + c * d / e"

BACK TO TABLE OF CONTENTS

  1. Open "yourname.py". Edit the script so that each letter is labeled as a separate variable in this format: letter1, letter2, etc.

  2. Run the script. Do any variables show up in the Variable Editor?

  3. If there are not any repeated letters in your name: Create a final variable "letterX" that is the same as the first letter of your name. Run the script again. Print letterX and letter1 in the command line. Does python have a problem with two different variables having the same value?

  4. If there are repeated letters in your name: Relabel one of those variables as "letterX". Print the variables with the same value one after the other in the command line. Does python have a problem with two (or more) different variables having the same value?

  5. Give letterX a new letter that is not in your name. Print the new letterX and the other variable(s) that were previously all the same letter. Did changing the value of letterX change the value of the other variable(s)?

  6. Redefine letterX with another variable instead of a letter (e.g., letterX=letter1). Print letterX and letter1, one after the other. Now change the value of letter1 to "z". Print letterX and letter1, one after the other. Did changing the value of letter1 change the value of letterX? What does this tell you about python variable assignment?

BACK TO TABLE OF CONTENTS

  1. Are 1 and 1.0 equivalent? Are "1" and "1.0" equivalent? Why do you think this is?
  2. Are 5 and (3+2) equivalent?
  3. Write out the statements [Are 1 and 1.0 equivalent?] X [Are "1" and "1.0" equivalent?] X [Are 5 and (3+2) equivalent?] in proper Boolean syntax, in which you replace X with "and", "or", "and not", or "not". List 5 ways to get True as your output.

BACK TO TABLE OF CONTENTS

  1. Create a list called "oddlist", listing all of the odd integers between 0 and 10. Did oddlist become a variable?
  2. Print oddlist. If you get an error message, double check how you created your list.
  3. When you use the "len" function on oddlist, how long does python say the list is?
  4. When you use the "type" function on oddlist, what type of variable does python say oddlist is? If you get something other than a list, double check how you created your list and try again.
  5. Create a list called "intlist", listing all of the integers between 0 and 100 -- do not type them all out manually!
  6. Print intlist. Does it list all integers between 0 and 100? If not, double check how you created your list.

BACK TO TABLE OF CONTENTS

  1. Create a dictionary called "about_me" that contains the following information: your name (string format), age (float format), year of study (integer format), and favorite foods in a list (list of strings).
  2. Print about_me. If there are no error messages at this point, double check your variable with the "type" function to make sure you have made a dictionary.
  3. Check the length of about_me. How does python determine the length of a dictionary?

BACK TO TABLE OF CONTENTS

  1. Create an array called "mixnums" composed of 3 integers and 3 floats. Print the array. What has happened to the array?
  2. Create an array called "mixtypes" composed of 2 integers, 2 floats, and 2 strings. Print the array. What has happened to the array? What does python do to arrays with mixed types?
  3. Create an array called "oddarray" of all odd numbers between 0 and 100.
  4. Create an array called "logarray" of 16 numbers between 1 and 5 that follow a logarithmic distribution. These should not be integers.

BACK TO TABLE OF CONTENTS