Python Basics
Python Variables & Printing
1. The print() Function (Deep Dive)
Printing Multiple Values
- Pass multiple arguments separated by commas
print()automatically adds a space between them
print("hello", 5, True) # hello 5 True
The Newline Character \n
- By default, every
print()ends with\n(newline character) \nis invisible but tells the console to move to the next line- That's why each
print()call appears on a new line
print("hello") # internally prints: hello\n
print("Tim") # internally prints: Tim\n
The end Keyword Argument
- Overrides what gets printed at the end of the statement
- Default is
end="\n"— change it to keep output on the same line
print("hello", 5, True, end=" | ") # hello 5 True |
print("Tim") # Tim
# Output: hello 5 True | Tim
print("hello", end="world") # helloworld (no newline)
endonly affects that specificprint()call
2. Variables
What is a Variable?
- A named container that stores a value
- Lets you access and manipulate data throughout your program
- Similar to variables in math (e.g.
x = 1)
Defining a Variable
x = 3 # stores integer 3
y = "hello" # stores string "hello"
- Convention: one space on each side of
=
Accessing a Variable
print(x) # 3
print(y) # hello
Variables Storing Other Variables
- Assigning one variable to another copies the value at that moment
- Changing the original later does not affect the copy
x = 3
y = "hello"
num = y # num = "hello" (copies y's current value)
y = x # y is now 3, but num is still "hello"
print(num) # hello
print(y) # 3
Order of Execution
- Python runs code top to bottom, line by line
- A variable's value at any point is whatever was last assigned above that line
x = 3
y = "hello"
print(x) # 3
print(y) # hello ← y hasn't been reassigned yet at this point
num = y
y = x
print(num) # hello
print(y) # 3
x = 4
print(x) # 4
3. Variable Naming Rules
| Rule | Valid | Invalid |
|---|---|---|
Must start with a letter or _ |
name, _name |
1name, @name |
Can contain letters, numbers, _ |
name2, max_score |
name 2, name@2 |
| No spaces allowed | max_score |
max score |
| No special characters | user_name |
user!name, user#name |
| Case sensitive | name ≠ Name |
— |
4. Naming Conventions
Use Meaningful Names
name = "Tim" # ✅ clear purpose
max_score = 10 # ✅ clear purpose
x1 = "Tim" # ❌ unclear purpose
Snake Case vs Camel Case
max_score = 10 # ✅ snake_case — preferred in Python
maxScore = 10 # ❌ camelCase — used in other languages, avoid in Python
Avoid Starting with Uppercase
- Classes in Python start with uppercase — naming variables with uppercase can cause confusion
Name = "Tim" # ❌ avoid — looks like a class
name = "Tim" # ✅ correct
5. Accessing Undefined Variables
- Using a variable that hasn't been defined causes a NameError
print(nam) # NameError: name 'nam' is not defined
Always read the error message — it shows the line number and what went wrong
6. Key Takeaways & Recap
print()can take multiple comma-separated values — auto-adds spaces- Every
print()ends with\nby default — useend=to change this - A variable is a named container storing a value
- Assigning
a = bcopiesb's value at that moment — later changes tobdon't affecta - Code executes top to bottom — order matters
- Variable names: start with letter/
_, no spaces/special chars, case-sensitive, use snake_case