Skip to main content
Chemistry LibreTexts

2.2: Input and Variables in Python

  • Page ID
    204332
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Hypothes.is tag:  s20iostpy02ualr

    Download Assignment F2020py02
    (if file opens in browser window right click and choose "save link as...")

    Learning Objectives

    Students will be able to:

    Content:

    • Explain how to input data in 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

     

    Note

    This activity asks you to execute lines of Python Code.  Start the Thonny IDE. In the Shell window you can type individual command lines, or in the editor window you can type a whole program and run it.

    clipboard_ea484ceb0619904f91108cf12f130637a.pngFigure \(\PageIndex{1}\):  Thonny GUI showing the three most common displays you will be using.

     

     

    Model 1: Input in Python

    Input, one of the four main operations of a computer, is performed using an input statement in Python. The prompt that appears on the screen to tell the user what to enter is included as a string literal  in the parentheses of the input statement.

    Input() and print() are known as functions in python.  In python 3 functions are followed by ( ), and inside the () are

     

    Flowchart Python Program
    Flow chart for input output functions

     

    # Programmer: Monty Python

    # Date: Sometime in the past

    # Description: A program that introduces variables

    name = input("What is your name? ")
    print("Your name is", name)
    

    Critical Thinking Questions:

    1. Enter and execute the Python program using the editor window in Thonny. To run the program when you are done typing you can either press F5, or click the green arrow (figure \(\PageIndex{1}\)) . What is printed in the shell window when the Python program is executed?
       
    2. Draw a line between each flowchart symbol and its corresponding Python code.
       
    3. Examine the first line of Python program:   name = input("What is your name? ")

       a.    What happens when this line of code is executed?

       b.   What appears in the console window when this line of code is executed?

    FYI:

    The words that appear on the screen to tell the user what to enter is known as a prompt.

      c.   Where is the data stored when the user enters something after the prompt and presses the Enter button?

    4.    Notice that the word name is in both lines of code. Is the string literal "name" printed when the second line of Python code is executed? What is printed?


     

    Model 2: Variables in Python

    The word name in the Python code is a variable – the value of the variable name is given to a memory location used to store data.

     

    name = input("What is your name? ")
    print("Your name is",name)
    name = input("What is your name? ")
    print("Your name is",name)
    

    5.   What happens when you execute each of the following lines of Python code?

    Hint, edit the above code, don't rewrite from scratch.  The idea here is to figure out how a variable name can be written (there are rules for naming variables and you need to try and figure out some of the basic ones, so try each of these in turn)

          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?")
    

    6.   Examine the errors that occurred when executing the lines of code in question 5.  In 5d did you notice how the word from (like print and input above) became purple?  In Thonny (it is a function), so if you are typing a word and it turns purple, that word is a function, and you can't use it as a variable name.

    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?")

          What are the rules for creating a valid variable name?

     

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

    Variable name Comments about variable name
    price  
    costoffirstitem  
    Ic  
    firstName  

     

    Note: Printing multiple items and concatenation

    You can  concatenate two strings literals, two string variables or a string literal and a string variable using the "+" symbol. The strings can be string literals or a variable containing string literals. To print multiple values without being concerned whether they are strings, you can use a "," comma.

    8.  Predict the output and execute the following lines of code. 

    name = input("What is your name? ")
    print("Your name is",Name)
    

     

       a.  Is the output what you would expect? Why or why not?

       b. How can you alter the code so that it functions properly?

    WORK

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

    f

    Exercise \(\PageIndex{1}\)

    Predict the results of each print statement and then using either the Thonny python shell on your computer or the Binder access to the LibreText Jupyter Hub validate your answers (you can cut and paste each question into the IDE/binder, the first one is already loaded)

    print("Your name is", "Pat.")
    print('Your name is', "Pat.")
    print("Your name is" + "Pat.")
    print("Your name is", 20)
    print("Your name is" + 20)
    1.  
    2. print (2*5)
    3. print ('2+5')
    4. print ("Age:",7)
    5. print ('Age:',2+5, "years")
    Answer a

    Answer b

    10 

    Answer c

    2+5

    Answer d

    Age: 7

    Answer e

    Age: 7 years

    f

    print("Your name is", "Pat.")
    print('Your name is', "Pat.")
    print("Your name is" + "Pat.")
    print("Your name is", 20)
    print("Your name is" + 20)
    

          a.   State the output for each of line of code.
          b.  What is the difference between the first two print statements? 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? 

    10. Execute the following program:

    name = input("Enter name:")
    ID=input("Enter your student ID number: ")
    course = input("Enter your course number: ")
    print(name +"'s ID is" + ID + "\nand is enrolled in " + course)
    

     

        a.  State what is displayed on the screen when you executed the program.
        b.  The \n is a "string literal escape sequence" in python. What does this do?
        c.  Change the \n to \t and describe what happens to the program output.

     


    Application Questions: Use the Thonny IDE to check your work

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

    2.   Provide an example of a string literal.

    3.   Provide an example of a numeric literal.

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

     


    Individual Homework Activity

    Write a Python program that prompts the user for a name of an element and its atomic number. See a sample output for Hydrogen below:

    clipboard_e4f738d01d360f1d874c292cc92620b58.png

    Be sure your output has the same punctuation as the above output.

     

    (8 pts)

    • Your program must contain documentation lines that include your name, the date, a line that states "Py02 Homework question 1" and a description line that indicates what the program is supposed to do. 
    • Paste the code to a copy of this end of the word document of this homework assignment and upload to your Google drive, with file name [your last name]_py02_HW
    • Save the program as a python file (ends with .py), with file name [your last name]_py02_program and upload that to the Google Drive.

    (2 pts)Make 1 annotation on any open access webpage dealing with the the use of Thonny.  Post them to the 2020sIOST group and tag them s20iostpy02ualr.  Remember the tags are case sensitive, and everything is lower case.

     

    Copyright Statement

    cc4.0
    This work  is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, this material is a modification by Ehren Bucholtz and Robert Belford of CS-POGIL content, with the original material developed by Lisa Olivieri, which is available here.


    This page titled 2.2: Input and Variables in Python is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?