Python Programming Fundamentals
Python Basics
Getting Started with Python
Before you can write any code, you need to set up your programming environment. This just means getting the necessary tools on your computer. The most important tool is the Python interpreter itself, which is the program that understands and runs your Python code.
You can download the latest version of Python for free from the official website, python.org. The installation is straightforward, but it's a crucial first step.
Along with the interpreter, many programmers use an Integrated Development Environment (IDE). An IDE is like a text editor supercharged for coding, with features like syntax highlighting and debugging tools that make writing code much easier.
Your First Lines of Code
Every language, whether spoken or for programming, has its own rules for grammar and structure. In programming, these rules are called syntax. Python is known for its clean and readable syntax, which makes it a great language for beginners.
Let's start with one of the most fundamental operations: displaying information. In Python, you can print text to the screen using the print() function.
# This is a comment. Python ignores lines starting with a #.
print("Hello, World!")
When you run this code, the text Hello, World! appears on your screen. The print() function takes whatever is inside the parentheses and outputs it. The quotation marks tell Python that Hello, World! is a string of text.
Storing and Using Information
Writing programs often involves working with data. To keep track of this data, we use variables. Think of a variable as a labeled box where you can store information. You give the box a name and put something inside it. You can change the contents later if you need to.
To create a variable in Python, you just choose a name, use the equals sign (=), and provide the value you want to store.
# Assign the string "Welcome to Python" to a variable named 'greeting'
greeting = "Welcome to Python"
# Print the value stored in the 'greeting' variable
print(greeting)
The information you store in variables comes in different forms, or data types. For now, we'll focus on three of the most common ones.
| Data Type | Description | Example |
|---|---|---|
String (str) | A sequence of characters, like text. | "Hello", 'Python' |
Integer (int) | A whole number, without a decimal. | 10, -5, 1000 |
Float (float) | A number with a decimal point. | 3.14, -0.5, 99.9 |
Python automatically figures out the data type when you create a variable. You can check the type of any variable using the type() function.
name = "Alice"
age = 30
price = 19.99
print(type(name)) # Output: <class 'str'>
print(type(age)) # Output: <class 'int'>
print(type(price)) # Output: <class 'float'>
Working with Data
Once you have data stored in variables, you can perform operations on it. Python supports standard arithmetic operations, just like a calculator.
operator
noun
A symbol that represents an action or process.
x = 15
y = 4
# Addition
print(x + y) # Output: 19
# Subtraction
print(x - y) # Output: 11
# Multiplication
print(x * y) # Output: 60
# Division
print(x / y) # Output: 3.75
Besides performing calculations, you can also make your programs interactive by asking the user for input. The input() function prompts the user to enter some text and returns their entry as a string.
user_name = input("What is your name? ")
print("Hello, " + user_name)
A key detail is that input() always gives you a string, even if the user types in numbers. If you want to treat the input as a number, you need to convert it. You can change a string to an integer with int() or to a float with float().
age_str = input("How old are you? ")
age_int = int(age_str) # Convert the string to an integer
years_to_100 = 100 - age_int
print("You will be 100 in", years_to_100, "years.")
Finally, let's look at logical operators. These are used to compare values and result in either True or False. These are essential for making decisions in your code, a topic we'll explore later.
| Operator | Meaning | Example (a=5, b=10) | Result |
|---|---|---|---|
== | Equal to | a == 5 | True |
!= | Not equal to | a != b | True |
> | Greater than | a > b | False |
< | Less than | b < 20 | True |
>= | Greater than or equal to | a >= 5 | True |
<= | Less than or equal to | b <= a | False |
Let's check your understanding of these core concepts.
Which of the following lines of code will correctly print the text 'Hello, Python!' to the screen?
After running the code user_age = 25, what is the data type of the user_age variable?
You've just taken your first steps into Python. You've learned how to display output, store information in variables with different data types, and perform basic operations and comparisons. These are the fundamental building blocks for all Python programming.