Notes

Variables

  • An abstraction inside a program that can hold a value.
  • Can be thought of as a container or a box that holds information that your program can use or modify.
  • Help you manage and organize your work with data, making your code more organized, readable, and adaptable.

Data types to store variables in

  • Integer: Used to store numbers that can later be used in mathematical operations, e.g., age or temperature.
  • Text (String): Used to store text, lists, and words that can later be referenced, e.g., Name, phone number, or address.
  • Boolean: Used to store simply true or false values, e.g., is it raining.

Naming variables

  • DON’T DO:
    • Too long and can make the code messy.
    • Dashes and spaces are not allowed when naming variables.
  • DO:
    • Should be short, descriptive, and easy to distinguish the type of variable.
    • Variable names must start with a letter or an underscore.
    • They cannot start with a number.
    • Variable names can only have alphanumeric characters or underscores.
    • Variable names are case sensitive.
    • Variable names cannot be Python keywords, such as ‘else’.

Primitive vs. Collection Data Types:

Primitive:

  • int
  • string
  • float
  • boolean

Collection Data Types:

  • list
  • dictionary

Concatenation:

  • The joining of strings and other types
  • Can perform mathematical operations, e.g., addition.

Formatting:

  • Allows us to display values using pre-decided rules.
  • Can format strings and other data types as needed.

Things to remember for lists:

  • Use to store multiple elements.
  • Data abstraction provides a separation between abstract parts of a data type and the actual details of it.
  • Can be created using list notations from the exam reference sheet.
  • The reference sheet has a list structure with index values 1-# of elements in the list. If a list index is less than 1 or greater than the list, it will produce an error.
  • Lists are good as they result in: fewer variables, more consistency, and improved readability.

Homework

# HW 1 - Integer addition
num1Int = 10
num2Int = 5
sum_result = num1Int + num2Int
print("Sum:", sum_result)
# HW 2 - Float division
float1 = 7.5
float2 = 2.0
quotient_result = float1 / float2
print("Quotient:", quotient_result)
# HW 3 - String formatting
message = "My name is {}, and I live in {}."
name = "David"
location = "San Diego"
formatted_message = message.format(name, location)
print(formatted_message)
# HW 4 - List creation
var1 = 7
var2 = 9
var3 = 2
var4 = 8
varsList = [var1, var2, var3, var4]
print(varsList)