No history yet

Python Basics

Getting Started with Python

Python is a popular programming language known for its clear and readable syntax. It's like writing in a structured form of English, which makes it a great choice for beginners. Before you can write Python code, you need a place to run it. This is called your environment.

You can install Python directly on your computer from the official website, python.org. Programmers often use an Integrated Development Environment (IDE) or a text editor to write their code. These tools provide features like syntax highlighting and debugging to make coding easier. For now, the simplest way to start is by using an online Python interpreter. This lets you write and run code directly in your web browser without any setup.

Lesson image

The Rules of the Road

Every language has rules, and programming languages are no different. These rules are called syntax. In Python, the syntax is designed to be clean and uncluttered. You don't need to use semicolons at the end of lines like in many other languages.

One of Python's most important syntax rules is its use of indentation. The whitespace at the beginning of a line is not just for looks; it's how Python groups statements together. Using indentation consistently is mandatory. We'll see how this works to organize bigger blocks of code later on.

You can also leave notes in your code called comments. Python ignores anything on a line that follows a hash symbol (#). Comments are for humans to read, helping to explain what your code does.

# This is a comment. Python ignores it.

# The line below will print a message to the screen.
print("Hello, Python!")

Storing and Using Information

variable

noun

A container for storing a value that can be referenced and manipulated in a program.

Variables are a fundamental concept in programming. Think of them as labeled boxes where you can store information. You give a variable a name and put a value inside it using the equals sign (=), which is called the assignment operator.

name = "Alice"
age = 30

# Now the variable 'name' holds the text "Alice"
# and 'age' holds the number 30

The kind of information you store in a variable determines its data type. The data type tells Python what kind of data it is, like a number or text, and what you can do with it. Python is dynamically typed, meaning you don't have to explicitly declare the data type; Python figures it out for you.

Data TypeDescriptionExample
Integer (int)Whole numbers42, -100
Float (float)Numbers with a decimal point3.14, -0.5
String (str)A sequence of characters (text)"Hello", 'Python'
Boolean (bool)Represents truth valuesTrue, False

Making Things Happen

Once you have variables, you can use operators to perform calculations and create expressions. An expression is a combination of values, variables, and operators that Python evaluates to produce a new value.

Arithmetic operators work just like they do in math.

OperatorNameExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division10 / 25.0
**Exponent2 ** 38

You can also use operators to compare values. These expressions evaluate to a Boolean value: either True or False.

> (greater than), < (less than), == (equal to), and != (not equal to) are common comparison operators.

is_greater = 10 > 5  # This results in True
is_equal = 10 == 5   # This results in False

To interact with a user, you need a way to display information and receive information. Python gives us two simple functions for this: print() and input().

The print() function displays values on the screen. You can give it text, numbers, or variables, and it will show them.

planet = "Earth"
print("Hello from planet", planet)

The input() function pauses your program and waits for the user to type something and press Enter. It then returns whatever the user typed as a string, which you can store in a variable.

# Ask the user for their name and store it
user_name = input("What is your name? ")

# Greet the user by name
print("Nice to meet you,", user_name)

Now you have the building blocks. Let's test your understanding.

Quiz Questions 1/5

What is the primary, syntactical purpose of indentation in Python?

Quiz Questions 2/5

What will the input() function return after a user types their age and presses Enter?

With variables, data types, and operators, you can start to write simple but useful programs.