1 · Python Foundations

Getting Started with Python

Learn how Python programs work, how to write and run Python code, how programs store information, and how to build your first interactive programs.

1.1

Getting Started

Before we start building larger programs, we need to understand the environment in which Python programs are written and executed.

You will be able to
  • Explain what Python is.
  • Describe the role of the Python interpreter.
  • Create and run a .py file.
  • Use the Python REPL for simple experiments.
  • Identify the basic parts of a Python development environment.
  • Use comments and indentation correctly.

1. What Is Python?

Python is a programming language used to give computers instructions.

Python is widely used for software development, data analysis, automation, artificial intelligence, web development, scientific computing, and many other areas.

Think of Python as a language.

Just as people use languages to communicate with one another, programmers use programming languages to communicate instructions to computers.

Your First Python Instruction
print("Hello, world!")

This tells Python to display the text:

Hello, world!
Try It — Your First Python Program
Loading Python...
Output
Python is loading...
Food for Thought
  • What do you think would happen if you changed the message inside the quotation marks?
  • Why do you think programming languages have rules for how instructions are written?
Important Idea
  • Python is a programming language.
  • Python allows us to write instructions that a computer can execute.
  • A Python program is made up of Python instructions.

2. The Python Interpreter

The Python interpreter is the program that processes Python code and executes it.

When you give Python an instruction such as:

print("Hello!")

the interpreter processes that instruction and Python produces the requested result.

The Basic Idea
Your Python Code
        ↓
Python Interpreter
        ↓
Program Executes
        ↓
Result
Remember:

You write Python code. The Python interpreter processes that code so that it can be executed.

Food for Thought
  • Why do you think we need something to process the Python code?
  • What do you think would happen if Python could not understand an instruction?
Important Idea
  • The interpreter processes Python instructions.
  • The interpreter allows us to execute Python programs.
  • Errors can occur when Python cannot correctly process our code.

3. Python Files and .py

Python programs are commonly saved in files ending with .py.

For example:

hello.py

The .py extension tells us that the file contains Python source code.

Example — A Python File
# my first Python program

name = "Alex"

print(name)

If this code is saved as:

hello.py

it becomes a Python script that can be run.

Food for Thought
  • Why might saving a program in a file be useful instead of typing it again every time?
  • What do you think the .py extension tells your computer?
Important Idea
  • Python source code can be stored in files.
  • Python files normally use the .py extension.
  • A saved Python file can be run again whenever we need it.

4. VS Code and IDE Basics

A programmer needs a place to write, save, organize, and run code.

An IDE, or Integrated Development Environment, provides tools that make programming easier.

VS Code is a popular code editor that can be configured for Python development.

Common Parts of a Coding Environment
  • Editor: where you write code.
  • File explorer: where you organize files.
  • Terminal: where you can run commands.
  • Output: where you can see program results.
Think of your coding environment as a workshop.

The editor is your workbench, your files are your projects, and the tools around you help you build and test your programs.

Food for Thought
  • Why might a programmer prefer a coding environment instead of a plain text editor?
  • Which tool would you use to write code? Which would you use to run commands?
Important Idea
  • A code editor provides a place to write programs.
  • An IDE provides programming tools in one environment.
  • VS Code can be used to write and work with Python programs.

5. Python REPL

The Python REPL allows you to interact with Python one instruction at a time.

REPL stands for:

Read
Evaluate
Print
Loop

It is especially useful when you want to experiment with a small piece of Python code.

Example
>>> 2 + 3
5

>>> "Hello"
'Hello'

The REPL lets you immediately see what Python does with your instruction.

Food for Thought
  • When would experimenting with one small instruction be easier than creating an entire program?
  • Why might a beginner find the REPL useful while learning Python?
Important Idea
  • The REPL lets you interact with Python one instruction at a time.
  • The REPL is useful for experimentation and quick tests.
  • A .py file is better suited for saving a complete program.

6. Comments

A comment is text written in a program for humans to read.

Python ignores comments when executing the program.

Example
# This is a comment

print("Hello!")

The line beginning with # is a comment. It does not produce output.

Try It — Comments
Loading Python...
Output
Python is loading...
Food for Thought
  • Why might comments become more useful as programs become larger?
  • What information would you want another programmer to know when reading your code?
Important Idea
  • Comments explain code to humans.
  • Python ignores comments when executing the program.
  • Comments can make code easier to understand and maintain.

7. Indentation

Python uses indentation to organize blocks of code.

For example:

if True:
    print("This code is indented.")

The indented line belongs to the if statement.

Important:

In Python, indentation is not simply visual formatting. It is part of the structure of the program.

Try It — Indentation
Loading Python...
Output
Python is loading...
Food for Thought
  • Why do you think programming languages need a way to organize related instructions?
  • What might happen if Python could not tell which instructions belonged together?
Important Idea
  • Indentation organizes blocks of Python code.
  • Indentation is part of Python's syntax.
  • Consistent indentation makes code easier to read and understand.
1.2

Variables & Assignment

We now know how to write and run Python programs. The next question is:

How can a program remember information?

Variables give our programs a way to give names to values so that we can use those values later.

You will be able to
  • Explain what a variable is.
  • Use assignment statements.
  • Create variables with meaningful names.
  • Follow basic Python naming conventions.
  • Reassign variables.
  • Use multiple assignment.
  • Recognize the purpose of constants.

1. Variables

A variable is a name that refers to a value.

For example:

name = "Alex"
age = 20

We have created two variables:

Using Variables
name = "Alex"
age = 20

print(name)
print(age)
Try It — Variables
Loading Python...
Output
Python is loading...
Food for Thought
  • Why is age a better variable name than x in a student program?
  • What information might you want your program to remember?
Important Idea
  • Variables give names to values.
  • Variables allow programs to store and use information.
  • Meaningful variable names make programs easier to understand.

2. Assignment Statements

The = symbol is called the assignment operator.

It assigns the value on the right to the variable on the left.

age = 20

Read this as:

Assign the value 20 to the variable named age.

Important beginner idea:

The = sign does not mean "is equal to" in the mathematical sense.

In Python, it means: assign this value to this variable.

Example
name = "Alex"
age = 20
city = "Newark"
Food for Thought
  • What value is being assigned to age?
  • Which side of the assignment contains the variable name?
Important Idea
  • Assignment stores a value in a variable.
  • The variable name appears on the left side.
  • The value or expression being assigned appears on the right side.

3. Naming Conventions

Good variable names help people understand what a program does.

Python variable names commonly use snake_case.

first_name = "Alex"
student_age = 20
total_price = 49.99
Better Names
name = "Alex"
age = 20
course_name = "Python"
Less Helpful Names
x = "Alex"
a = 20
thing = "Python"

These names may work, but they do not communicate much information to someone reading the program.

Food for Thought
  • Which name makes the purpose clearer: student_age or x?
  • How might good names help you debug a program later?
Important Idea
  • Variable names should communicate meaning.
  • Python commonly uses snake_case for variable names.
  • Good names make code easier to read.

4. Reassignment

A variable can be assigned a new value.

age = 20

age = 21

print(age)

The final value of age is:

21
Try It — Reassignment
Loading Python...
Output
Python is loading...
Food for Thought
  • What happens to the old value when a variable is reassigned?
  • Where might reassignment be useful in a real program?
Important Idea
  • Variables can be reassigned.
  • A variable can refer to a different value later in a program.
  • The current value is the value most recently assigned.

5. Multiple Assignment

Python allows multiple variables to be assigned in one statement.

first_name, last_name = "Alex", "Smith"

This assigns:

first_name → "Alex"
last_name  → "Smith"
Try It — Multiple Assignment
Loading Python...
Output
Python is loading...
Important Idea
  • Python can assign multiple variables in one statement.
  • Each variable receives its corresponding value.

6. Constants

A constant is a value that a programmer intends to keep unchanged.

Python does not enforce constants in the same way some languages do. Instead, programmers commonly use uppercase names to communicate their intention.

PI = 3.14159
MAX_LOGIN_ATTEMPTS = 3
Remember:

Uppercase naming communicates that a value is intended to be treated as a constant. It does not make the variable impossible to change.

Food for Thought
  • Why might a program need values that are intended to remain unchanged?
  • Why would MAX_LOGIN_ATTEMPTS be easier to understand than simply writing 3 everywhere?
Important Idea
  • Constants represent values that are intended to remain unchanged.
  • Python programmers commonly use uppercase names for constants.
  • The uppercase convention communicates programmer intent.
1.3

Data Types

We now know that variables allow programs to store information.

But not all information is the same.

Think about different kinds of information.

A person's age, a person's name, the price of a product, and whether someone is a student are all pieces of information, but they are different kinds of values.

You will be able to
  • Identify common Python data types.
  • Use int, float, str, bool, and None.
  • Use type() to inspect a value's type.
  • Explain why different types represent different kinds of information.

1. Integers — int

An integer is a whole number without a decimal part.

age = 20
students = 25
temperature = -5

These values are integers.

Try It — Integers
Loading Python...
Output
Python is loading...
Important Idea
  • int represents whole numbers.
  • Examples include 10, 0, and -5.

2. Floating-Point Numbers — float

A float represents a number with a decimal component.

price = 19.99
temperature = 72.5
gpa = 3.8
Try It — Floats
Loading Python...
Output
Python is loading...
Important Idea
  • float represents numbers with decimal values.
  • Examples include 3.14, 19.99, and 3.8.

3. Strings — str

A string is a sequence of characters used to represent text.

name = "Alex"
city = "Newark"
course = "Python"

Strings are usually written inside quotation marks.

Try It — Strings
Loading Python...
Output
Python is loading...
Important:

These are different values:

20
"20"

The first is an integer. The second is a string containing the characters 2 and 0.

Food for Thought
  • Why might a person's age be stored as an integer rather than a string?
  • When might the characters "20" be useful as text?
Important Idea
  • str represents text.
  • Strings are written using quotation marks.
  • 20 and "20" are different types of values.

4. Boolean Values — bool

A boolean represents one of two logical values:

True
False

Booleans are useful when a program needs to represent something that can be yes/no, on/off, or true/false.

Example
is_student = True
has_license = False
Try It — Booleans
Loading Python...
Output
Python is loading...
Important Idea
  • bool represents logical values.
  • There are two boolean values: True and False.
  • Booleans become especially important when programs make decisions.

5. None

Python uses None to represent the absence of a value or the idea that there is currently no meaningful value.

result = None

print(result)
print(type(result))
Important:

None is not the same thing as 0, False, or an empty string.

Important Idea
  • None represents the absence of a value.
  • None is its own special value in Python.
  • It is often used when a value does not yet exist or is not available.

6. The type() Function

The type() function tells us what type of value we are working with.

age = 20

print(type(age))

Python reports:

<class 'int'>
Try It — Identify the Types
Loading Python...
Output
Python is loading...
Food for Thought
  • Why is knowing a value's type useful to a programmer?
  • What problems might occur if you assume a value is a number when it is actually text?
Important Idea
  • Python values have types.
  • type() allows us to inspect a value's type.
  • Understanding types is essential for writing correct programs.
1.4

Input & Output

So far, we have written programs that use information we provide directly in the code.

Real programs need to communicate with users.

Think of a conversation.

A program can produce information for the user and ask the user for information in return.

You will be able to
  • Use print() to display information.
  • Use input() to receive user input.
  • Explain why input() produces a string.
  • Convert strings into integers and floats.
  • Convert values to strings using str().

1. Output with print()

The print() function displays information to the user.

print("Hello!")
print(25)
print(3.14)
Try It — print()
Loading Python...
Output
Python is loading...
Important Idea
  • print() displays information.
  • We can print strings, numbers, variables, and expressions.

2. Input with input()

The input() function allows a program to ask the user for information.

name = input("What is your name? ")

print(name)

The program pauses and waits for the user to enter something.

Try It — User Input
Loading Python...
Output
Python is loading...

3. Important: input() Returns a String

This is one of the most important ideas in beginner Python.

Even if the user enters a number, input() gives the program a string.

age = input("How old are you? ")

print(type(age))

If the user enters:

20

the value stored in age is:

"20"

It is text, not an integer.

Try It — What Type Is Input?
Loading Python...
Output
Python is loading...
Remember this:

input() always gives you text.

If you need a number, you must convert the input.

Important Idea
  • input() receives information from the user.
  • The result of input() is a string.
  • Numbers entered by users must be converted if you want to perform numerical operations.

4. Converting Input with int()

The int() function converts a suitable value into an integer.

age = int(input("How old are you? "))

print(age)

Now the user's input is converted from text into an integer.

Try It — Convert to Integer
Loading Python...
Output
Python is loading...
Important Idea
  • int() converts suitable values to integers.
  • It is commonly used when numerical input is expected.

5. Converting Input with float()

The float() function converts a suitable value into a floating-point number.

price = float(input("Enter the price: "))

print(price)
Try It — Convert to Float
Loading Python...
Output
Python is loading...
Important Idea
  • float() converts suitable values to floating-point numbers.
  • It is useful when users enter values such as prices, measurements, or decimal numbers.

6. Converting Values with str()

The str() function converts a value into a string.

age = 20

age_text = str(age)

print(age_text)
print(type(age_text))
Try It — Convert to String
Loading Python...
Output
Python is loading...
Important Idea
  • str() converts a value into text.
  • Type conversion allows programs to work with values in different forms.

7. Putting Everything Together

We now have enough knowledge to build a small interactive program.

The program will:

  1. Ask for the user's name.
  2. Ask for the user's age.
  3. Ask for the user's GPA.
  4. Convert numerical input into the appropriate types.
  5. Display the information.
Mini Program — Student Profile
Loading Python...
Output
Python is loading...
Food for Thought
  • Which variables store strings?
  • Which variables store numbers?
  • Why do we use int() for age?
  • Why do we use float() for GPA?
  • What would happen if we removed the conversions?
Important Idea
  • Programs can receive information using input().
  • Input initially arrives as strings.
  • We can convert input using int() and float().
  • Variables allow us to store and reuse the information.
  • print() allows the program to communicate results.

8. The Big Picture

You have now learned the core building blocks needed to write simple Python programs.

Python Program
      ↓
Variables
      ↓
Values
      ↓
Data Types
      ↓
Input
      ↓
Processing
      ↓
Output
Remember the progression:

First, you learned how to run Python.

Then, you learned how programs store information.

Then, you learned that different values have different types.

Finally, you learned how programs communicate with users.

What You Know Now
  • You can write and run Python code.
  • You understand Python files and the REPL.
  • You can use comments and indentation.
  • You can create and modify variables.
  • You understand basic Python data types.
  • You can inspect types with type().
  • You can display information with print().
  • You can receive information with input().
  • You can convert user input into numbers.
  • You can build a simple interactive Python program.

Exit Ticket

Before moving to the next topic, answer these questions without looking back at the examples.

  1. What is the purpose of the Python interpreter?
  2. What is a .py file?
  3. What is the difference between a Python file and the REPL?
  4. What is a variable?
  5. What does the = operator do in Python?
  6. What is the difference between 20 and "20"?
  7. What is the difference between an int and a float?
  8. What are the two possible boolean values?
  9. What does type() do?
  10. What does print() do?
  11. What does input() return?
  12. Why would you use int(input(...)) instead of simply input(...) when asking for an age?
  13. Write a small Python program that asks for a user's name and age and then displays both.