Introduction to Python Programming
Python Basics
Getting Started with Python
Python is a powerful and popular programming language known for its clear, readable syntax. It's a great choice for beginners because it lets you write programs with fewer lines of code than most other languages. The first step on your journey is to get Python set up on your computer.
You can download the latest version directly from the official Python website, python.org. The installation is straightforward and includes Python's standard library, an interactive shell to run commands one by one, and a basic development environment called IDLE (Integrated Development and Learning Environment). Once installed, you're ready to write your first program.
In programming, a long-standing tradition is to make your first program display the text "Hello, World!". In Python, this is incredibly simple. We use a built-in instruction called print() to display output to the screen. You just put the text you want to show inside the parentheses, enclosed in quotation marks.
print("Hello, World!")
Syntax and Structure
One of Python's defining features is its emphasis on readability. Unlike many other programming languages that use curly braces {} or keywords to define blocks of code, Python uses indentation.
In Python, indentation is not just for style. It's a rule that the language enforces.
When you start writing more complex programs with structures like loops or conditions, you'll group statements together by indenting them. The standard is to use four spaces for each level of indentation. For now, just remember that consistent indentation is key.
You can also leave notes for yourself or other programmers in your code using comments. Comments are ignored by the Python interpreter. To write a comment, simply start the line with a hash symbol (#).
# This is a comment. Python will ignore this line.
print("This line will be executed.")
Storing and Using Information
To do anything useful, programs need to store and work with data. We do this using variables. Think of a variable as a labeled box where you can store a piece of information. You give the box a name, and you can put something inside it. Later, you can look inside the box by using its name.
variable
noun
A named storage location in a computer's memory that holds a value.
You create a variable by choosing a name and using the assignment operator, which is the equals sign (=), to give it a value. The name of the variable goes on the left, and the value you want to store goes on the right.
# Assigning the string "Alice" to a variable named 'name'
name = "Alice"
# Assigning the number 30 to a variable named 'age'
age = 30
# Now we can print the values by using the variable names
print(name)
print(age)
The information you store in a variable has a specific type. Python has several built-in data types, but let's start with the four most common ones.
| Data Type | Description | Example Code |
|---|---|---|
Integer (int) | Whole numbers, positive or negative. | year = 2024 |
Float (float) | Numbers with a decimal point. | price = 19.99 |
String (str) | A sequence of characters, like text. | greeting = "Hello" |
Boolean (bool) | Represents one of two values: True or False. | is_active = True |
Strings are very flexible and can be enclosed in either single quotes ('...') or double quotes ("..."). Booleans are important for making decisions in your code. Note that True and False are special keywords in Python; they are not strings, and they must be capitalized.
Interacting with the User
So far, we've only seen how to display information. A program becomes much more interesting when it can receive information from a user. Python's input() function makes this easy. It pauses your program, waits for the user to type something and press Enter, and then returns whatever they typed as a string.
# Ask the user for their name and store it in a variable
user_name = input("What is your name? ")
# Greet the user by name
print("Nice to meet you, " + user_name)
In the example above, the text inside the input() parentheses is the prompt shown to the user. After the user types their name, it's stored in the user_name variable. We then use the + operator to combine the string "Nice to meet you, " with the name the user provided, creating a personalized greeting.
Keep in mind that input() always gives you back a string, even if the user types a number. If you need to treat the input as a number for calculations, you'll need to convert it first.
You can convert a string to an integer using int() or to a float using float().
# Get the user's age as a string
age_string = input("How old are you? ")
# Convert the string to an integer
age_number = int(age_string)
# Now you can perform math with it
age_in_a_decade = age_number + 10
print("In ten years, you will be:")
print(age_in_a_decade)
Now you have the basic building blocks to write simple Python programs that can display information, store it in variables, and interact with a user. Let's test your understanding.
What is the primary built-in function used in Python to display output on the screen?
Unlike many other languages that use curly braces {}, how does Python group statements into code blocks?
You've just taken your first steps into the world of Python. These fundamentals—variables, data types, and basic input/output—are the foundation upon which all other programming concepts are built.
