how I gained an understanding of Python

Hello all! This is my experience learning Python over the past six weeks, ten minutes a day, from September 13th, 2024, to October 24th, 2024, using Replit’s 100 Days of Python (Day 1 – 42). I had little experience with Python at the start of this course, only knowing some other mainstream languages.

What I learned
defining and using variables/lists/dictionaries
variables

Variables are a way to store a value. The first time I used them was on Day 2 to store user input. I learned that a person doesn’t need to declare the variable type; instead, Python will infer the type from our assigned value.

Note: If one requires a variable type, unlike the traditional variableType variableName = value, Python uses type casting instead, where variableName = variableType(value).

lists

Lists, introduced on day 32, are a way to store multiple values. Lists are very flexible. They can hold as many, or as little items as a person wants. They can also contain different types in a single list. Elements in a list can also be changed (listName[index] = value), added (listName.append(item)), and removed (listName.remove(item)).

The syntax for a list is listName = [item, item, item, etc.] or listName = [] for an empty list

Note: The index is the placement of the value within a list. It starts from 0, meaning the first value of the list has an index of 0, the second has an index of 1, etc.

dictionaries

Dictionaries are very similiar to lists, as they also store multiple values, however, instead of accessing the value using an index, a dictionary does it by a key. Dictionaries were introduced on day 40. The syntax for a dictionary is, as shown below:

dictName = {key:value, key:value, etc.]} or dictName = {} for an empty dictionary

using conditional statements (if-then, for loops, etc)
if statements

If – else statements were introduced on Day 5. It is a conditional statement that is used to check whether something matches (or doesn’t) with something, whether that be an integer or a string. I found it very interesting that Python does not use curly brackets {} and instead opts for indenting and colons. The image below also uses nested if statements. It is an if statement in another if statement, which I learned on Day 7.

Elif was introduced on Day 6. It is short for else if, and is used when a person needs an additional condition.

Note: Python uses and for two conditions in a single if statement.

while loops

while loops were introduced on Day 15 and it allows your code to repeat itself based on a condition a person sets. It is similar to an if-statement, except that it allows the code indented to be run over and over until the condition is not satisfied. One must also change the variable in which the condition is used so that it does not result in an infinite loop (aka. counter += 1 in the code below).

A while(true)loop is a type of while loop that goes on forever until it encounters a break or exit().

for loops

for loops were introduced on Day 19, and are used when a person wants something to run for a specific amount of times.

The code for a for loop is in the left image above. counter is a variable that only exists in the loop, and it acts as a tracker for the loop (from 0 to 9 in this case). The variable does not have to be counter, and can be any name. The range can be defined as range(number) as shown above or range(startingValue, stoppingValue, increment) as shown below.

Note: the starting value is inclusive; the ending value is exclusive, hence why it is 101 instead of 100.

definition of functions

Functions, or subroutines, were introduced on Day 23 and are used when a person needs some code to run multiple times. Instead of copy & pasting that code over and over, it is put inside a function and then called in a program. The syntax for a function is def funcName(parameters):, def being short for define and parameters being the variables/information that we need for the function to run properly.

There do not need to be any parameters passed to the function as shown below.

user-centred design

I learned on Day 29 that the print() statement, although reckoned as a very simple concept, can actually be quite complicated. There are multiple modifiers that exist for it, such as end="" and sep="". It can also use ANSI escape codes to change the color of terminal text as shown in the code below.

I also use f-strings—introduced on Day 30—which make it easy to embed variables or expressions inside a string. They start with the letter f before the string and use curly braces {} to insert values. For example, f"{name}" would insert the value of the variable name directly into the string. I can also use them for more complex formatting. As shown, {text:^35} centers the text in a 35 character space, and {colorChange('red')} prints the text in a red color.

flowcharting

Flowcharting is the act of planning your code beforehand. It is useful as it allows a person to visually see their logic within complex pieces of code. Here is an example of a flowchart of a theoretical code that signs a person up for an event:

thorough code commenting

Single-line comments in Python start with the # symbol and extend to the end of the line as shown below:

Multi-line comments use triple quotes and will not interfere with any code:

Note: triple quotes can be interpreted as docstrings instead of a comment if placed immediately after a function or class definition.

This was a very interesting and fun experience learning Python. I was intrigued on how different and similiar it is to other mainstream coding languages such as C++. One emminent example of this is how Python doesn’t use semicolons at the end of the line. Python, in my opinion, is also more visually pleasing to work with. It has very clean and readable syntax, which is formed using identation instead of curly braces and the tendency to use words instead of symbols. It also has an extensive array of libraries and frameworks—prewritten code—that simplify my development.

Thanks for reading this blog post!



Comments

3 Responses to “how I gained an understanding of Python”

  1. mcrompton Avatar
    mcrompton

    Good post, Felix. I like your explanations of each concept. You show me that you know what you are talking about, but I’m not sure that I have evidence that you can apply your knowledge. Did you create each of the examples (including the flowchart), or are they copy and pasted from examples in your course (or somewhere else?)

    1. fhang27 Avatar
      fhang27

      I created the flowchart and most of the examples. I used one or two examples from the course since I believed they greatly demonstrated the concept.

      1. mcrompton Avatar
        mcrompton

        Thank you, Felix.

Leave a Reply

Your email address will not be published. Required fields are marked *