Introduction to Python Programming
Python Setup & Syntax
Getting Python Running
Before writing Python, you need to install it. You have a couple of good options. You can install Python directly from python.org and use it with a text editor like VS Code. This gives you a clean, standard setup.
Another popular choice is installing a distribution like , which bundles Python with many useful data science libraries and tools. This is great if you plan to work with data, as it saves you a lot of setup time later.
Once Python is installed, you can run a script from your terminal. Save your code in a file, for example hello.py, and then execute it.
# In your terminal
python hello.py
This command tells the Python interpreter to read and execute the code inside your file.
Syntax and Structure
Python's syntax is famously clean. One of its most distinctive features is its use of indentation to define code blocks. Where other languages use curly braces {} to group statements in a loop or function, Python uses whitespace. This isn't just for style; it's a rule the interpreter enforces.
Consistent indentation is mandatory. Using four spaces is the standard convention.
Comments are crucial for making your code understandable. In Python, you use the hash symbol # to start a comment. Everything after the # on that line is ignored by the interpreter.
# This is a single-line comment.
x = 10 # This comment explains the variable assignment.
For multi-line comments, you can use triple quotes, although these are technically multi-line strings that are often used for this purpose.
"""
This is a multi-line comment
or docstring. It can span
multiple lines.
"""
y = 20
Data Types and Operators
Python handles data types dynamically. You don't need to declare a variable's type; the interpreter figures it out when you assign a value. This is called and it makes for faster development.
Here are the most common built-in types:
my_integer = 100 # Integer
my_float = 10.5 # Float (decimal number)
my_string = "Hello" # String (text)
my_boolean = True # Boolean (True or False)
is_nothing = None # NoneType (represents the absence of a value)
You can check a variable's type with the type() function, and convert between compatible types.
x = "123"
y = int(x) # y is now the integer 123
z = str(y) # z is now the string "123"
Python includes a standard set of operators to manipulate these types.
| Category | Operators | Description |
|---|---|---|
| Arithmetic | +, -, *, /, //, %, ** | Perform math. // is floor division, ** is exponentiation. |
| Comparison | ==, !=, >, <, >=, <= | Compare values, resulting in True or False. |
| Logical | and, or, not | Combine boolean expressions. |
| Assignment | =, +=, -=, *=, /= | Assign and update values in one step. |
Talking to the User
To display information, you use the print() function. To get information from the user, you use input().
# Get the user's name
name = input("What is your name? ")
# Greet the user
print("Hello, " + name + "!")
A key thing to remember is that input() always returns a string. If you need a number, you have to convert it yourself.
age_string = input("How old are you? ")
age_number = int(age_string)
years_to_100 = 100 - age_number
Combining strings and variables for output can be clumsy. Python provides a clean solution called (formatted string literals). By putting an f before the opening quote, you can embed expressions directly inside curly braces within the string.
name = "Alice"
age = 30
# Using an f-string for clean, readable output
print(f"Hello, my name is {name} and I am {age} years old.")
# You can even put expressions inside!
print(f"In 5 years, I will be {age + 5} years old.")
With these building blocks, you can start writing simple, interactive Python scripts.
If a data scientist wants to install Python along with a suite of popular libraries like NumPy and Pandas with minimal setup, which of the following is the most convenient option?
Unlike many other programming languages that use curly braces {}, Python uses a different mechanism to define code blocks. What is it?