web development
, data science
, machine learning
, and automation
.PyCharm
, or even a simple text editor like Notepad
.Important: Python source code files always use the
.py
extension.
print
Functionprint
function.print
function features like formatting and special characters.print
The print
function is used to output text or variables to the console. or to a file.
Video: Use of print() function in python
Syntax:
print(value1, value2, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Parameters:
value1, value2, ...
: The values to be printed. Multiple values can be separated by commas.sep
: (Optional) Specifies how to separate multiple values. Default is a space ' '
.end
: (Optional) Specifies what to print at the end. Default is a newline character '\n'
.file
: (Optional) Specifies the file where to print. Default is sys.stdout
(console).flush
: (Optional) Specifies whether to forcibly flush the stream. Default is False
.Instructions:
Examples
# Task 1.1: Print a simple message
print("Hello, world!")
# Task 1.2: Print multiple items
print("Hello", "world", 2024)
Instructions
Examples
# Task 2.1: Print different data types
print(42)
print(3.14159)
print("This is a string")
sep
and end
ParametersInstructions
Examples
# Task 3.1: Change the separator
print("apple", "banana", "cherry", sep=", ")
# Task 3.2: Change the ending character
print("Hello", end=" ")
print("world!")
# Task 3.3: Print with a custom ending character:
print("Hello", "World", end="!")
Instructions
# Task 4.1: print a integer variable
x = 5
print(x)
# Task 4.2: print a string variable
message = 'Python is fun'
# print the string message
print(message)
# Task 4.3: print a string variable
message = "How are you?"
print(message)
Instructions
Examples
# Task 5.1: Use f-strings
name = "Ahmad"
age = 30
print(f"My name is {name} and I am {age} years old.")
Python f-Strings Explained: What Does print(f’{a=}’) Do?
The expression print(f”{a=}”) is part of Python’s f-string formatting introduced in Python 3.8.
When you use a=, Python prints both the name of the variable and its value. Essentially, it shows what the variable is and its corresponding value.
Instructions
Examples
# Task 6.1: Print a newline character
print("Hello\nWorld")
# Task 6.2: Print a tab character
print("Hello\tWorld")
video: Python line break: How to Print Line Break
# Task 6.3: Print a tab character
print("Name\tAge\tCity")
print("Alice\t30\tNew York")
print("Bob\t25\tLos Angeles")
Output:
Name Age City
Alice 30 New York
Bob 25 Los Angeles
In this example, \t
is used to align the columns of text.
In Python, the \t
character is a special escape sequence that represents a horizontal tab. When used in the print
function or any other string operation, it inserts a tab space in the output. This can be particularly useful for formatting text to make it more readable.
\t
can be combined with other string manipulation techniques, such as f-strings
# Task 6.4: Print a tab character with f-strings
name = "Alice"
age = 30
city = "New York"
print(f"{name}\t{age}\t{city}")
Instructions
Examples
# Task 7.1: Print to a file
with open("output.txt", "w") as file:
print("Hello, file!", file=file)
When you use the instruction with open(“output.txt”, “w”) as file in Python, the file is created in the current working directory of your program. On an Android system, this could be different depending on the environment where the code is executed (e.g., a specific app’s data directory, a shared storage location, etc.).
To determine the exact path, you can use the os module to get the current working directory:
import os
print(os.getcwd())
Regarding file closure, when you use the with
statement to open a file, Python automatically takes care of closing the file for you once the block of code under the with
statement is executed. There is no need to explicitly close the file; it is done automatically when the block is exited. This is one of the benefits of using the with statement for file operations. for more details, see Understanding Python’s os.getcwd(): Get Current Working Directory with Examples
Objective: Learn about syntax errors in Python by examining and correcting a sample code snippet.
Instructions:
Code Snippet:
print("Hello World!"
Steps:
print("Hello World!")
Syntax error:
See also:
print()
function to display it.\t
(tab character) to align item names and prices neatly.name
, age
, and hobby
.print()
function to create a sentence incorporating these variables."Hello World\n" * 100
) as a possible solution.related video: 100 times “hello world” without loop
print()
statements.\n
).Related Video: How to print multiple lines
"w"
).write()
method.
There are two main types of comments in Python:
# This is a single-line comment
print("Hello, World!")
"""
This is a multi-line comment.
It can span multiple lines of code.
"""
print("Hello, World!")
See also:
In Python, indentation refers to the use of whitespace (spaces or tabs) to denote block-level structure in the code. Python uses indentation to define the scope of code blocks, such as:
In Python, indentation is mandatory and is used to determine the grouping of statements. The number of spaces used for indentation is not fixed, but it’s standard to use 4 spaces for each level of indentation. Read more: Indentation - PEP 8 – Style Guide for Python Code
Here’s an example:
if True:
print("Hello") # This line is indented with 4 spaces
print("World") # This line is also indented with 4 spaces
In this example, the two print statements are indented with 4 spaces, indicating that they are part of the if block.
Python’s indentation system has several benefits, including:
Another example, consider the following code snippet:
if True:
print("True")
else:
print("False")
Task: 15 Please correct the following Python code:
if True:
print("True")
print("False")
Error message: IndentationError: unexpected indent
”
See also:
Title: “Teach Your Team”
Instructions:
💡 Key Learning Points:
✔ Leadership through teaching.
✔ Confidence in public speaking.
✔ Understanding Python better by explaining it.
✅ Online Quizzes for Python Basics