No history yet

Python Basics

Setting Up Your Workspace

Before you can write any code, you need the Python interpreter. Think of the interpreter as a translator that converts your Python code into instructions the computer can understand. The best place to get it is from the official Python website, python.org. Download the latest stable version for your operating system.

During installation, make sure to check the box that says "Add Python to PATH." This small step will make it much easier to run your programs from the command line later on.

Once installed, you can write Python code in any plain text editor. However, most developers use an Integrated Development Environment (IDE) like VS Code, PyCharm, or even a simple editor that comes with Python called IDLE. These tools offer features like syntax highlighting and debugging that make coding much smoother.

Lesson image

Storing Information

Programs need to work with information, like numbers, names, or statuses. In Python, we store this information in variables. A variable is like a labeled box where you can put a piece of data. You give the box a name, and you can change what's inside it whenever you need to.

variable

noun

A symbolic name that is a reference or pointer to an object. Once an object is assigned to a variable, you can refer to the object by that name.

Creating a variable is simple. You just choose a name, use the equals sign (=), and provide the data you want to store.

# Assigning the number 42 to a variable named 'answer'
answer = 42

# Assigning text to a variable named 'greeting'
greeting = "Hello, world!"

The kind of data you store determines the variable's data type. Python has several built-in types, but we'll start with the most common ones.

Data TypeDescriptionExample
str (String)A sequence of characters, like text. Always wrapped in quotes."Python" or 'AI'
int (Integer)A whole number, without a fractional part.10, -5, 2024
float (Float)A number with a decimal point.3.14, -0.5, 99.0
bool (Boolean)Represents one of two values: True or False.True, False

Python is smart enough to figure out the data type on its own when you assign a value to a variable. This is called dynamic typing, and it makes the language flexible and easy to use.

Talking to Your Program

Communicating with your program is essential. You need to see what it's doing and give it new information to work with. Python provides two simple functions for this: print() and input().

Python basics cover fundamental concepts like variables, data types, and basic operations.

The print() function displays information to the screen. You can give it text, numbers, or the value stored in a variable.

# Print a simple text message
print("Analyzing data...")

# Print the value of a variable
user_name = "Alex"
print("Welcome,", user_name)

To get information from the user, you use the input() function. It shows the user a message, waits for them to type something, and then returns whatever they typed as a string.

# Prompt the user for their name
prompt_message = "Please enter your name: "
user_name = input(prompt_message)

# Greet the user with the name they provided
print("Hello,", user_name)

One important thing to remember is that input() always gives you a string, even if the user types a number. If you need to treat the input as a number, you'll have to convert it first, but we'll get to that later.

Let's check your understanding of these core concepts.

Quiz Questions 1/6

What is the primary role of the Python interpreter?

Quiz Questions 2/6

What is the term for Python's ability to automatically determine a variable's data type when a value is assigned to it?

With these building blocks, you're ready to start writing simple but powerful Python scripts.