← Back to Python Chapter 1: The First Bit
0% Complete

Welcome to Python!

Python is one of the most popular programming languages in the world. It's known for its clean syntax, readability, and versatility. Whether you want to build websites, analyze data, automate tasks, or create AI - Python can do it all.

Why Python?

  • Beginner-Friendly: Python reads almost like English
  • Powerful: Used by NASA, Google, Netflix, and Instagram
  • Versatile: Web development, AI, data science, automation, security
  • Huge Community: Millions of developers, endless resources

Key Concept

Python uses indentation (spaces or tabs) to define code blocks, unlike other languages that use braces {}. This forces clean, readable code!

Your First Python Program

The tradition in programming is to start with "Hello, World!" - let's see how simple it is in Python:

hello.py Python
print("Hello, World!")
Hello, World!

That's it! One line of code. In other languages, this might take 5-10 lines. Python's philosophy is: "Simple is better than complex."

Lab Exercise 1: Hello World

Beginner

Instructions

Write your first Python program! Use the print() function to display a message.

Your Task

Write code to print "Hello, Hexworth!" to the console.

Code Editor
Output
Click "Run Code" to see output...

Checkpoint: Introduction Complete!

You've written your first Python program. Ready for variables?

Variables: Storing Information

A variable is like a labeled box where you can store information. You give the box a name, and you can put data inside it, retrieve it later, or change what's inside.

Creating Variables

In Python, you create a variable by giving it a name and assigning a value with the = operator:

variables.py Python
# Creating variables name = "Alice" age = 25 height = 5.7 is_student = True # Using variables print(name) print(age)
Alice 25

Variable Naming Rules

Must: Start with a letter or underscore, contain only letters, numbers, and underscores.
Cannot: Start with a number, contain spaces, or use Python keywords (like if, for, while).
Best Practice: Use descriptive names like user_age instead of x.

Reassigning Variables

You can change what's stored in a variable at any time:

reassign.py Python
score = 0 print("Starting score:", score) score = 100 print("New score:", score) score = score + 50 print("After bonus:", score)
Starting score: 0 New score: 100 After bonus: 150

Lab Exercise 2: Working with Variables

Beginner

Instructions

Practice creating and using variables. Store your information in variables and print them out!

Your Task

Create three variables: my_name (your name as a string), my_age (your age as a number), and my_hobby (one of your hobbies). Then print all three.

Code Editor
Output
Click "Run Code" to see output...

Checkpoint: Variables Mastered!

You know how to create and use variables. Time to learn about data types!

Data Types: Different Kinds of Information

Python can work with many different types of data. Each type has its own characteristics and operations. The main built-in types are:

Core Data Types

Type Description Example
int Whole numbers (integers) 42, -7, 0
float Decimal numbers 3.14, -0.5, 2.0
str Text (strings) "Hello", 'Python'
bool True or False True, False
list Ordered collection [1, 2, 3], ["a", "b"]
dict Key-value pairs {"name": "Alice"}

Checking Types

Use the type() function to check what type a value is:

types.py Python
print(type(42)) # <class 'int'> print(type(3.14)) # <class 'float'> print(type("Hello")) # <class 'str'> print(type(True)) # <class 'bool'> print(type([1, 2, 3])) # <class 'list'>

Type Conversion

You can convert between types using functions like int(), float(), str():
int("42") → 42
str(100) → "100"
float(5) → 5.0

Lab Exercise 3: Exploring Data Types

Beginner

Instructions

Use the type() function to explore different data types. Then practice type conversion!

Your Task

1. Create a variable num_str with the value "100" (as a string)
2. Convert it to an integer using int() and store in num_int
3. Print both variables and their types

Code Editor
Output
Click "Run Code" to see output...

Checkpoint: Data Types Understood!

You know the core data types. Let's learn about input and output!

Print & Input: Communicating with Users

Programs need to communicate with users - displaying information and receiving input. Python makes this easy with print() and input().

The print() Function

You've already used print()! Let's explore its features:

printing.py Python
# Basic print print("Hello!") # Print multiple items (separated by space) print("My age is", 25) # Custom separator print("A", "B", "C", sep="-") # f-strings (formatted strings) - POWERFUL! name = "Alice" age = 25 print(f"My name is {name} and I am {age} years old")
Hello! My age is 25 A-B-C My name is Alice and I am 25 years old

f-strings (Formatted String Literals)

f-strings are Python's most powerful way to format output. Put f before the quotes, then use {variable} to insert values. You can even do math: f"2+2={2+2}" prints "2+2=4"

The input() Function

Get input from the user. Note: input() always returns a string!

input_example.py Python
# Get user's name name = input("What is your name? ") print(f"Hello, {name}!") # Get a number (must convert!) age_str = input("How old are you? ") age = int(age_str) # Convert string to int print(f"Next year you'll be {age + 1}!")

Lab Exercise 4: User Interaction

Beginner

Instructions

Practice using f-strings to create formatted output. Since we can't use real input() in the browser, we'll simulate it!

Your Task

Create a greeting message using f-strings. Given name = "Wizard" and level = 7, print: "Welcome, Wizard! You are level 7."

Code Editor
Output
Click "Run Code" to see output...

Checkpoint: I/O Complete!

You can now communicate with users. One more topic - comments!

Comments: Notes for Humans

Comments are notes in your code that Python ignores. They're for you and other programmers to understand what the code does. Good comments = good code!

Types of Comments

comments.py Python
# This is a single-line comment # Python ignores everything after # """ This is a multi-line comment (docstring). It can span multiple lines. Often used to document functions and classes. """ # Inline comment: x = 10 # This sets x to 10 # Comments help explain WHY, not WHAT: # BAD: Set x to 10 # GOOD: Initialize counter for retry loop

Comment Best Practices

Do: Explain complex logic, document assumptions, note TODOs.
Don't: State the obvious, write novels, let comments get outdated.
Remember: Code tells you HOW, comments tell you WHY.

Commenting Out Code

Comments are also useful for temporarily disabling code while debugging:

debug.py Python
print("This will run") # print("This won't run - commented out!") print("This will also run")

Lab Exercise 5: Commenting Code

Beginner

Instructions

Practice adding meaningful comments to code. Remember: explain WHY, not WHAT!

Your Task

Add a comment explaining what this code does and WHY the number 3 is used:

Code Editor
Output
Click "Run Code" to see output...

Checkpoint: Comments Complete!

You know how to document your code. Time for the summary!

Chapter 1 Summary

Congratulations! You've completed Chapter 1 and learned the fundamentals of Python programming. Let's recap what you've mastered:

What You Learned

  • Python Basics: Python's philosophy of simplicity and readability
  • Variables: Creating, naming, and reassigning variables
  • Data Types: int, float, str, bool, list, dict and type conversion
  • Print & Input: f-strings, formatted output, and user interaction
  • Comments: Single-line, multi-line, and best practices

Key Takeaways

Variables

Use descriptive names. Think of them as labeled boxes.

Data Types

Know your types. Use type() and conversion functions.

f-strings

f"text {variable}" is your best friend for output.

Comments

Explain WHY, not WHAT. Future you will thank you.

Chapter 1 Complete!

You've mastered the fundamentals. Ready to prove your skills?

Take the Chapter 1 Practical Exam