Learn how Python programs work, how to write and run Python code, how programs store information, and how to build your first interactive programs.
Before we start building larger programs, we need to understand the environment in which Python programs are written and executed.
.py file.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.
Just as people use languages to communicate with one another, programmers use programming languages to communicate instructions to computers.
print("Hello, world!")
This tells Python to display the text:
Hello, world!
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.
Your Python Code
↓
Python Interpreter
↓
Program Executes
↓
Result
You write Python code. The Python interpreter processes that code so that it can be executed.
.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.
# 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.
.py extension tells your computer?.py extension.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.
The editor is your workbench, your files are your projects, and the tools around you help you build and test your programs.
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.
>>> 2 + 3 5 >>> "Hello" 'Hello'
The REPL lets you immediately see what Python does with your instruction.
.py file is better suited for saving a complete program.A comment is text written in a program for humans to read.
Python ignores comments when executing the program.
# This is a comment
print("Hello!")
The line beginning with # is a comment.
It does not produce output.
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.
In Python, indentation is not simply visual formatting. It is part of the structure of the program.
We now know how to write and run Python programs. The next question is:
Variables give our programs a way to give names to values so that we can use those values later.
A variable is a name that refers to a value.
For example:
name = "Alex" age = 20
We have created two variables:
nameagename = "Alex" age = 20 print(name) print(age)
age a better variable name than x in a student program?
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.
The = sign does not mean "is equal to"
in the mathematical sense.
In Python, it means: assign this value to this variable.
name = "Alex" age = 20 city = "Newark"
age?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
name = "Alex" age = 20 course_name = "Python"
x = "Alex" a = 20 thing = "Python"
These names may work, but they do not communicate much information to someone reading the program.
student_age or x?A variable can be assigned a new value.
age = 20 age = 21 print(age)
The final value of age is:
21
Python allows multiple variables to be assigned in one statement.
first_name, last_name = "Alex", "Smith"
This assigns:
first_name → "Alex" last_name → "Smith"
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
Uppercase naming communicates that a value is intended to be treated as a constant. It does not make the variable impossible to change.
MAX_LOGIN_ATTEMPTS be easier to understand than simply writing 3 everywhere?We now know that variables allow programs to store information.
But not all information is the same.
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.
int, float, str, bool, and None.type() to inspect a value's type.int
An integer is a whole number without a decimal part.
age = 20 students = 25 temperature = -5
These values are integers.
int represents whole numbers.10, 0, and -5.float
A float represents a number with a decimal component.
price = 19.99 temperature = 72.5 gpa = 3.8
float represents numbers with decimal values.3.14, 19.99, and 3.8.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.
These are different values:
20 "20"
The first is an integer.
The second is a string containing the characters 2
and 0.
"20" be useful as text?str represents text.20 and "20" are different types of 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.
is_student = True has_license = False
bool represents logical values.True and False.
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))
None is not the same thing as 0,
False, or an empty string.
None represents the absence of a value.None is its own special value in Python.type() Function
The type() function tells us what type of value
we are working with.
age = 20 print(type(age))
Python reports:
<class 'int'>
type() allows us to inspect a value's type.So far, we have written programs that use information we provide directly in the code.
Real programs need to communicate with users.
A program can produce information for the user and ask the user for information in return.
print() to display information.input() to receive user input.input() produces a string.str().print()
The print() function displays information to the user.
print("Hello!")
print(25)
print(3.14)
print() displays information.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.
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.
input() always gives you text.
If you need a number, you must convert the input.
input() receives information from the user.input() is a string.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.
int() converts suitable values to integers.float()
The float() function converts a suitable value into
a floating-point number.
price = float(input("Enter the price: "))
print(price)
float() converts suitable values to floating-point numbers.str()
The str() function converts a value into a string.
age = 20 age_text = str(age) print(age_text) print(type(age_text))
str() converts a value into text.We now have enough knowledge to build a small interactive program.
The program will:
int() for age?float() for GPA?input().int() and float().print() allows the program to communicate results.You have now learned the core building blocks needed to write simple Python programs.
Python Program
↓
Variables
↓
Values
↓
Data Types
↓
Input
↓
Processing
↓
Output
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.
type().print().input().Before moving to the next topic, answer these questions without looking back at the examples.
.py file?
= operator do in Python?
20 and "20"?
int and a
float?
type() do?
print() do?
input() return?
int(input(...)) instead of
simply input(...) when asking for an age?