Learn Python with Pj! Part 1 – Getting started
Hello World!
My name is Pj Metz and I’m the education evangelist at GitLab. My day job involves working with universities across the globe to help faculty and students learn to use GitLab for educational or research purposes. Currently, my code experience is limited to C# and JavaScript, with some HTML and CSS in there for good measure. However, one of the most popular languages in the education community is Python, so I decided to jump in and teach myself Python to better connect with my community members.
I’ll be learning on Codecademy, an online interactive learning platform that offers a variety of languages and career path curriculums, both free and paid. It’s where I started learning to code back in 2020 so I’m already comfortable with it’s format and curriculum style.
Every few weeks you’ll see what I’ve learned and how I’ve applied that new knowledge. I’ll discuss the basics of writing in Python and show off some of what I’ve done. I’m still relatively new to writing code in general, so expect to see this through the eyes of a beginner — not just a Python beginner, but coding in general. I might even make a mistake in my descriptions/explanations. Let’s jump in! ?
First lessons
The first few lessons involved writing a “hello world” and changing the value of a premade variable.
I moved on to writing my own variables and experimenting with several different types, including ints, strings, and floats. I learned that you can change a variable after defining it, similar to many languages, and that you can even change the type; the most recently defined type will be the one used at run time. Concatenation works similarly to other languages: using a plus sign to combine variables. I did some reading ahead and learned about f-strings as an easy method of concatenating strings. I’m used to doing something similar in JavaScript for my Twitter bots, so this felt important to know.
I also learned how to do some control flow through if
, elif
, and else
. The logic remains the same, but conventions are a bit different. I’m used to writing an if statement like this in JavScript.
if(partyRock === 'in the house tonight') {
everybody = 'have a good time'
console.log(`Party rock ${partyRock} everybody just ${everybody}`)
} else {
everybody = 'sad party rock noises'
console.log(everybody)
}
In Python, there are no curly braces. Rather, a colon and indent takes care of that work.
if partyRock == 'in the house tonight':
everybody = 'have a good time'
print(f"Party Rock is {partyRock} everybody just {everybody}")
else:
everybody = 'sad party rock noises'
print(everybody)
Initial thoughts
I like the readability of Python. It’s a little less cluttered, but I remember being very excited about curly braces when I first learned them. Using them for functions and methods and the like always made me feel like a “real programmer” when I was first starting. That being said, Python syntax is coming along naturally for me.
Something that’s different for me is the way Python has you initialize variables. C# is a statically typed language, meaning that part of defining a variable is saying what type of variable it is (int, string, float, etc.). Python does not require you to define the type, it will simply know at run-time. This is similar to JavaScript, but it does still throw me since I started learning with C#. Additionally, in JavaScript you have to use let
, var
, or const
. In Python you just … name it and give it a value. Felt strange at first, but has become more natural as I progressed. Not having to define the type always strikes me as “weird,” but that’s personal preference, not anything actually verifiably wrong.
Additionally, the naming convention of variables is different as well. Python convention dictates underscores as spaces, while C# and JavaScript both prefer camel case, which is where each new word is capitalized.
int minLength = 8
minLength = 8
min_length = 8
The naming conventions of Python have certain rules for when to use underscores and how, especially double underscores which behave differently in Python depending on where they appear in the name. I only know what I’ve seen so far in Codecademy, but they’ve named all their variables with underscores instead of spaces.
Favorite new knowledge
I really like being able to create multiple line strings simply by using three quotes, similar to using three backticks for a code block in markdown. Formatting the output has always been frustrating for me; having to remind myself that n
exists and then looking up how exactly I’m supposed to use it is something I’ve spent an embarrassing amount of time on. And likely will do until the day I hang up my keyboard for good.
This is nice in that how it looks in the code is how it looks in the output. I love that!
This is the first installment in the Learn Python with Pj! series. Make sure to read:
- Part 2 – Lists and loops
- Part 3 – Functions and strings
- Part 4 – Dictionaries and Files
- Part 5 – Build a hashtag tracker with the Twitter API
“@MetzinAround is learning Python and taking us along for the ride. Here’s part 1!” – Pj Metz
Click to tweet