| Previous chapter | Next Chapter |
|---|---|
| Python Chapter 2: Working with Loops |
Welcome to the first step in your Python learning journey! Before diving into any complex concepts, it’s essential to get familiar with the basics of Python syntax. Python is known for its readability and simplicity, which makes it an excellent starting point for beginners. Let’s break down some core elements you’ll encounter.
1. Comments
Comments are lines in your code that the computer ignores. They’re just for you or other developers to read and understand the code better. You can write a comment using the # symbol.
Example:
# This is a comment
print("Hello, HHF Technology there.") # This will print a message
2. Variables
A variable is like a container where you store information. You give it a name, and then you can use that name to refer to the data later.
Example:
name = "Alice"
age = 25
In the example above, name stores the string "Alice", and age stores the number 25.
3. Data Types
In Python, data comes in different types. Here are some of the most common ones:
- String: Text data. Written in quotes, like
"Hello"or'world'. - Integer: Whole numbers, like
5or-10. - Float: Decimal numbers, like
3.14or0.5. - Boolean: True/False values. Either
TrueorFalse.
Example:
is_sunny = True # Boolean
temperature = 31.5 # Float
city = "Dubai" # String
4. Printing to the Screen
To display information on the screen, you can use the print() function. Here are different ways to print variables:
- Concatenating Strings: You can concatenate variables with text using the
+operator. Just remember to convert non-string variables into text usingstr().Example:
name = "Alice"
age = 25
print("Hello, " + name + ". You are " + str(age) + " years old.")
- Using Commas: You can also use commas inside
print(). This way, Python automatically adds spaces and handles different data types without needing conversion.Example:
name = "Alice"
age = 25
print("Hello,", name, ". You are", age, "years old.")
- Using f-strings (formatted string literals): Introduced in Python 3.6, f-strings are one of the most convenient ways to format strings. You just put the variable names inside curly braces
{}within the string, and prefix the string with anf.Example:
name = "Alice"
age = 25
print(f"Hello, {name}. You are {age} years old.")
- Using
.format()method: Another way to format strings is with the.format()method. You use placeholders{}in the string and fill them with variables inside theformat()function.Example:
name = "Alice"
age = 25
print("Hello, {}. You are {} years old.".format(name, age))
- Using
%operator (old-style formatting): This is an older way to format strings, using%as a placeholder and specifying the type (like%sfor strings or%dfor integers). Though still supported, f-strings and.format()are generally preferred in modern Python code.Example:
name = "Alice"
age = 25
print("Hello, %s. You are %d years old." % (name, age))
With these methods, you can choose the style that best fits your coding preferences. F-strings are generally the most recommended for readability and ease of use.
5. Basic Math Operations
Python can do math for you using standard operators:
+for addition-for subtraction*for multiplication/for division**for exponentiation
Example:
x = 10
y = 2
print(x + y) # Outputs 12
print(x * y) # Outputs 20
print(x ** y) # Outputs 100 (10^2)
6. Indentation
Python uses indentation (spaces or tabs) to organize code into blocks. This is especially important when you start working with loops, conditions, and functions. Each block of code must be properly indented.
Example:
if age >= 18:
print("You are an adult")
else:
print("You are not an adult")
7. Making Decisions (Conditions)
You can use if, elif, and else statements to control what your code does based on conditions.
Example:
temperature = 30
if temperature > 25:
print("It's hot outside")
elif temperature > 15:
print("It's warm")
else:
print("It's cold")
That’s it for now! By understanding these basics, you’re well on your way to writing simple Python programs. In the next section, we’ll dive into loops and functions to make your code even more powerful.
Stay curious and keep experimenting!
Greetings from HHF Team!