Introduction to Python Programming
Python Basics
Getting Started with Python
Before you can write any Python code, you need a place to write and run it. This is called your development environment. The first step is to install Python itself on your computer. You can download it for free from the official Python website.
Once installed, you can write code in a simple text editor and save it with a .py extension, like my_first_program.py. You then run this file from your computer's terminal. Many developers prefer to use an Integrated Development Environment (IDE), which is software that combines a text editor, a terminal, and other helpful tools into one application. For now, Python's built-in IDLE editor is a great place to start.
Python's Syntax
Python is known for its clean and readable syntax. It's designed to be uncluttered, which helps you focus on the logic of your code. One of the most unique features of Python is how it uses whitespace. Unlike many other languages that use brackets or keywords to define blocks of code, Python uses indentation.
Consistent indentation is not just for style in Python—it's a rule. It tells the interpreter how your code is structured.
You can also leave notes for yourself or others in your code using comments. A comment starts with a hash symbol (#) and extends to the end of the line. Python ignores comments when it runs the program.
# This is a comment. Python will ignore it.
# The line below will be executed.
print("Hello from Python!")
Storing Information
Programs need to store and manage information. We do this using variables. Think of a variable as a labeled box where you can keep a piece of data. You give the box a name and put something inside it. You can change the contents later if you need to.
# Here, 'name' is the variable, and "Alice" is the value.
name = "Alice"
# We can assign a new value to the same variable.
name = "Bob"
The kind of data you store is called its data type. Python has several built-in types, but a few are essential for getting started.
| Data Type | Description | Example |
|---|---|---|
String (str) | A sequence of text characters. | "Hello, world!" |
Integer (int) | A whole number. | 42 |
Float (float) | A number with a decimal point. | 3.14159 |
Boolean (bool) | Represents either True or False. | True |
Python is dynamically typed, which means you don't have to specify the data type when you create a variable. Python automatically figures it out based on the value you assign.
# A string for a user's name
user_name = "Charlie"
# An integer for their age
user_age = 25
# A float for the price of an item
item_price = 19.99
# A boolean to check if they are logged in
is_logged_in = False
Basic Input and Output
A program isn't very useful if it can't communicate with the outside world. This is done through input and output operations. Output is how the program presents information to the user, and input is how it receives information from the user.
The primary way to display output in Python is with the print() function. You can give it text, numbers, or variables, and it will display them in the console.
current_year = 2024
print("Welcome to the program!")
print("The current year is:", current_year)
To get input from a user, you can use the input() function. It shows the user a message, pauses the program, and waits for them to type something and press Enter. The text the user enters is then returned by the function so you can store it in a variable.
One important detail: the
input()function always gives you back a string, even if the user types in numbers.
If you need to treat the user's input as a number, you have to convert it. You can use int() to convert a string to an integer or float() to convert it to a float.
# Ask for the user's name
name = input("What is your name? ")
# Ask for their age (and convert it to an integer)
age_string = input("How old are you? ")
age = int(age_string)
print("Hello,", name)
print("Next year you will be", age + 1)
Now you have the fundamental building blocks for writing simple Python programs. You can set up your environment, write clean code, store data in variables, and interact with a user.
What is the primary purpose of a variable in Python?
How does Python define a block of code, such as the body of a loop or function?
