Python Activity 2: Input and Variables

Learning Objectives

Students will be able to:

  • Explain how to input data using Python
  • Explain the meaning and purpose of a variable
  • Determine if a variable name is valid
  • Explain concatenation and the use of “+" Process:
  • Create input statements in Python
  • Create Python code that prompts the user for data and stores it in a variable
  • Create valid and good variable names
Prior Knowledge
Material covered in Activity 1
Critical Thinking Questions:


1. Enter and execute the Python program. What is printed on the screen when the Python program is executed?

FYI: input() and print() are functions in Python
2.Examine the first line of Python program:
name = input("What is your name?") 
  • a. What appears on the screen when this line of code is executed?
FYI: The words that appear on the screen and tell the user what to enter are known as a prompt.
name = input("What is your name?") 

The word name in the Python code is a variablea name given to a memory location used to store data.

  • b. What happens to the data the user entered?
3. Explain the errors that occur when you execute each of the following lines of Python code.
  • a. name? = input(“What is your name?”)
  • b. your name = input(“What is your name?”)
  • c. 1st_name = input(“What is your name?”)
  • d. from = input(“Where were you born?”)
4. Examine the errors that occurred when executing the lines of code in question 3. Then examine the following lines of valid code.
name2 = input("What is your name?")
your_name = input("What is your name?")
yourName = input("What is your name"?)

List the rules that you need to follow to create a valid variable name.

5. Are the following variable names valid? Are they good names? Why or why not?

Variable name Comments about variable nameSorted ascending
6. Execute the following lines of code. Is the output what you would expect? Why or why not?


7. Use the following set of Python statements to answer the questions below.

  • a. State the output for each of line of code.
  • b. How are the first two print statements different? Does the difference affect the output?
  • c. Notice that some statements include a comma (,) between the two literals being printed and some statements use a “+”. Do they produce the same output?
  • d. Explain the purpose of the comma.
  • e. Why does the last print statement crash the program? What would you do to correct it?
FYI:“+” concatenates two strings. The strings can be string literals or a variable containing string literals.
8. State what is displayed on the screen when the following program is executed:

9. What caused the output in the print statement in question 8 to be printed on more than one line?

Application Questions: Use the Python Interpreter to input your code and check your work

1. State a good variable name for an employee’s ID number.

2. Write a line of Python code that prompts the user for the name of their favorite ice cream and stores it in a valid variable name.

3. Crazy Sentence Program. Create a program that prompts the user for the name of an animal, a color, the name of a vehicle, and the name of a city. Then print a sentence that contains the user input in the following order. Include the additional words in the sample output as part of your output. Example: Assume the user enters the words: tiger, green, motorcycle, and Wildwood. The output would be: The green tiger drove the motorcycle to Wildwood.

Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng Screen_Shot_2018-07-05_at_1.38.40_PM.png r1 manage 17.7 K 2018-07-05 - 17:39 JimSkon  
PNGpng Screen_Shot_2018-07-05_at_2.12.12_PM.png r1 manage 27.8 K 2018-07-05 - 18:14 JimSkon  
PNGpng Screen_Shot_2018-07-05_at_2.12.22_PM.png r1 manage 46.3 K 2018-07-05 - 18:14 JimSkon  
PNGpng Screen_Shot_2018-07-05_at_2.20.06_PM.png r1 manage 35.2 K 2018-07-05 - 18:20 JimSkon  
Topic revision: r1 - 2018-07-05 - JimSkon
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback