Python Programming Fundamentals
Python Basics
Setting Up Your Workspace
Before you can write Python code, you need to install it on your computer. The official Python website is the best place to find the installer for your operating system. Just download the latest version and follow the installation instructions.
Once installed, you can write Python code in any plain text editor. However, most developers use an Integrated Development Environment (IDE) or a dedicated code editor like VS Code, PyCharm, or Sublime Text. These tools offer helpful features like syntax highlighting, which colors your code to make it more readable, and code completion.
Python's Clean Syntax
Python is known for its clean and readable syntax. One of the most distinctive features is its use of indentation. Where other languages might use curly braces {} or keywords to group blocks of code, Python uses whitespace. This means the spacing at the beginning of a line is not just for looks—it's part of the program's structure. Consistent indentation is mandatory.
This design choice forces you to write code that is visually organized and easy to follow. You'll typically use four spaces for each level of indentation.
Storing Information
In programming, you need a way to store and manage data. We do this using variables. A variable is like a labeled box where you can keep a piece of information. You create a variable by giving it a name and assigning it a value using the equals sign (=), which is called the assignment operator.
Think of
age = 25as telling the computer, "Create a box namedageand put the number 25 inside it."
The type of data you store determines what you can do with it. Python has several basic data types built in.
Integer
noun
A whole number, without a fractional part. It can be positive, negative, or zero.
Floats, short for floating-point numbers, are numbers that have a decimal point.
Examples:
3.14,-0.001,99.9
Strings represent text. You create them by enclosing characters in either single quotes (') or double quotes (").
Examples:
'Hello, world!',"Python programming"
Finally, Booleans represent one of two values: True or False. They are essential for logic and decision-making in programs.
# Variable assignments for different data types
# An integer
user_count = 105
# A float
item_price = 14.99
# A string
user_name = "Alex"
# A boolean
is_active = True
Interacting with Your Program
A program isn't very useful if it can't communicate. The simplest way to display information is with the print() function. Whatever you put inside the parentheses will be shown in the output console.
print("Welcome to the program!")
product_name = "Laptop"
price = 1200
# You can print variables and text together
print("The price of the", product_name, "is", price)
To get information from a user, you can use the input() function. It displays a prompt and waits for the user to type something and press Enter. The function then returns the user's input as a string.
# Ask the user for their name and store it
name = input("What is your name? ")
# Greet the user
print("Hello,", name)
It's important to remember that input() always gives you a string. If you need a number, you have to convert it. For example, you can use int() to convert a string to an integer.
age_str = input("How old are you? ")
# Convert the string input to an integer
age_num = int(age_str)
# Now you can do math with it
years_to_100 = 100 - age_num
print("You will be 100 in", years_to_100, "years.")
Now you have a grasp of the fundamental building blocks of Python. It's time to check your understanding.
What is the recommended source for downloading the official Python installer?
In Python, how are blocks of code, such as the body of a function, grouped together?
