Comments, keyboard input, and naming variables
Moving on from the first program with input and output, we’ll now consider how to use Comments, Keyboard Input, and Naming Variables in Python.
We will look at the following areas of Python:
- Comments - using #
- Input - using the built-in function input()
- Naming variables and PEP 8
- A program using some of the above
- A comment in Python starts with a hash #
- Everything after the hash is ignored by the Python interpreter
- A comment is used to explain your program to another programmer (or yourself coming back to it later)
- Comments are helpful in making code clear and easy to understand
Examples of using comments:
Comments start with # and the Python interpreter ignores them. They are messages for people reading the code.
# Calculate the sum of four and fifty-six s = 4 + 56 # Create a variable containing a name Name = “Simon Jenkins”
- The built-in function input() lets keyboard input be stored in a variable
- The syntax is x = input("Please enter your name")
- The program will then print the prompt ("Please enter your name") and wait for something to be typed. It will then assign what is typed to the variable x
- You choose the prompt and the variable name
- input() always gives a string. Even if you type a number, the data type of the variable is str.
- This means you may have to cast to a different data type if you need a number
Examples of the input built in function, collecting keyboard input and storing it in a variable.
If you try each example below, and use the type() function to see what data type the variables are, each of name, age and filename will be class str.
name = input('Please type in your name: ') print('Hello', name) age = input('Give your age in years: ' ) filename = input('Which file do you need? ')
Figure 1: The built-in function input() for getting keyboard input and storing it in a variable. You choose the variable name, and a helpful prompt to explain what the user should type. The input function always stores what the user types in a variable of type str (string).
Naming variables and PEP 8
- A variable name is chosen by the programmer
- Variable names are case sensitive, so Age, AGE and age are all different
- Variable names must start with a letter or an underscore, and contain only letters, underscores or digits
- Programmers are encouraged to use meaningful variable names to help code make sense to the reader. So, income tax is a more useful variable name than x.
- Python was designed to be clear and easy to read to make it easier, for developers, to add new programme features and to fix errors. Giving variables sensible names helps people to read and understand a program.
- The Python style guide PEP 8 gives advice on every aspect of writing Python. PEP 8 is not about making programs work, or correct syntax, it’s about good style, and making programs easy to read and adapt. Read more at: https://www.python.org/dev/peps/pep-0008/
A program using some of the above
We can now read in data from the keyboard and do calculations with it. Examples of this can be found in the coding videos, and in the lab for this chapter. A further example can be viewed below:
Example code: a Python program to convert miles to kilometers.
Remember, anything after a hash is a comment and ignored by the Python interpreter.
# Program to calculate km from miles # 1 mile is 1.609 km # # Use run/run module to run # Ask how many miles they want to convert miles = input("How many miles ") # the variable miles is type string, because it came from the input() function. # We need to calculate with it, so convert miles to a number miles = float(miles) # do the calculation km = miles * 1.609 # print the answer print(miles,"miles is",km,"km")
Summary - Comments, keyboard input, and naming variables
- Python comments start with # and are notes to help programmers, they’re ignored by the Python interpreter
- The built-in function input asks for keyboard input and stores it in a variable
- Variable names start with a letter or underscore, and contain letters, digits or underscores
- Variable names should make sense in the context of the program. This makes the program easier to understand
- Changes to the Python language are summarised and discussed in PEPs ’Python Enhancement Proposals’. PEP 8 is about code style, writing clear and elegant Python code, and includes advice on variable names
Now you've completed this section of the Course, it is time to check your knowledge by answering some quick questions before moving on to the next section where you will watch two videos about machine learning and decisions in Python.
In this section, you will explore data types, variables, input and output functions, and discover more about calculations and mathematical operators in Python.
A world-leading tech and digital skills organization, we help many of the world’s leading companies to build their tech and digital capabilities via our range of world-class training courses, reskilling bootcamps, work-based learning programs, and apprenticeships. We also create bespoke solutions, blending elements to meet specific client needs.