Python Helsinki: 1

Introduction to Programming: Part 1

Variables and conditionals

Some notes

Input refers to any information a user gives to the program. Specifically, the Python command input reads in a line of input typed in by the user. It may also be used to display a message to the user, to prompt for specific input.

Commenting

Any line beginning with the pound sign #, also known as a hash or a number sign, is a comment. This means that any text on that line following the # symbol will not in any way affect how the program functions. Python will simply ignore it.

Comments are used for explaining how a program works, to both the programmer themselves, and others reading the program code. In this program a comment explains the calculation performed in the code:

print("Hours in a year:")
# there are 365 days in a year and 24 hours in each day
print(365*24)
Choosing a good name for a variable

It is often useful to name variables according to what they are used for.

For example, if the variable contains a word, the name word is a better choice than, say, a.

There is no set limit to the length of a variable name in Python, but there are some other limitations. A variable name should begin with a letter, and it can only contain letters, numbers and underscores _.

Lowercase and uppercase letters are different characters. The variables name, Name and NAME are all different variables. While this rule has a few exceptions, we will ignore those for now.

It is a common programming practice in Python to use only lowercase characters in variable names. If the variable name consists of multiple words, use an underscore between the words. While this rule also has a few exceptions, we will ignore those for now.

Printing with f-strings

What if we want to have more flexibility and control over what we print out? So called f-strings are another way of formatting printouts in Python. The syntax can initially look a bit confusing, but in the end f-strings are often the simplest way of formatting text.

With f-strings the previous example would look like this:

result = 10 * 25
print(f"The result is {result}")

Let’s break this apart. In the very beginning of the string we are printing out there is the character f. This tells Python that what follows is an f-string. Within the string, enclosed in curly brackets, is the variable name result. The value it contains becomes a part of the printed string. The printout is exactly the same as in the previous examples:

Sample output

The result is 250

Numbers as input

Usually you do not need to create two separate variables (like input_str and year above) to read a number value from the user. Instead, reading the input with the input function and converting it with the int function can be achieved in one go.

Similarly, a string can be converted into a floating point number with the function float. This programs asks the user for their height and weight, and uses these to calculate their BMI:

year = int(input("Which year were you born? "))
print(f"Your age at the end of the year 2021: {2021 - year}" )

height = float(input("What is your height? "))
weight = float(input("What is your weight? "))

height = height / 100
bmi = weight / height ** 2

print(f"The BMI is {bmi}")

Sample output

Which year were you born? 1995
Your age at the end of the year 2021: 26

What is your height? 163
What is your weight? 74.45
The BMI is 28.02137829801649

Conditional statements

Indentation

Python recognises that a block of code is part of a conditional statement if each line of code in the block is indented the same. That is, there should be a bit of whitespace at the beginning of every line of code within the code block. Each line should have the same amount of whitespace.

a = 3
condition = a < 5
print(condition)
if condition:
    print("a is less than 5")

Sample output

True
a is less than 5
points = int(input("How many points are on your card? "))
if points < 100:
    points *= 1.1
    print("Your bonus is 10 %")

elif points >= 100:
    points *= 1.15
    print("Your bonus is 15 %")

print("You now have", points, "points")

Sample output

How many points are on your card? 55
Your bonus is 10 %
You now have 60.5 points
Back to top