No history yet

Python Basics

Getting Started with Python

Before you can write Python code, you need the Python interpreter. Think of it as a translator that converts your Python commands into instructions the computer understands. The best way to get it is from the official Python website, python.org. The installation is straightforward, and it comes with a basic code editor called IDLE that's perfect for starting out.

Lesson image

Once it's set up, you can start writing your first program. By tradition, the first program many people write in a new language is one that simply displays "Hello, World!" on the screen. In Python, this is incredibly simple.

print("Hello, World!")

That's it. The print() command is a built-in function that outputs whatever you put inside the parentheses. The text inside the quotes is called a string, which is just a sequence of characters. Unlike many other languages, Python doesn't require a semicolon at the end of each line. It relies on clean, readable syntax.

One of Python's most important syntax rules is indentation. Where other languages might use brackets or keywords to group code, Python uses whitespace. This forces code to be organized and easy to read.

Variables and Data Types

Imagine you have a box. You can put something in the box and label it so you know what's inside. In programming, a variable is like that labeled box. It's a name that refers to a value stored in the computer's memory.

variable

noun

A storage location in a computer's memory that has a name and contains a value.

Creating a variable in Python is as simple as choosing a name and assigning it a value using the equals sign =.

name = "Alice"
age = 30

print(name)
print(age)

Here, name and age are variables. The information they hold comes in different types, or data types. Python automatically figures out the data type based on the value you assign.

  • String (str): Text, like "Alice".
  • Integer (int): Whole numbers, like 30.
  • Float (float): Numbers with a decimal point, like 98.6.
  • Boolean (bool)': Represents True or False. Useful for logic and decision-making.

Using Operators

Operators are special symbols that perform operations on variables and values. You already know many of them from math class.

Arithmetic operators let you perform mathematical calculations.

OperatorNameExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%Modulusx % y (remainder)
**Exponentiationx ** y (xyx^y)

Comparison operators, as the name suggests, compare two values and return a Boolean (True or False) result.

x = 10
y = 5

print(x > y)  # Is x greater than y? Prints True
print(x == 10) # Is x equal to 10? Prints True
print(y != 5) # Is y not equal to 5? Prints False

Notice the double equals sign == for checking equality. A single equals sign = is for assigning a value to a variable.

Interacting with the User

So far, we've only displayed information. What if you want to get information from the user? For that, Python provides the input() function. It pauses the program and waits for the user to type something and press Enter.

# The text inside input() is the prompt shown to the user
user_name = input("What is your name? ")

# We can use the variable in our output
print("Hello, " + user_name + "!")

One important thing to know: the input() function always returns the user's input as a string. If you ask for a number and want to do math with it, you'll need to convert it first. You can use int() to convert to an integer or float() to convert to a float.

age_string = input("How old are you? ")
age_number = int(age_string)

years_in_a_decade = 10

age_in_ten_years = age_number + years_in_a_decade

print("In a decade, you will be " + str(age_in_ten_years) + " years old.")

In the final line, we had to convert age_in_ten_years back into a string with str() before we could join it with the other text for printing. You can only combine data of the same type this way.

Time to check your understanding of these core concepts.

Quiz Questions 1/6

What is the primary role of the Python interpreter?

Quiz Questions 2/6

Which line of code will correctly display the message Success! on the screen?

You've now covered the absolute fundamentals: setting up your environment, writing basic commands, storing information in variables, and interacting with a user. These are the essential building blocks for everything that comes next.