Python for PyTorch
Python Basics
Python's Simple Syntax
One of the best things about Python is its readability. It's designed to look a lot like plain English, which makes it easier for beginners to pick up. A key feature of Python's syntax is its use of indentation. Where other languages might use brackets or keywords to group code, Python uses whitespace. This forces programmers to write clean, organized code from the start.
Let's look at the classic first program everyone writes: "Hello, World!".
# This is a comment. Python ignores anything after a #
print("Hello, World!")
That's it. The print() function simply displays whatever you put inside the parentheses on the screen. Simple, right? This focus on clean and straightforward code is a core principle of the language.
Storing Information
To do anything useful, a program needs to work with information, or data. Python has several built-in data types to handle different kinds of information. Think of them as different kinds of containers, each designed for a specific type of item.
We store data in variables. A variable is just a name you give to a piece of data so you can refer to it later. Here are the most common data types you'll encounter:
| Data Type | Description | Example |
|---|---|---|
Integer (int) | Whole numbers, without a fractional part. | age = 30 |
Float (float) | Numbers with a decimal point. | price = 19.99 |
String (str) | A sequence of characters, like text. | name = "Alice" |
Boolean (bool) | Represents one of two values: True or False. | is_learning = True |
When you assign a value to a variable in Python using the equals sign (=), Python automatically figures out the data type. You don't have to declare it beforehand.
# Python knows this is an integer
number_of_students = 25
# And it knows this is a string
subject = "Computer Science"
print(subject)
Controlling the Flow
A program doesn't just run from top to bottom. It needs to make decisions and repeat actions. This is where control structures come in. They direct the flow of your program.
To make decisions, we use if statements. An if statement checks if a condition is true. If it is, it runs a block of code. You can also add an else to run code if the condition is false. For more complex decisions, elif (short for "else if") lets you check multiple conditions.
temperature = 75
if temperature > 80:
print("It's a hot day!")
elif temperature < 60:
print("Better bring a jacket.")
else:
print("The weather is perfect.")
# Output: The weather is perfect.
What about repeating tasks? For that, we use loops. A for loop is great for when you want to do something for each item in a list.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(f"I like to eat {fruit}s.")
# This will print a line for each fruit in the list.
A while loop is used to repeat a block of code as long as a certain condition remains true. It's useful when you don't know ahead of time how many times you need to loop.
countdown = 3
while countdown > 0:
print(countdown)
countdown = countdown - 1
print("Blast off!")
# Output:
# 3
# 2
# 1
# Blast off!
These basic building blocks—syntax, data types, and control structures—are the foundation for writing any Python program.
Time to check your understanding of these core concepts.
What does Python use to define a block of code (e.g., the body of an if statement or a loop)?
In Python, which keyword is used to check an additional condition if the preceding if condition was false?