Code Like a Ninja: Learn Python Like a Pro

Chapter 1: Advanced Data Structures and Algorithms

Lesson One

Python Lesson 1: Introduction to Python Programming

1. What is Python?

Python is a high-level, interpreted programming language known for its simplicity and readability. It is widely used in web development, data science, artificial intelligence, automation, and more.

Key Features:

  • Easy to read and write

  • Large standard library

  • Supports multiple programming paradigms (procedural, object-oriented, functional)

  • Open-source and free


2. Python Syntax Basics

Printing to the Screen

Use print() to display output:

print("Hello, World!")

Comments

Comments are ignored by Python and are used to explain code:

# This is a single-line comment

"""
This is a
multi-line comment
or docstring
"""

Variables

Variables store data. You don’t need to declare their type explicitly.

name = "Alice"  # string
age = 25        # integer
height = 5.7    # float

print(name, age, height)

Data Types

Python has several built-in data types:

TypeExampleint10float3.14str"Hello"boolTrue / Falselist[1, 2, 3]tuple(1, 2, 3)dict{"a": 1}


Operators

Arithmetic Operators

a = 10
b = 3
print(a + b)  # Addition: 13
print(a - b)  # Subtraction: 7
print(a * b)  # Multiplication: 30
print(a / b)  # Division: 3.3333...
print(a // b) # Floor division: 3
print(a % b)  # Modulus: 1
print(a ** b) # Exponentiation: 1000

Comparison Operators

print(a > b)   # True
print(a < b)   # False
print(a == b)  # False
print(a != b)  # True

Logical Operators

x = True
y = False
print(x and y)  # False
print(x or y)   # True
print(not x)    # False

Conditional Statements

age = 18

if age >= 18:
    print("You are an adult")
else:
    print("You are a minor")

Loops

1. for Loop

for i in range(5):
    print(i)  # Prints 0 to 4

2. while Loop

count = 0
while count < 5:
    print(count)
    count += 1

Functions

Functions help organize code into reusable blocks.

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

print(greet("Alice"))

Lists

Lists are ordered, changeable collections.

fruits = ["apple", "banana", "cherry"]
fruits.append("orange")  # Add an item
print(fruits[1])         # Access by index
fruits.remove("banana")  # Remove an item
print(fruits)

Practice Exercises

  1. Write a Python program to print your name and age.

  2. Write a program that checks if a number is even or odd.

  3. Write a function that takes two numbers and returns their sum.

  4. Create a list of 5 of your favorite foods and print each item using a loop.

  5. Write a program to print numbers from 10 down to 1 using a loop.