Intro to Computer Science / Unit 4: Programming Basics in Python
4.1 Output and Variables
Notes
The print() function displays output. A variable is a name that stores a value. You create one with the assignment operator =.
- Variable names use letters, digits and underscores, cannot start with a digit, and are case sensitive (
scoreandScoreare different). - Assignment copies the value on the right into the name on the left. A variable can be changed later.
#starts a comment, which Python ignores.
name = "Mia"
points = 10
points = points + 5
print(name, points)
- This prints
Mia 15. Firstpointsis 10, then it is reassigned to 10 + 5.
Practice
What does
x = 4; x = x + 3; print(x) print?x starts at 4, then becomes 4 + 3 = 7.
Which is a valid Python variable name?
Names cannot start with a digit or contain hyphens or spaces. Underscores are allowed.
What does
a = 5; b = a; a = 9; print(b) print?b received the value 5 when it was assigned. Changing a later does not change b.
Ready for more? Take the Unit 4 test.