Python Basics: A Beginner's Guide
This document provides an introductory overview of Python programming.
It covers fundamental concepts, syntax, and examples to help beginners get
started. Feel free to copy this Markdown content and paste it into MassiveMark
Playground to generate a DOCX or PDF file.
Table of Contents
1. Introduction to Python
2. Setting Up Python
3. Basic Syntax and Data Types
4. Control Structures
5. Functions
6. Simple Example: A Calculator
7. Next Steps
Introduction to Python
Python is a high-level, interpreted programming language known for its simplicity
and readability. Created by Guido van Rossum and first released in 1991, Python
emphasizes code readability with its use of indentation for code blocks.
Why Learn Python?
• Versatile: Used in web development, data science, automation, AI, and
more.
• Beginner-Friendly: English-like syntax reduces the learning curve.
• Large Community: Extensive libraries (e.g., NumPy, Pandas) and
resources available.
Key Features:
• Dynamically typed.
• Supports multiple paradigms (procedural, object-oriented, functional).
• Cross-platform compatibility.
Setting Up Python
To start coding, install Python from the official website: [Link].
Installation Steps
1. Download the latest version (e.g., Python 3.12).
2. Run the installer and check "Add Python to PATH".
3. Verify installation by opening a terminal and typing:
1
• text
python --version
Integrated Development Environments (IDEs)
• IDLE: Comes with Python (simple for beginners).
• VS Code: Free, extensible with Python extensions.
• PyCharm: Feature-rich for larger projects.
Basic Syntax and Data Types
Python uses indentation (usually 4 spaces) to define code blocks—no curly braces
needed.
Variables
Variables store data. No explicit type declaration required.
python
# Integer
age = 25
# Float
height = 5.9
# String
name = "Alice"
# Boolean
is_student = True
print(f"Hello, {name}. You are {age} years old.") # Output:
Hello, Alice. You are 25 years old.
Data Types Table
Type Description Example
int Whole numbers 42
float Decimal numbers 3.14
str Text "Hello"
bool True/False values True
list Ordered, mutable [1, 2, 3]
collection
dict Key-value pairs {"key": "value"}
2
Lists and Dictionaries
python
# List
fruits = ["apple", "banana", "cherry"]
[Link]("date")
print(fruits) # Output: ['apple', 'banana', 'cherry', 'date']
# Dictionary
person = {"name": "Bob", "age": 30}
print(person["name"]) # Output: Bob
Control Structures
Control flow with conditionals and loops.
If-Else Statements
python
temperature = 25
if temperature > 30:
print("Hot")
elif temperature > 20:
print("Warm")
else:
print("Cool")
# Output: Warm
For Loops
python
for i in range(5):
print(i)
# Output: 0 1 2 3 4
While Loops
python
count = 0
while count < 3:
print(count)
count += 1
# Output: 0 1 2
3
Functions
Functions are reusable blocks of code. Define with def.
python
def greet(name):
return f"Hello, {name}!"
print(greet("World")) # Output: Hello, World!
# Function with default parameter
def add(a, b=10):
return a + b
print(add(5)) # Output: 15
Lambda Functions (Anonymous)
python
square = lambda x: x ** 2
print(square(4)) # Output: 16
Simple Example: A Calculator
Here's a basic command-line calculator.
python
def calculator():
num1 = float(input("Enter first number: "))
op = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))
if op == '+':
print(num1 + num2)
elif op == '-':
print(num1 - num2)
elif op == '*':
print(num1 * num2)
elif op == '/':
if num2 != 0:
print(num1 / num2)
else:
print("Error: Division by zero!")
else:
print("Invalid operator!")
4
# Uncomment to run
# calculator()
Next Steps
• Practice on platforms like LeetCode or Codecademy.
• Explore libraries: Install with pip install numpy and try data manipulation.
• Build projects: A to-do list app or web scraper.
For more resources:
• Official Docs: [Link]
• Free Course: Automate the Boring Stuff with Python