Intro .1 .2 .3 .4 .5 .6 .7 .8 .9 .10 .11 .12 .13 .14 .15 .16 .17 .18

Variables

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

Variables can be named from letters like X, Y, Z to phrases like APIKEY. The point of these names is to store some sort of Data to a resuable value.

x = 10
name = "Gerald"
fav_food = "Cookies"

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.

    variable_one = "Aashray"

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

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.

    VariableOne = "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.

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.

    variableOne = "Arushi"

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

Try making your own CamelCase

## Attempt to add an integer and a string
myInteger = 42
myString = "Hello"

# Try to add them together
result = myInteger + myString

#You can't!

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

/Users/isabelmarilla/nighthawkcodingsocietyreal/portfolio_2025/_notebooks/2024-09-24-big-idea-3-1.ipynb Cell 4 line 6
      <a href='vscode-notebook-cell:/Users/isabelmarilla/nighthawkcodingsocietyreal/portfolio_2025/_notebooks/2024-09-24-big-idea-3-1.ipynb#W3sZmlsZQ%3D%3D?line=2'>3</a> myString = "Hello"
      <a href='vscode-notebook-cell:/Users/isabelmarilla/nighthawkcodingsocietyreal/portfolio_2025/_notebooks/2024-09-24-big-idea-3-1.ipynb#W3sZmlsZQ%3D%3D?line=4'>5</a> # Try to add them together
----> <a href='vscode-notebook-cell:/Users/isabelmarilla/nighthawkcodingsocietyreal/portfolio_2025/_notebooks/2024-09-24-big-idea-3-1.ipynb#W3sZmlsZQ%3D%3D?line=5'>6</a> result = myInteger + myString
      <a href='vscode-notebook-cell:/Users/isabelmarilla/nighthawkcodingsocietyreal/portfolio_2025/_notebooks/2024-09-24-big-idea-3-1.ipynb#W3sZmlsZQ%3D%3D?line=7'>8</a> #You can't!


TypeError: unsupported operand type(s) for +: 'int' and 'str'

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 a numerical value going from 1,2,3,4 or -1,2,-3 etc. These are numbers with no decimals and are ussualy called ints

    int x = 3

    print(x)

In this case we call int x to be 3. Normally you don’t need to say the data type before the variable in python, however in other languages like JS or C++ you would need to.

Strings

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

    str Cookies = " My Fav cookies are Choclate Chip "

Here we set a string cookie to be representing the statement that “ My Fav Cookies are Choclate Chip” The “ “ marks are what determine it is a string in most coding languages.

Boolean

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

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

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

Float

Floats are a integers that can have decimal values

    x = 3.1415
    print(x)

In this case we call int x 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.

my_list = [1, 2, 3, 4, 5]
print(my_list)

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": "Wonderland"
}
print(my_dict)

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 which has a dictionary that stores:

  • Name as a string
  • Age as a integer
  • Favorite food as a string
  • Balance as a float
  • Hobbies as a list
  1. Be sure to follow Snake case convention for your variables
  2. Build your own code cells using each of the variable types
  3. Experiment with the + operator on each of the variable types and output the results.