Python Basics: Your First Programs
What Is Python? Your First Program
In this lesson you will
- Explain what Python is and where it is used
- Print output and write comments
- Create variables and name the basic types
Introduction
Python is one of the most popular programming languages in the world. It is used for websites, data analysis, automation, science and artificial intelligence. It is also famous for being readable: Python code looks a lot like plain English, which makes it a great first language.
The simple explanation
A program is a list of instructions the computer follows one after another. Python is the language you write those instructions in.
Press Run on this program. The first time, your browser downloads Python itself (about 10 MB), so wait a few seconds. After that, programs run quickly.
print() shows something on the screen. The text inside the quotation marks is called a string. Change the message and run it again.
Quick quiz
What does print('Hi') do?
Several instructions
Python runs your lines from top to bottom, in order.
Comments
A # starts a comment: a note for humans that Python ignores. Use comments to explain why you wrote something.
Variables
A variable is a name for a value, like a labelled box. You create one with =, which means "store the value on the right under the name on the left".
You can change a variable later:
Variable names use lower case letters and underscores, like first_name. They cannot start with a digit or contain spaces.
The basic types
Every value has a type. Use type() to see it.
Python decides the type from the value, and it can change when you reassign a variable. Types matter because they decide what you can do: you can add numbers, and you can join text, but mixing them needs care.
Run it and read the error at the bottom, which says what went wrong. Reading errors is a core programming skill. The last line of a Python error is the most useful one.
Quick quiz
What is the type of the value 3.14?
Asking the user for input
Python has an input() function that asks the user to type something. It does not work in the editors on this page, since there is no keyboard prompt, but on your own computer you can use it like this:
Python
name = input("What is your name? ")
print("Nice to meet you,", name)Try it yourself
Create variables for your name and your favourite number, then print one sentence that uses both, such as Asha's favourite number is 7. Hint: print(name, "likes", number) prints values separated by spaces.
Summary
- Python runs your lines from top to bottom.
print()shows output, and#starts a comment.name = valuecreates a variable.- The basic types are
str,int,floatandbool.type()reveals a value's type.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading