Python Basics

python logo

In this article, we are going to discuss the fundamentals of Python and how to print to the screen. There is no knowledge required to follow along, but you will want to have set up your Python development environment. We will talk about types of variables and provide examples of how they can be used. This article will conclude with a simple application that can be copied/pasted and run locally.

Important Notes

Before you begin your Python endeavor, it is extremely important that you know a few key syntax basics of Python. First and foremost, indentation matters! Python uses spaces to determine what level or scope the code being run is at. If you create a function or conditional statement, the subsequent code MUST have the correct spacing. In VS Code, you will be alerted to code that is not properly aligned. If you create a function at the top level, then all code on the next line must be 3 spaces or 1 tab indent to remain in the function’s scope. We have provided an example below on how to create a function. You do not need to know about functions; we only want to demonstrate how indentations work.

def myFunction(name):
   lBlackListed = ["john","doe"]
   if name in lBlackListed:
      print("you may not proceed")
   else:
      print("you may pass")

As you can see above, there are two different levels. The function is created at the top level, and the conditionals (if-else statements) are created on the second level. Each level has 3 indent spaces.

How Python Handles Variables

A second call-out is how Python handles variables. A variable is an alias in programming used to store some type of data. Python is unique because it is not strongly typed; instead, variables are defined with a variable name. Some programming languages require that you define a variable by specifying the type, such as string, int, list, and char. Below is an example of defining a few variable types:

aString = "This is a string" #string
aChar = "c" #One letter string
anInt = 5 #this is an integer
aList = ["item1","item2",5,"item4"] #this is a list

As shown above, variables can be defined with a memorable name; there is no need to worry about telling Python what kind of variable you are defining. There are some important call-outs. There is no “char” variable in Python. A single character, or char, is defined the same way as a string; Python also treats it the same way as a string. Secondly, the most basic collection of items is called a list. We will talk more about those in the future but it is important to note that you can mix different types of variables together in the same list; you can also put lists inside other lists.

Using Defined Variables

In Python, you use a variable simply by calling it by the name you defined it as. It is best practice to use names that accurately describe your data. If I store a person’s name in a variable called pNumber then I am not likely able to easily call that variable up. I may fair better using something like sName. The s just tells me it is a string, though this is not a requirement. Below is an example of two types of variables being compared. Please note that you do not need to know about conditional statements. We will cover that in the future in more detail.

iYoungAge = 31
iPerson1 = 100
if Person1 > iYoungAge:
   print("Person 1 is old. Just kidding")
else:
   print("Person 1 is young!")

If you were to copy and paste the above code and run it, then you should see the same output as below:

Person 1 is old. Just kidding

Conclusion

We don’t want to overwhelm you with information, so we will cut it short. In this article, we talked about why indentation matters in Python syntax, and we showed how Python variables are simply created. If you copied and pasted the age example, then congrats! You are officially a programmer. If you haven’t yet, then do so! In the future, we will talk about variables more in-depth. We will talk about collections, type casting, and variable concatenation. Have fun and stay elite.

Leave a Comment