2️⃣

2.2 Variables

Variables

Variables are things you can store data in. For example, if I want the computer to remember the number 5, I can assign it to a variable as data. You do that by coming up with a variable name (let’s call our variable x) and saying x = 5.
Now when you say x in your code, the computer will remember you saved 5 into x, and recall it. Here are some examples and their corresponding type name in Python:
  • x = 5 → integer (int)
  • a = 9.2 → floating point number (float)
  • hey = 'hello' → string (str)
  • ohmygod = ' random string' → string (str)
  • green = True → boolean (bool)
Example code:
# use variables to store data first_name = "Jane" last_name = 'Doe' age = 25 price = 16.54 is_raining = True can_vote = False # variables can be assigned different values print(first_name) # prints Jane first_name = "John" print(first_name) # prints John
View code on GitHub.

Practice

Store Data

Set a variable named message to "Hi it's a good day" and a variable named x to 24.
 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.