If you want to save your work from this notebook, you should be sure to make a copy of it on your own computer.
In this workbook, we're going to explore Python variables and data types.
Variables are one of the fundamental building blocks of Python. A variable is like a tiny container where you store values and data, such as filenames, words, numbers, collections of words and numbers, and more.
Additionally, there are four essential Python data "types," which each have different powers and capabilities:
Data Type | Explanation | Example |
---|---|---|
String | Text | "lemonade" |
Integer | Whole Numbers | 40 |
Float | Decimal Numbers | 40.2 |
Boolean | True/False | False |
print("hello Intro CA!")
print("hello Intro CA!")
print("hello Intro CA!") # This is a comment and doesn't affect the code
How can we assign the value 5 to the variable new_variable
?
new_variable ... 5
What type of Python data is new_variable
?
type(5)
We can display variables in two different ways with Jupyter notebooks. Notice the differences.
print(new_variable)
new_variable
This is a Python expression. What do you guess is the resulting value?
new_variable * 2
new_variable / 2
Let's re-assign new_variable.
new_variable ... "Chaos is how I learn"
What type of Python data is new_variable
now?
type(new_variable)
This is a Python expression. What do you guess is the resulting value?
new_variable * 2
This is a Python expression. What do you guess is the resulting value?
new_variable / 2
Let's create a variable that will report whether it's warm outside.
warm_outside = temperature > 50
temperature = 27
warm_outside
What type of Python data is warm_outside
?
type(warm_outside)
A special kind of string that we're going to use in this class is called an f-string. An f-string, short for formatted string literal, allows you to insert a variable directly into a string.
An f-string must begin with an f
outside the quotation marks. Then, inside the quotation marks, the inserted variable must be placed within curly brackets {}
.
f"{variable} in a string"
subject = "Making mistakes"
print("FILL-IN-THE-BLANK is how I learn")
In this exercise, you're going to ask your partner(s) a few biographical questions about themselves and then put their answers into the correct variables.
I'll go first!
name = 'Prof. Walsh'
age = 1000
place = 'Chicago'
favorite_food = 'tacos'
dog_years_age = age * 7.5
student = False
print(f'✨This is...{name}!✨')
print(f"""{name} likes {favorite_food} and once lived in {place}.
{name} is {age} years old, which is {dog_years_age} in dog years.
The statement '{name} is a student' is {student}.""")
Now let's do the same thing but with biographical info about your partner! Ask your partner a few questions and then fill in the variables below accordingly.
name = #Your code here
age = #Your code here
home_town = #Your code here
favorite_food = #Your code here
dog_years_age = #Your code here
student = # Your code here
Now print the introduction below:
print(f'✨This is...{name}!✨')
print(f"""{name} likes {favorite_food} and once lived in {place}.
{name} is {age} years old, which is {dog_years_age} in dog years.
The statement "{name} is a student" is {student}.""")
Let's explore how variables are used in a real Python script.
The code below will calculate the most frequent words in a text file. The text file information is stored in the variable filepath_of_text
.
So to count the most frequent words in a different text file, you need to change the value assigned to filepath_of_text
.
# Import Libraries and Modules
import re
from collections import Counter
# Define Functions
def split_into_words(any_chunk_of_text):
lowercase_text = any_chunk_of_text.lower()
split_words = re.split("\W+", lowercase_text)
return split_words
# Define Filepaths and Assign Variables
filepath_of_text = ... # Pick a text here!!!!!!
number_of_desired_words = 40
stopwords = ['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours',
'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers',
'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves',
'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are',
'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does',
'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until',
'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into',
'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down',
'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here',
'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more',
'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so',
'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', 'should', 'now', 've', 'll', 'amp']
# Read in File
full_text = open(filepath_of_text, encoding="utf-8").read()
# Manipulate and Analyze File
all_the_words = split_into_words(full_text)
meaningful_words = [word for word in all_the_words if word not in stopwords]
meaningful_words_tally = Counter(meaningful_words)
most_frequent_meaningful_words = meaningful_words_tally.most_common(number_of_desired_words)
# Output Results
most_frequent_meaningful_words