Lesson 2: Hello World & Input

Module 1: Getting Started & I/O · about 10 min · free

Now that you've got the basics, let's look at our first code. Open PyCharm or VS Code and let's check out some basic commands in Python.

Command print()

This is how we print anything in Python. You write print() and what you want to print you put in the round brackets.

name = "Peter" #String is always in quotation marks
print(name) #Prints content of variable name
print(type(name)) #Prints datatype of variable name

Output

Peter
<class 'str'>

The input() Function

The input() function serves to load input from the user via the keyboard. After invoking input(), the program pauses and waits until the user writes text and presses Enter. Inside the round brackets, we can put a text message that will be shown to the user as instructions.

name = str(input("Whats your name? "))
age = int(input("How old are you? "))
print(name, "is", age, "years old")
print(f"{name} is {age} years old")

Output

Whats your name? Max
How old are you? 18
Max is 18 years old
Max is 18 years old

As you can see, we used something called an f-string. It is a string that allows us to embed variables, expressions, and calculations into a string through curly braces.

Practice Exercise

Now, try to write a program that will continuously ask the user for their first name, last name, and birth year, and then prints: "[first name] [last name] was born in [birth year]".

Reveal Answer

firstName = input("What is you first name? ")
lastName = input("What is you last name? ")
birthYear = input("In what year were you born? ")
print(f"{firstName} {lastName} was born in {birthYear}")

Output

What is you first name? Filip
What is you last name? Novak
In what year were you born? 2006
Filip Novak was born in 2006

Want XP, levels and progress tracking? Play this lesson in the CodeStart game.

Play Lesson 2