No history yet

Python Basics

Getting Started with Python

Python is a popular programming language known for its clear, readable syntax. It’s like writing in a simplified version of English, which makes it a great choice for beginners. Before you can write any code, you need to set up your programming environment.

First, you'll need to install Python itself. You can download it for free from the official Python website, python.org. The installation process is straightforward, much like installing any other software.

Lesson image

While you can write Python code in a simple text editor, it’s much easier to use an Integrated Development Environment, or IDE. An IDE is software that provides tools to make coding easier, like syntax highlighting (coloring your code to make it more readable) and debugging aids. Some popular IDEs for beginners include Thonny, VS Code, and PyCharm.

The Rules of the Road

Every language has rules, and programming languages are no different. Python’s rules, or syntax, are designed to be simple and consistent. One of the most unique aspects of Python is that it uses whitespace, specifically indentation, to structure code. Where other languages might use curly braces {} or keywords, Python uses tabs or spaces. This makes the code clean and easy to read.

In Python, indentation isn't just for style, it's a rule the computer follows to understand the structure of your program.

Another key part of syntax is adding comments. Comments are notes in your code that are ignored by the computer but are invaluable for humans. They help you and others understand what your code is doing. In Python, a comment starts with a hash symbol #.

# This is a comment. The computer will ignore this line.
print("Hello, World!") # This part of the line is a comment too.

Storing Information

Programs need to work with information, like numbers and text. To keep track of this information, we use variables. Think of a variable as a labeled box where you can store a value. You can give the box a name and put something inside it. You can also change what's inside the box later.

age = 25
name = "Alice"

# The variable 'age' now holds the number 25.
# The variable 'name' now holds the text "Alice".

The type of information you store is also important. Python has several built-in data types. For now, let’s focus on four basic ones:

Data TypeDescriptionExample
Integer (int)Whole numbers10, -5, 0
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

Python is smart enough to figure out the data type on its own when you create a variable. You don't need to explicitly tell it that 10 is an integer or that "Alice" is a string.

A Simple Conversation

A program isn't very useful if it can't communicate with the outside world. This is where input and output operations come in. The most common way to display output is the print() function. Whatever you put inside the parentheses will be shown to the user.

print("This message will be displayed on the screen.")

user_name = "Bob"
print("Hello,", user_name)

To get input from a user, you can use the input() function. This function will pause the program and wait for the user to type something and press Enter. The text the user types is then returned as a string, which you can store in a variable.

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

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

When you run the code above, it will first print "What is your name? ". After you type your name and press Enter, it will respond with a personalized greeting.

Let's put it all together. Time to check your understanding.

Quiz Questions 1/5

What does Python use to define the structure of code blocks, such as loops and functions?

Quiz Questions 2/5

What is the primary purpose of an Integrated Development Environment (IDE)?

With these building blocks—setting up your environment, understanding syntax, using variables, and handling input/output—you have the foundation for writing simple but powerful Python programs.