Welcome to the Python Basics Tutorial! This guide will help you get started with Python programming, covering fundamental concepts and practical examples.

Table of Contents

  1. Introduction to Python
  2. Basic Syntax & Structure
  3. Data Types
  4. Control Structures
  5. Functions
  6. Modules & Packages
  7. Practice Exercises

Basic Syntax & Structure

Python uses indentation to define code blocks, making it readable and concise.

Example: Hello World

print("Hello, World!")  # Output: Hello, World!
python_syntax

Key Features

  • No semicolons: Statements end with a newline.
  • Whitespace-sensitive: Indentation defines scope.
  • Comments: Use # for single-line comments.

Data Types

Python has dynamic typing, so variables automatically adapt to their assigned values.

Common Data Types

Type Description
int Integer values (e.g., 42)
float Decimal numbers (e.g., 3.14)
str Strings (e.g., "Hello")
list Ordered collections (e.g., [1, 2, 3])
tuple Immutable collections (e.g., (1, 2, 3))
dict Key-value pairs (e.g., {"name": "Alice"})
python_data_types

Control Structures

Python supports if-else, for, and while loops for logic control.

Conditional Statements

if x > 0:  
    print("Positive")  
elif x == 0:  
    print("Zero")  
else:  
    print("Negative")  

Loops

  • For Loop:
    for i in range(5):  
        print(i)  
    
  • While Loop:
    while x < 10:  
        x += 1  
    
python_loops

Functions

Functions organize code into reusable blocks.

Defining a Function

def greet(name):  
    return f"Hello, {name}!"  

Function Parameters

  • Positional Arguments: greet("Alice")
  • Keyword Arguments: greet(name="Bob")
  • Default Values: def greet(name="Guest"):
python_functions

Modules & Packages

Use modules to organize related functions and packages to manage larger projects.

Importing Modules

import math  
print(math.sqrt(16))  # Output: 4.0  

Creating a Package

  1. Create a directory with an __init__.py file.
  2. Add modules inside the directory.
  3. Use import package.module to access them.
python_modules

Practice Exercises

Try these exercises to reinforce your knowledge:

  1. Write a program to calculate the sum of two numbers.
  2. Create a list of fruits and loop through it to print each item.
  3. Define a function that checks if a number is even or odd.

For more advanced topics, check out our Python Advanced Features guide! 🚀