The Magic of Python Variables

Variables essentially act as a storage containers to store information

For Example:

example = 3
starter = "Hello world!!"
value= "variable"

Then, we can do:

print(starter)
print(example)
print(value)
Hello world!!
3
variable

We use variables because it allows us to have more efficency when creating our code. There are many ways to excute and code a program, however we always want to find the most efficent way. Let’s look at this example

print("Hello World, Welcome")
print("Hello World, Welcome")
print("Hello World, Welcome")
print("Hello World, Welcome")
print("Hello World, Welcome")
Hello World, Welcome
Hello World, Welcome
Hello World, Welcome
Hello World, Welcome
Hello World, Welcome

This is really inefficent and time consuming, also repetive instead we can make a variable assignment,.

hi= "Hello World, Welcome"

print(hi)
print(hi)
print(hi)
print(hi)
print(hi)

Hello World, Welcome
Hello World, Welcome
Hello World, Welcome
Hello World, Welcome
Hello World, Welcome

This is way less effort you need to put in, and also, much more efficent code than typing hello world 5 times. There is an even more efficent way to set this code up with For Loops, but that is for a later lesson,

Variable Naming:

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, functions, and methods in Python.

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 the convention for class names.

CamelCase

CamelCase is where you captalize the second and subsequent words in the variable name. This is not normally used in Python conventions except when naming UPPER_CASE_CONSTANTS (PI_CONSTANT=3.14)

Popcorn Hack # 1

Try making your own PascalCase variable and set the variable to a string.

Popcorn Hack # 2

Now try making your own CamelCase variable and set the variable equal to a integer.

Variable Types

In Python there are many variables Some include…

Integers

Integers are numerical values such as 1, 2, 3, 4 or -1. There are no decimals in an integer. ‘’’

These values must be whole digit numbers

int_var = 10

Strings

Strings are a chain of text, numbers or characters, all inside of “ “ ‘’’

Numbers can be set as strings but must also be included in “ “ or using the str() function.

# Numbers can be set as strings but must also be included in " " or using the str() function.
str_var = "Hello World"

# Convert any value into a string using the str() function
int_var = 104
str_var = str(int_var) # Converted 104 to "104"

# You can access different parts of the string using brackets
letter_one = str_var[0]

# You can also get groups of letters using brackets
half = str_var[0:5]

# To split the string into parts you can choose which parts to split at and then it will convert into a list
str_var.split(" ") # Split at space making str_var = ["Hello","World"]

# To rejoin split strings, use the join() function
rejoin_str = ","
rejoin_str.join(str_var) # the join() function joins a list using a rejoin string, in this example a command
'1,0,4'

Boolean

Booleans are True or False, they are used for conditional statements (Usually in if statements or while loops). ‘’’

These can be extremely useful in if statements.

bool_var = True 

if bool_var:
 int_var = 10
else:
 str_var = "Hello World"

Float

Floats are numbers that have decimals

# Float Examples
pi = 3.141
e = 2.718
atmGasConstant = 0.08206
standardTemperature = 273.15
faradayConstant = 96485.00

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.

# Lists are very useful to store data for a later use, its like a bunch of variables compacted into one.
lst = ["s", "i", "g", "m", "a"]

# To retrieve something from a list, you take the index or the list number like this.
lst[0]

lst[0:3] # You can grab values from one index to another like in strings

# Index can also be found with the list.index() function
lst.index("g")

# To add a value to the list, you can use the list.append() function
lst.append("69")

Output: lst = ["s", "i", "g", "m", "a", 69] # Lists can include different kinds of values (int, str, bool, float, list)

# To remove a value from the list, you can use the list.remove() function
lst.remove("s")

Output: lst = [ "i", "g", "m", "a", 69]

Popcorn Hack # 3

Try making your own set of 3 variable variables. It can be anything. Use your creativity! Try a String or a Float

Hacks Practice

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
  • Be sure to follow Snake case convention for your variables
  • Build your own code cell(s) that define each variable types
  • Experiment with the + operator on string types, integer types, and float types. What operations can the + operator perform?
  • Provide comments and outputs in the cell that are easy to follow
### Store as a Dictionary

my_dict = {
 "name": "Ahaan",
 "age": 15,
 "Favorite Food": "Pizza",
 "status": "Not as cool as spencer"
}

### Store as regular variables which can be called at any time

favorite_food = "Pizza"
my_name = "Ahaan Vaidyanathan"
my_age = 15
status = "Not as cool as spencer"

### Store as a List

my_list = ["Ahaan", 15, "Pizza"]

 

Experimenting with the + operator on string types, integer types, and float types.

There are multiple examples, here for this example. Remember, we have to use the str conversion when combining a integer or float with a string. You can also use commas to add a space and combine text.

Here are both examples:

print("Hi" + " "+ "I am"+ " "+str(15)+ " "+ "years old")
print("Congrats for Learning 3.1")

# With commas
print("Hi", "how", "are", "you" + "?") # Can be useful when printing out separate strings without combining them.

# Or even better
code = "code "
print(code * 3) # The asterisk can be used to print a variable multiple times