The Ultimate Beginners Guide to Python

A simple scripting language for novice programmers

--

Generated by Midjourney Bot

Welcome to the ultimate guide to Python. Today, we’re going to talk about a wide range of topics, from the history of Python, to managing data structures. I’ve sectioned this blog (which I don’t normally do) because it’s going to be quite lengthy, and adding headlines helps to organize the blog for people to access a specific section. Alright, let’s jump in!

Introduction: Why Python?

The first programming language was Assembly Language. This was created as a replacement for binary code, which was the only thing computers understood at the time and it was A LOT harder to use then Assembly Language. Assembly Language is great, but still extremely technical and difficult to use. So people kept improving on this language with other languages, like BASIC and C. C is considered to be one of the first High Level Languages. C is still extremely popular today, even though it is much older compared to other more modern languages. Eventually, improvements were also made on C, and C++ was released. The 1990’s brought a whole onslaught of new languages like Java, R, JavaScript, and the star of today’s show, Python. Initially, many people didn’t like Python, because it had so much competition, like Java, R and C++, but eventually people caught on with how easy to use it was. Because of Python’s easy to understand scripture, many people think that it’s newer than other languages like Java and R, but it was actually made before them, but it didn’t catch on until much later. This is why Python is one of the most common languages for people to start programming with.

Chapter 1: Print Statements, String Manipulation, and Variables

Let’s dive in to some basic Python scripture, starting with print statements. Print statements are a way to print a string of text or a number.

What if we wanted merge 2 strings into 1? Well, we can do that with concatenation. Just like we add numbers, we can add strings!

What if we want to store a value? We can use a variable to do that. Variables can store data, no matter the data type. Data type is a classification for variables. There are strings (Words and phrases), integers (whole numbers), floats (decimal numbers) and a few others. These 3 are the most common data types.

How would we store a value given by the user? The input() function is there to help! This allows the user to input any string.

We can also find the length of any string using the len() function.

This includes spaces and extra characters.

We can convert a variable from one data type to another.

The str() function converts any data type to a string (if possible) and the int() function converts any data type to an integer (if possible). As we can see in this snippet of code, just changing the data type of a variable can drastically change the output. In the first print statement, we were concatenating, because the 2 variables were both strings, but in the second print statement, we were actually adding the 2 numbers, because they are now integers.

A fast way to print a string multiple times is by multiplying that string. That might sound weird to you at first, but let me show you how it’s done.

As we can see, this command tells the computer to execute the print(‘Hello World’) function 10 times. Ironically, this is a nice segue into chapter 2.

Chapter 2: For Loops and While Loops

Earlier, we printed a string 10 times, with only 1 line of code, but they all printed right next to each other. What if we wanted them to print line by line? Well, that’s where for loops come in handy!

Compared to what we have seen so far, for loops are weirdly written. Let’s break this down. The 10 in the parentheses refers to the number of times we are executing this block of code. That’s the easy part, but what about the ‘for i in range’? Well, this is where it gets interesting. The computer is actually creating a variable called i, and then incrementally increasing this variable until it is 1 below the number in the parentheses. We can see this with the following code:

This variable can technically be named anything! Using i is standard, but a lot of people change it depending on the situation. We can also change where i starts and at what increment i is increased. Take a look at the following code:

In this for loop there are 3 parameters, instead of 1. The first parameter is the start. This determines where i starts. The next parameter is the stopping point. This, quite evidently, is where the for loop stops adding to i. The 3rd parameter is the step. This determines what increment i is increased by. For example, if the step is 2, then i will skip count by 2’s. The start and the step are assumed to be 0 and 1, respectively, unless they are changed. This is why we only had 1 parameter in the original loop — we were only defining the stopping point — the start and the step had already been assumed by the computer. Also, all code in the for loop has an indent before it, usually a tab or a space.

This is great and all, but what if we want a program to run forever? Well, that’s a job for the For Loops partner in crime, the While Loop.

While loops are actually used to execute a block of code while a certain condition is true, but they can be manipulated to go on forever.

As we can see, this executed the loop only while the condition was true. In this case, the condition was ‘while x < 10’.

By using the ‘while True’ command, we manipulate a loop to go on forever. Just like for loops, all code inside a while loop has an indent before it.

So, to summarize, there are 2 types of loops in Python, For Loops, and While Loops. For loops can be set to repeat any number of times. They create a variable (usually called ‘i’ but can technically be called anything) and incrementally increase that variable until the loop is done. On the other hand, while loops execute a block of code while a condition is true. We can manipulate this to go forever by replacing a condition with the word ‘True’. You can also put a statement that will forever be true, like 1 = 1. To end any loop, use the ‘break’ command and you will exit out of the loop, this applies for For loops and While loops.

Chapter 3: Conditionals

You probably already saw me using some conditionals when exploring the ‘break’ command earlier. The conditional probably made sense, since the wording is quite easy to understand.

One quirk of conditionals is that when using an equal sign, you must use 2 equal signs instead. The reason is that 1 equal sign is supposed to represent setting a variable equal to a value, but a double equal sign represents testing whether or not those 2 values are equal. No need to worry much about the explanation — just remember to use double equal signs! As with loops, everything inside the conditional is indented. Also, if testing whether 2 things are NOT equal, then instead of 2 equal signs, write ‘!=”.

There’s also a way to test for multiple conditions at once.

The ‘or’ command is used to execute a block of code if one condition is true or another condition is true. Quite self explanatory.

The ‘and’ command is also simple. It’s in the name — if one condition is true AND another condition is true, then execute this block of code. This is why I love Python, it’s very readable and easy to understand, even for a novice programmer.

You’re probably thinking: “Well, this is cool, but what if I want to test for a conditional and execute a block of code if that conditional is true, but if it’s NOT true, then execute another block of code?” There’s a way to do that too!! Python is great that way my friend!

The ‘else’ statement is only executed if the first conditional is not true. Here is another version of the else statement which is a little more tricky:

This probably looks weird to you, so let’s break it down. First, the computer is setting x = 9. Then, it is checking if x is greater than 10. If x IS greater than 10, then it prints ‘YAY!’ and ends the code there. If x is not, then is moves on. The ‘elif’ command is just a way of saying that if the last conditional was true, ignore this statement, but if it was false, then test if the following condition is true. Since, in this case, the first conditional was false, we then test for the next conditional. Since this conditional is true, we print ‘DOUBLE YAY!!’ and end the program, not executing the ‘else’ function since that should only be executed if the previous ‘elif’ statement is not true.

Conditionals can be confusing, but hopefully data structures treat you well!

Chapter 4: Intro to Data Structures

Often times when coding, it is useful to be able to store multiple items under one entity. That’s where data structures come in! There are quite a few data structures, but today we are focusing on lists. Data structures can store data, just like variables, except they can store multiple pieces of data, not just one piece like a variable can. Many data structures have an index, or a key, to access items in the list, while others don’t.

Lists are one of the most common data structures to use for storing data in an organized manner. Lists, quite literally, are lists. They store a list of chunks of data. To create a list, we say ‘list = [‘Enter items here’, ‘Put commas in between’, ‘We can even add numbers’, 2646575]’. In Python, all strings must be in quotes. If we put nothing in the brackets, we create an empty list. The ‘len’ function also works on lists. To add an item, we can say ‘list.append(‘WORD’)’ to add ‘WORD’ to the list. The same works with the list.remove() function.

There is also a special type of for loop used to loop through a list. Here’s a short video about iterating through lists:

Outro: How To Start With Python

When starting to code, it’s important to know where to code! I recommend replit.com, a coding platform which has numerous languages available to code in, and making an account is free. All the coding I did for this blog was on replit, and it’s really useful for smaller coding projects. If you do get more serious with coding, try downloading VisualStudio Code. It’s a free app which you can get on most operating systems.

Thanks for reading today everybody! If you have any ideas for cool AI projects, or circuitry inquiries, let me know down below!

--

--

Ankit Durbha
Computer Science: A teenager’s perspective

I created the publication Computer Science: A teenager's perspective with the goal of creating a community of like-minded, technology enthusiastic peers.