Skip to main content

Age program

TL;DR

TL;DR stands for "Too Long; Didn't Read". It's a common abbreviation used online for when some text is too long and you can't be bothered reading it.

If you think you already know the basics of Python, read this page for a quick recap.

This chapter is here so that those of you who are more familiar with Python don't have to go through the more beginner-friendly explanations.

It will also be helpful once you've completed the entire section. You can come back to this page for a one-page recap of what you've learned!

If you don't think you already know the basics of Python, skip this page now by clicking here, and go straight to the next chapter.

A Python editor

For this course and for small-scale Python development and learning, I recommend repl.it. It's an online editor that's powerful, fast, and free.

Strings and numbers

Numbers in Python can be integers or decimals, known as int and float respectively. Strings are literal representations of characters, which can contain numbers or anything else.

You can use double or single quotation marks for strings, but you can't mix and match them (e.g. "Rolf' is invalid).

You can have the opposite quotation mark inside a string—only the outermost set of quotation marks signals a string. This is valid: "He told me you're awesome".

You can't add strings and numbers together, but you can add strings to strings and that joins them.

Variables

Variable names can contain underscores (_), numbers, and letters. They cannot start with a number.

Variables in Python are names for existing values.

You can use variables to refer to a value you created earlier on, and you can also change those values as you run through your program.

Output and Input

We can use print() to show something in a text console. We put whatever we want to show inside the brackets:

print("Hello, world!")

We can use input() to ask the user something and get a string back containing what they typed:

age = input("Enter your age: ")

Since we always get a string back, we may want to convert it into an integer so we can do maths on it:

age = input("Enter your age: ")
age_as_number = int(age)
print(age_as_number * 12)

You'll get an error if the user enters something that isn't a number.

You can also convert numbers to strings to you can add them to other strings:

age = input("Enter your age: ")
age_as_number = int(age)
months = age_as_number * 12
months_as_string = str(months)
print("Your age in months is: " + months_as_string)

String formatting

As of Python3.6, we can use f-strings to include non-string variables inside our strings.

months = 360
print(f"Your age in months is: {months}")

This has an f in front of the string, and then replaces {months} by the value of that variable (given it exists, of course!).

You can have multiple variables being replaced inside a single f-string.

Your first project

Create an application that asks the user for their age in years, and prints out their age in seconds.

Optionally, extend it so it prints out months, days, hours, and seconds.

Tackle this exercise here.