Expressions, Variables, and Data Types
Table of Contents
Expressions
An expression in Python is a combination of values, variables, operators, and function calls. When evaluated, an expression produces a value.
Examples
3 + 5
→8
x - 2
(assumingx = 10
) →8
"Hello" + " World"
→"Hello World"
Key Operators in Python
- Arithmetic Operators:
+
(Addition):5 + 3
→8
-
(Subtraction):10 - 2
→8
*
(Multiplication):4 * 2
→8
/
(Division):16 / 2
→8.0
//
(Floor Division):16 // 3
→5
gives only the whole number part (quotient) of a division%
(Modulus):16 % 3
→1
gives only the remainder of a division**
(Exponentiation):2 ** 3
→8
- String Operators:
+
(Concatenation):"hello" + "world"
→helloworld
combines two strings into one.*
(Repetition):"hi" * 4
→hihihihi
repeats the string an integer number of times.
Evaluating Expressions
Python follows a specific order of operations when evaluating expressions, often called “PEMDAS”:
- Parentheses
()
: Highest precedence, anything in parentheses gets evaluated first. - Exponents
**
: Exponentiation happens next. - Multiplication
*
, Division/
and//
, Modulo%
: These are processed left to right. - Addition
+
and Subtraction-
: These are also processed left to right.
When evluating a complicated expression, work from the innermost parentheses out. Example:
(2 + (3 * 4)) ** 2
(2 + 12) ** 2
14 ** 2
196
Variables
A variable is a name that refers to a value stored in memory. Variables allow us to store and manipulate data. You can assign values to variables using the =
operator.
Declaring and Assigning Variables
x = 10
y = 20
name = "Alice"
- Variable names should be descriptive and follow these rules:
- Must begin with a letter or underscore (
_
) - Can only contain letters, numbers, and underscores
- Cannot be a Python keyword (e.g.,
if
,else
,while
)
- Must begin with a letter or underscore (
Data Types
Python has several built-in data types. Each data type determines what kind of value can be stored and what operations can be performed on that value. In this class, we are concerned with the following four data types.
Numbers
- Integers (int): Whole numbers (positive or negative).
- Example:
x = 5
- Example:
- Floats (float): Numbers with decimal points.
- Example:
pi = 3.14
- Example:
Strings (str)
- A string is a sequence of characters enclosed in quotes (single, double, or triple).
- Example:
greeting = "Hello"
- You can access individual characters with indexing (
greeting[0]
gives"H"
) and slice strings withgreeting[1:4]
.
- Example:
Booleans (bool)
- Booleans represent True or False values.
- Example:
is_student = True
- Example:
Type Conversion
Sometimes you need to convert between data types.
- int(): Convert to an integer.
- float(): Convert to a float.
- str(): Convert to a string.
- bool(): Convert to a bool.
Examples
int("5") # Converts the string "5" to the integer 5
float(10) # Converts the integer 10 to the float 10.0
str(123) # Converts the integer 123 to the string "123"
Input and Output
- Use
input()
to get user input (returns a string).input()
prompts the user with instructions you provide, and then waits for the user to type in a result.input()
always returns a string! If you are trying to work with numbers, you may need to convert the result (see Type Conversion above).
- Use
print()
to output data to the console.
Example
name = input("Enter your name: ")
print("Hello, " + name + "!")
Common Errors
- SyntaxError: Occurs when the code violates Python’s grammar rules.
- Example: Missing a closing parenthesis
)
at the end of aprint
statement.
- Example: Missing a closing parenthesis
- TypeError: Occurs when an operation is applied to an object of inappropriate type.
- Example: Adding a string to an integer (
"3" + 3
).
- Example: Adding a string to an integer (
Practice Problems
- Write a Python program to calculate the area of a rectangle. Prompt the user for length and width.
- Write a program that converts a temperature in Celsius to Fahrenheit using the formula \(F = C \times 1.8 + 32\). Have the user input a temperature in Celsius.
- Write a program that prompts the user for a number of minutes and displays that duration in hours and minutes. For example, 135 minutes would be 2 hours and 15 minutes.