Variables

A Variable is defined as an abstraction inside of a program that can hold a value or values.

Variables can be named from letters like n for number, or description like name or fav_food. The point of these names is to store some sort of Data to a sensible name.

n = 10
name = "Gerald"
fav_food = "Cookies"

Popcorn Hack: Try making your own set of 3 variable variables

# Popcorn Hack Cell

# Run this cell to define the variables in Python

n = 10
name = "Gerald"
fav_food = "Cookies"

# Complete the Popcorn Hack from the Variables cell

# How could you tell if Python is storing the variables correctly?


Variable Naming

This brings us to the topic of naming Variables. The names of variables are really important when working in groups. For example when one of your teammates review your code they use the names of your variables to quickly understand your code. In the code above you can understand that the variable fav_food represents a favorite food

There are 3 Important Coding Practices to follow when it comes to naming variables

SnakeCase

SnakeCase is where you replace spaces in the words in a variable names to an underscore. This is the standard naming convention for variables in Python.

name_one = "Aashray"

Here’s an example of a SnakeCase variable that uses a _ as a space.

Popcorn Hacks: Now try making your own SnakeCase variable and set the variable equal to a integer.

PascalCase

PascalCase is where you capitialize every word in your variable, but keep it all as one singluar phrase with no spaces. Altough this is shown in example, it should not be used for varialbes in Python. This is reserved for class names.

NameOne = "Chrissie"

Here you can clearly see that the vairable has two diffrent words, and we didn’t need to use a space to seperate it.

Popcorn Hack: Try making your own PascalCase variable

CamelCase

CamelCase is where you captalize the second and subsequent words in the variable name. This is not normally used in Python conventions.

nameOne = "Arushi"

Here the One is captalized to indicate a second word in the variable without using a space.

PopCorn Hack: Try making your own CamelCase

# Popcorn Hack Cell
name_one = "Aashray"
NameOne = "Chrissie"
nameOne = "Arushi"

# All three variables are different.  Programming languages are sensitive to snake case, upper case, etc.
print(name_one, NameOne, nameOne)

# Complete the Popcorn Hack from the previous Variable Naming cell
Aashray Chrissie Arushi

Variable Types

Earlier we explained how to assign variables and properly name them. However in the code above even though I followed all these steps and properly named them I ran into a error.

This is because the types of data, string and int cannot be added. But what are integers and strings?

In python there are multiple types of data that a variable can be, for now lets look at the most commonly use ones.

Integers

Integers are numerical values such as 1, 2, 3, 4 or -1, -2, -3, etc. These are numbers with no decimals and are usually called “ints”.

# Python integer assignment, loosely typed
n = 3

In this case (loosely typed), we call the variable n and assign it the number 3. You don’t need to specify the data type before the variable in Python.

However, in other languages like Java or C++ (strongly typed), you need to prefix the variable name with the word int.

// Java integer assignment using type prefix, strongly typed
int n = 3;

Strings

Strings are a chain of text, numbers or charcters that are all inside of “ “

# camel case, not typically used in python
cookiesStatement = "My Fav cookies are Choclate Chip"
# snake case
cookies_statement = "My Fav cookies are Choclate Chip"

Here we set a string cookieStatement “My Fav Cookies are Choclate Chip” The “ “ marks are what determine it is a string in Python and most coding languages.

Boolean

Booleans are True or False, and they are used for condtional statements

ChrissieGetsSleep = False
    
if ChrissieGetsSleep = True:
    return 1 
 else: 
    return 0

Here we have a if statement that checks if the boolean variable is currently true and if its true it will return 1, but if its false it will return 0.

Float

Floats are numbers that can have decimal values

pi = 3.1415

In this case we assign pi to be 3.1415, and this is a float because of its decimal points.

Lists

Lists are ordered collections of items in Python. They can contain a mix of different data types, including integers, floats, strings, and more. However, it is more common to have list contain the same data type.

my_list = [1, 2, 3, 4, 5]

In this list we have define a series of integers.

Dictionaries in Python

Dictionaries are versatile data structures in Python that store key-value pairs. Each value in a dictionary is associated with a unique key, which allows for efficient lookups and retrieval of values.

my_dict = {
    "name": "Alice",
    "age": 30,
    "city": "San Diego"
}

In this dictionary we are collecting data to represent the attributes of a person.

# Data types cell

# Variable names in Python typically follow snake_case 
friendly_greeting = "Hello"
my_name = "Jane Doe"
my_age = 18

# Printing data types often requires mixing types and formatting them correctly

# print with concatenation
print("Example 1: " + friendly_greeting + " " + my_name + " is see you are " + str(my_age) + " years old.")

# or you can use f-strings
print(f"Exammpl 2: {friendly_greeting} {my_name} is see you are {my_age} years old.")

# or you can separate the variable in the print statement
print("Example 3:", friendly_greeting, my_name, "is see you are", my_age, "years old.")

Example 1: Hello Jane Doe is see you are 18 years old.
Exammpl 2: Hello Jane Doe is see you are 18 years old.
Example 3: Hello Jane Doe is see you are 18 years old.

Addition versus Concatenation

Operaterors such as + will produce different results on different data types. Most are familiar with + on numbers, but on characters it connects the sequences of characters. This connection is called concatenation.

# Addition of two integer variables

int1 = 1
int2 = 2
print("Concatation between two non strings in a print statement will EVALUATE them.")
print("Notice how they get ADDED together.")
print(int1 + int2)
print()

# Concatenation of two string variables

print("Concatation between two  strings in a print statement will CONNECT them.")
print("Notice how they get CONNECTED together.")
string1 = "1"
string2 = "2"
print(string1 + string2) # notice how this CONNECTS the variables

# Print statements like the above can be used to EVALUATE the result of + on two variables

Hacks

Review each of the sections above and produce a python program that stores:

  • Name as a string
  • Age as a integer
  • Favorite food as a string
  • Mix the name, age, food into a List and a Dictionary
  1. Be sure to follow Snake case convention for your variables
  2. Build your own code cell(s) that define each variable types
  3. Experiment with the + operator on string types, integer types, and float types. What operations can the + operator perform?
  4. Provide comments and outputs in the cell that are easy to follow