Share Your Feedback

What is Python

  • Python is a high-level, general-purpose programming language.
  • It is known for its clear syntax, readability, and versatility.
  • Python is widely used for web development, data science, machine learning, and automation.

Getting Started

Important: Python source code files always use the .py extension.

Lesson 1: Python print Function

Objectives

  • Understand the basic usage of the print function.
  • Learn how to print different data types.
  • Explore advanced print function features like formatting and special characters.
  • Practice printing in various tasks.

Introduction to 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.

Task 1: Basic Printing

Instructions:

  1. Print a simple message.
  2. Print multiple items separated by commas.

Examples

# Task 1.1: Print a simple message
print("Hello, world!")

# Task 1.2: Print multiple items
print("Hello", "world", 2024)

Task 2: Printing Different Data Types

Instructions

  1. Print integers, floats, and strings.

Examples

# Task 2.1: Print different data types
print(42)
print(3.14159)
print("This is a string")

Task 3: Using sep and end Parameters

Instructions

  1. Change the separator between printed items.
  2. Change the ending character of a print statement.

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="!")

Task 4: Print Variables

Instructions

  • print variables values using print function

# 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)

Task 5: String Formatting with f-Strings

Instructions

  1. Use f-strings (formatted string literals) for the same purpose.

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.

Task 6: Printing Special Characters

Instructions

  1. Print a newline character within a string.
  2. Print a tab character within a string.

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}")

Task 7: Printing to a File

Instructions

  1. Print a message to a text file instead of the console.

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

Task 8: Understanding Syntax Errors in Python

Objective: Learn about syntax errors in Python by examining and correcting a sample code snippet.

Instructions:

  1. Review the Code Snippet: Look at the provided Python code and identify any syntax errors.
  2. Identify the Error: Understand what a syntax error is and why it occurs.
  3. Correct the Code: Fix the syntax error in the code snippet.
  4. Explanation: Write a brief explanation of what the syntax error was and how you corrected it.

Code Snippet:

print("Hello World!"

Steps:

  1. Review the Code Snippet: Look carefully at the code snippet above.
  2. Identify the Error:
    • A syntax error occurs when the Python interpreter finds code that does not conform to the rules of the Python language.
    • The provided code snippet has a syntax error because it has an unmatched parenthesis.
  3. Correct the Code:
    • To fix the error, ensure all parentheses are properly closed.
    • The corrected code should look like this:
      print("Hello World!")
      
  4. Explanation:
    • Syntax Error: The error was due to a missing closing parenthesis.
    • Correction: Adding the closing parenthesis at the end of the print statement fixes the syntax error.

Syntax error:

  • A syntax error in programming occurs when the code violates the rules of the programming language’s syntax.
  • This means that the code’s structure and commands do not conform to the expected format that the interpreter or compiler requires to successfully read and execute the code.

See also:

Task 9: Print Your Favorite Quote

  • Choose your favorite quote.
  • Use Python’s print() function to display it.
  • Ensure the quote is properly formatted (e.g., with quotation marks and correct line breaks if necessary).

Task 10: Create a Simple Receipt**

  • Define item names and prices.
  • Use \t (tab character) to align item names and prices neatly.
  • Print a simple receipt showing itemized costs.
  • Optionally, include a total cost at the bottom.

Task 11: Use Variables in Print Statements**

  • Create three variables: name, age, and hobby.
  • Assign appropriate values to each variable.
  • Use the print() function to create a sentence incorporating these variables.

Task 12: 100 Times “Hello World” Without Loop**

  • Print “Hello World” 100 times without using a loop.
  • Consider string multiplication ("Hello World\n" * 100) as a possible solution.

related video: 100 times “hello world” without loop

Task 13: How to Print Multiple Lines**

  • Print multiple lines of text using different methods:
    • Using multiple print() statements.
    • Using newline characters (\n).

Related Video: How to print multiple lines

Task 14: Output to a File**

  • Write a short summary of your week (tasks completed, hours worked, achievements).
  • Open a text file in write mode ("w").
  • Write the summary to the file using Python’s write() method.
  • Close the file properly.

Comments

  • Comments are important for making code more readable and understandable, especially for other programmers who may need to read or modify the code.
  • Comments in Python are non-executable lines of code and ignored by the Python interpreter when the code is executed.

There are two main types of comments in Python:

  • Single-line comments: These comments start with the hash symbol (#) and extend to the end of the line.
# This is a single-line comment
print("Hello, World!")
  • Multi-line comments: These comments are enclosed in triple quotes (“”” or ‘’’).
"""
This is a multi-line comment.
It can span multiple lines of code.
"""
print("Hello, World!")

See also:

Indentation

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:

  • Function definitions
  • Loops (for, while)
  • Conditional statements (if, elif, else)
  • Exception handling (try, except)

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:

  • Improved readability: Indentation makes the code structure clear and easy to read.
  • Reduced errors: Indentation helps avoid errors caused by mismatched braces or parentheses.
  • Simplified syntax: Python’s indentation system eliminates the need for explicit block delimiters like braces or keywords.

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:

Task 16: Code Together, Lead Together

Title: “Teach Your Team”
Instructions:

  1. Each student picks one basic Python topic (e.g., print function,Basic Printing, Printing Different Data Types, Using sep and end Parameters, Print Variables, String Formatting with f-Strings, Printing Special Characters, Syntax Error, Comments, Indentation).
  2. Prepare a 2-minute explanation for the group (using examples).
  3. Deliver their explanation to the team.
  4. Peers ask questions and give feedback.

💡 Key Learning Points:
✔ Leadership through teaching.
✔ Confidence in public speaking.
✔ Understanding Python better by explaining it.

Practice & Progress


Multiple-Choice Questions (MCQs)


Find and Fix Mistakes

Online Quizzes for Python Basics


Coding Exercises


Mini Projects


Review Questions


References and Bibliography