Variables
- An abstraction inside a program that can hold a value.
- Think of them as containers or boxes for data.
- They help manage and organize data in your code, making it more organized, readable, and adaptable.
Different Data Types for Variables
- Integer: Used for numbers used in mathematical operations (e.g., age or temperature).
- Text (String): Used for text, lists, and words (e.g., name, phone number, or address).
- Boolean: Used for representing true or false values (e.g., “Is it raining?”).
Correct Way to Name Variables
- Should be short, descriptive, and easy to distinguish.
- Must start with a letter or underscore (not a number).
- Can only have alphanumeric characters or underscores.
- Are case sensitive.
- Cannot be Python keywords (e.g., ‘else’).
Hacks
Storing if someone’s pet is a dog:
- Variable Name:
isDog
- Data Type: Boolean
Storing someone’s birthday:
- Variable Name:
birthday
- Data Type: Text (String)
Storing the amount of money someone is spending:
- Variable Name:
amountSpent
- Data Type: Float (for decimal values)
Storing if it is sunny:
- Variable Name:
isSunny
- Data Type: Boolean
Primitive vs. Collection Data Types
Primitive Data Types
- int
- string
- float
- boolean
Collection Data Types
- list
- dictionary
Concatenation
- Concatenation is the joining of strings and other types.
- Example:
myFirstName + " " + myLastName
Formatting
- Allows you to display values using predefined rules.
- Example:
message = "My first name is {0} and my last name is {1}"