Getting Started with Python: Classes, Objects, File Types & More
I am studying Python with @skillcrush, in this article we cover classes, objects, file types, and more as we wrap this intro course
I am learning with Skillcrush. Please check them out and sign up if you're looking to break into tech!
Receive $250 off your course by using the link above. Use the coupon code: TWIXMIXY
NOTE: this discount is for Break Into Tech + Get Hired package exclusively.
It's the same program I am participating in!
🐍 Greetings travelers! We are studying Python with Skillcrush. In this article we cover classes, objects, file types, and more as we wrap this intro course.
🟩🟩🟩🟩🟩🟩🟩🟩⬜⬜⬜⬜ 66% DONE
I’ve wrapped up my Front End Developer portfolio at Skillcrush, read here:
I am now enrolled at UC Berkeley! 🏫 I will be taking their Full Stack MERN Coding Bootcamp through the remainder of the year.
You can subscribe without signing up for a substack account! Just select “None” when subscribing. All the content from my learning journey is free.
Classes & Objects
→ CLASS
A blueprint for creating a new Python object
→ OBJECT
A data structure, like a variable or a list
To Write A Class:
Define the class
Set up the class
Create properties for the class
Add functions and variables
Run the class
Define the class:
class Person:
Classes are always capitalized in Python.
Set up the class:
class Person:
def __init__(self, name, age):
→ CONSTRUCTOR FUNCTION
Defines how you set up your class
Constructor Function is the word __init__
.
self
is shorthand for the Person
class. name
& age
are the arguments.
Create properties for the class:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
= name
and = age
are properties.
self.name
is saying in the Person
class, assign the name
property to the name
argument.
self.name
uses the dot notation, which is how properties are assigned to a class.
Add functions and variables:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greeting(self):
print("Hi my name is " + self.name)
person1 = Person("Hannah", 25)
Defining a greeting
function and passing self
through the argument.
person1
, now being outside of the class, can only pull from Person and then speak to the arguments. So name and age.
Run the class:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greeting(self):
print("Hi my name is " + self.name)
person1 = Person("Hannah", 25)
print(person1.name)
print(person1.age)
print(person1.greeting())
Pet Shop Challenge
I felt good about this challenge. Referring to my notes above definitely helped. I added in some extra steps in there by adding titles for each data point and turning the numbers into strings.
Check it out: Replit
Start Personal Assistant Challenge
The goal of this starter course is to build a personal assistant app, so in this challenge we’ll begin building it.
Definitely don’t feel like I know what I’m doing with this challenge, but I wrote up what I could think to write.
Check it out: Replit
Personal Assistant App Challenge
In this challenge we put together a lot of challenges. I did the best I could and got all my print()
’s to function… now to look at the solution code, so we what I may have missed.
Overall it looked good to me. I needed to make some updates to the birthday app, but overall it worked! Made some fun updates.
Check it out: Replit
🟩🟩🟩🟩🟩🟩🟩🟩🟩⬜⬜⬜ 75% DONE
Python File Types
→ FILE
An object for storing data or commands for an app.
→ FILE PROCESSING
A system used to create, save, and retrieve the data in your files.
→ TEXT FILES
Can contain letters, numbers, and symbols, end in a .txt file extension, & are useful for storing large amounts of text.
→ JSON FILES
A popular type of text file for data exchange, such as taking information from one coding language to another.
File processing is the system used to create, save, and retrieve data in files.
File Modes:
"r"
– Read mode, which allows you to access a file and view the contents without making changes."w"
– Write mode, which allows you to add data to a file or create a brand new file."a"
– Append mode, which allows you to add data to the end of a file.
\n
- a line break
Television Shows Challenge
Definitely felt like I struggled through this. Because I still find loops to be confusing, when they requested a loop be tossed in here I couldn’t figure it out.
Check it out: Replit
Personal Assistant Data Files Challenge
Attempting to bring it all together. I typed up the best I could from what I’ve learned, but I’m receiving errors and don’t feel sure.
I was actually pretty close. So I’m glad I am continuing to type code, even when I’m unsure. I had a few syntax issues but was able to clean those up.
Check it out: Replit
🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩⬜⬜ 82% DONE
CSV Module
→ CSV MODULE
Used to create and edit CSV files.
→ TABULAR DATA
Data organized in a table format define by rows and columns.
→ CSV
Comma Separated Value - A file format for storing tabular data in a spreadsheet format, like Microsoft Excel or Google Sheets.
snacks,amount,calories
cheese pizza,1 slice, 70
ice cream,1 cup,130
chips,1 cup,150
→ HEADER
A row used to label and identify the data in each column
To read the CSV file in Python:
import csv
with open("snacks.csv", "r") as treats:
reader = csv.DictReader(treats, delimiter=",")
for row in reader:
print(row["snacks"], row["amount"], row["calories"])
DictReader()
- converts the CSV file to a dictionary that can be read by Python.
F-strings - short for formatted strings, easily concatenate a string with a value inside of a print function.
name = "Ann" profession = "developer"
print(f'Hi! My name is {name} and I am a {profession}. Pleased to meet you!')
Language Translator Challenge
I’m honestly shocked that I got this one basically correct on the first go!
The only issue was previously I had the translations = {}
under the CSV reader. Once I moved it above, it worked perfectly.
Check it out: Replit
Finish Language Translator App Challenge
I feel pretty good about this one. I definitely tried a few different things, but ultimately got it to work without looking at the solution code.
Check it out: Replit
🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩⬜ 89% DONE
Finish Class Project
Personal Assistant Birthdays Challenge
Definitely struggled through this one. I got turned around on the directions and ultimately had to look at the solution code to see what they meant in terms of what to build and where.
Check it out: Replit
Complete Personal Assistant Challenge
This part was to add a contact list. So basically mimicking the work I did with birthdays. I believe I was able to get most of it, but when I went to get specific contact info I received an error. So time to see what I missed.
I was missing an “s” from contact in one place and then I had some confusion about what to call the job title field. On the main.py they called it title and on the PersonalAssistant.py they called it position. It worked when I updated it accordingly.
Check it out: Replit
You can also see the app on GitHub:
Go here: https://github.com/TwixmixyJanet/Python-Personal-Assistant/tree/main
🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩 100% DONE
I’ve loved beginning to learn Python! This wraps up the intro to Python course. I have two more courses in Python that I can take with Skillcrush. I also begin my courses at UCB in just over a week. Stay tuned!
What did you think? Leave me a comment and share!