Python Data Types! :3
In Python there are several diffrent types of varriables, in this part of the lesson we will go over the most commonly used ones!
Integers, strings, booleans, floats, and lists!
Integers
Integers are whole number such as -2, -1, 0, 1, or 2, to name a few!
# Here is an example of an integer used as a variable
age = 15
height = 60
favorite_number = 8
print(age)
print(height)
print(favorite_number)
15
60
8
Strings
theres are chains of: words, numbers, or charecters, and they are placed in quotes. You can use either single quotes ' '
or double quotes " "
, but use the same one for each string.
#camel case (no underscore)
nameStatement = "My name is Nora"
print(nameStatement)
#snake case (underscore)
name_Statment = 'My name is Nora'
print(nameStatement)
#incorrect:
#name_Statment = 'My name is Nora"
My name is Nora
My name is Nora
Booleans
Booleans are true or false statments. They are used in conditional statments, making an action occur is something is true or false.
#boolean
joanna_drinks_match = False
#boolean used in if then/conditional statments
if joanna_drinks_match == True:
print (1)
else:
print (0)
0
Floats
Floats are numbers containing decimals (the oposite of Integers)
#heres an example of a float, constants follow upper snake case conventions
IMMUTABLE_PI = 3.14
print(IMMUTABLE_PI)
3.14
Lists
Lists are ordered items. They can contain a mix of data types but most comonly are all the same data type. Lists can contain integers, floats, strings, and more!
If you want to print or use a specific item in a list, refer to it with variable_name[itemnumber]
. Keep in mind that when counting, computers always start at 0, so using 1
will call the second item in the list
#here an an example of a list of strings
grocery_list = ["apples","honey","almond milk","cereal"]
print(grocery_list[0])
print(grocery_list[1])
print(grocery_list[2])
print(grocery_list[3])
apples
honey
almond milk
cereal
Dictionaries in Python
In this part of the lesson we will go over dictionaries in python and how to make them!
Dictionaries contain data structures and include and store key-value pairs. In the dictionary, there are specific values that associate with a unique key. This is useful since it makes it easier and efficient to define multiple related values, see the data and lookup specific values.
Print or use a variable in a dictionary by first referring to the dictionary name and then the variable inside of it.
personal_info = {
"name": "Bob",
"age": 78,
"city": "Las Vegas"
}
print(personal_info["name"])
print(personal_info["age"])
print(personal_info["city"])
Bob
78
Las Vegas
In this example, we define data and format it to show the attributes of a person and then print statements to put them together in an introduction. Printing data types often requires mixing types and formatting them correctly
# Data types cell
# Variable names in Python typically follow snake_case
friendly_greeting = "Hello"
my_name = "Bob Tastic"
my_age = 78
print("Print with concatenation")
print("Example 1: " + friendly_greeting + " " + my_name + ", I see you are " + str(my_age) + " years old.")
print()
print("Or you can use f-strings")
print(f"Example 2: {friendly_greeting} {my_name}, I see you are {my_age} years old.")
#
print()
print("Or you can separate the variable in the print statement")
print("Example 3:", friendly_greeting, my_name, ", I see you are", my_age, "years old.")
Print with concatenation
Example 1: Hello Bob Tastic, I see you are 78 years old.
Or you can use f-strings
Example 2: Hello Bob Tastic, I see you are 78 years old.
Or you can separate the variable in the print statement
Example 3: Hello Bob Tastic , I see you are 78 years old.
In this example, we define same data with a dictionary
# Data types cell using dictionary
# Dictionary definition
info = {
"greeting": "Hello",
"name": "Bob Tastic",
"age": 78
}
print("Concatenation")
print("Example 1: " + info["greeting"] + " " + info["name"] + ", I see you are " + str(info["age"]) + " years old.")
print()
print("F-strings")
print(f"Example 2: {info['greeting']} {info['name']}, I see you are {info['age']} years old.")
print()
print("Separate the variable in the print statement")
print("Example 3:", info["greeting"], info["name"], ", I see you are", info["age"], "years old.")
Concatenation
Example 1: Hello Bob Tastic, I see you are 78 years old.
F-strings
Example 2: Hello Bob Tastic, I see you are 78 years old.
Separate the variable in the print statement
Example 3: Hello Bob Tastic , I see you are 78 years old.
Popcorn Hack
: Make a dictionary about fruits!
Addition versus Concatenation
Operators like + produce different outcomes based on the different data types. +’s are usually used for numbers, but for characters it connects sequences in a process called concatenation.
# Addition of two integer variables
int1 = 3
int2 = 4
# Concatation between two non strings in a print statement will EVALUATE them.
print(int1 + int2)
print("Notice how they get ADDED together.")
print()
# Concatenation of two string variables
# Concatation between two strings in a print statement will CONNECT them.
string1 = "3"
string2 = "4"
print(string1 + string2) #
print("Notice how this CONNECTS the variables")
# Print statements like the above can be used to EVALUATE the result of + on two variables
7
Notice how they get ADDED together.
34
Notice how this CONNECTS the variables