1. Home
  2. Training Library
  3. Data Types and Variables |SDTL3 RS A3.1|

Comments, keyboard input, and naming variables |SDTL3 RS A3.1|

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

Comments

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

Copy code

1
# Calculate the sum of four and fifty-six

Copy code

1
s = 4 + 56

Copy code

1
# Create a variable containing a name

Copy code

1
Name = “Simon Jenkins”

Input

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

Copy code

1
name = input('Please type in your name: ')

Copy code

1
print('Hello', name)

Copy code

1
age = input('Give your age in years: ' )

Copy code

1
filename = input('Which file do you need? ')

The built-in function input() for getting keyboard input and storing it in a variable.

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.

Copy code

1
# Program to calculate km from miles

Copy code

1
# 1 mile is 1.609 km

Copy code

1
#

Copy code

1
# Use run/run module to run

 

Copy code

1
# Ask how many miles they want to convert

Copy code

1
miles = input("How many miles ")

 

Copy code

1
# the variable miles is type string, because it came from the input() function.

Copy code

1
# We need to calculate with it, so convert miles to a number

Copy code

1
miles = float(miles)

 

Copy code

1
# do the calculation

Copy code

1
km = miles * 1.609

 

Copy code

1
# print the answer

Copy code

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

 

Difficulty
Intermediate
Duration
43m
Students
10
Description

In this section, you will explore data types, variables, input and output functions, and discover more about calculations and mathematical operators in Python.

About the Author
Students
27534
Labs
123
Courses
1332
Learning Paths
45

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.