This Programming Language is Essential To Know
The Basics of Python #1
Introduction
I've been releasing AI-related articles recently and I realized that you often needed Python for building an AI. I then thought to release a series of small articles on the basics of Python for those who do not know this essential programming language.
Note: If you find any part of this article confusing, please feel free to re-read the parts that confuse you, search it up online, or put your question in the comment section.
If you prefer video tutorials, here is the accompanying YouTube video https://youtu.be/LVhv7bxaurU.
What is Python
Python is a high-level programming language, which means it aims to be closer to the English language than to a computer's binary digits in the way you write it. Python is also an interpreted programming language, meaning a program (called the "interpreter") runs through your file line by line and executes it.
The way you write a language is called its syntax. Most people find Python more readable than most other programming languages. We'll see more about this later.
Download
To code in Python, you are going to need to have its interpreter downloaded.
Most systems typically have Python installed. To check if you have Python installed do the following...
Windows
python --version
If you get a number like 3.11.4
then you're set. If you get an error you can download it with the link below.Mac/Linux
python --version
If you get a number like 3.11.4
then you're set. If you get an error you can download it with the link below.If you find that you have to download Python, you can download it here.
To code in Python, I recommend downloading Visual Studio Code or JetBrains PyCharm.
Print Statements
As defined earlier, syntax is the way a programming language is written. In Python, there are a few key syntax differences that are not similar to other programming languages.
But let's start off easy, let's do what every developer does when they start learning a new programming language, let's print "Hello World" to the console. The console is where your program can write output such as writing the result of an operation to the console.
In Python, to print something to the console, you need to type print()
and then what you want to output in between the (
and )
. So to print "Hello World", you would type print("Hello World")
.
Outputs in the console typically look like this:
Answer
print("My name is John")
print("I am 43 years old")
And the output of that would look like this:
Variables
Next, let's cover a very core and essential part of every programming language. And if you've worked with other programming languages before this article, you know that variables are very useful and important in any program. But what are variables? Variables are names given to different locations in a computer's memory.
So let's say you had a form and wanted to store the user's first name for later use. Well, you could define a variable called first_name
(spaces and certain special characters aren't allowed in variable names) and set its value equal to the first name field on the form, let's say the user's first name was "John"
. The value "John"
would be stored somewhere in computer memory and whenever you reference first_name
in your program, the word "John"
would be retrieved from the computer's memory and used as needed.
Even though Python is dynamically typed, meaning you do not need to specify the type of a variable when you declare that variable, I still think we should learn about the different types of variables as it is useful to know.
If you did not just understand a word you just read in this paragraph, do not worry about it and continue reading.
Types of Variables
There are different types of variables to store different types of values.
Here are the primary variable types in Python:
string
: Stores a series of characters, such as "John"
. When declaring a string as the value to a variable, you must put " " (double quotes) or ' ' (single quotes) around the value. So you should type "John"
or 'John'
, but not just John
(don't worry if you don't understand this, this will be explained more soon). In English words, a string is either a letter, word, or 1+ sentence(s).char
: This is typically used in other programming languages to store a single ASCII character (a single letter), but in Python, this type does not exist, it is merged with the string
type.
int
: Stores integer values (hence the name int). These can be any number (no decimals only whole numbers) such as -1
, 0
, 5
, 453
, etc. When assigning an int as the value to a variable, you just put the number with nothing surrounding it (no " " or ' '). So that would look like number = 1
, not number = "1"
.float/double
: In other programming languages, there are two separate variable types for float and double. Typically a double is more precise, but in Python floats and doubles are essentially combined into floats. Regardless, both store decimal values such as -1.44
, 43.341
, etc.
bool
: Stands for "boolean." This can only store True
or False
values. In binary, 0
or 1
.
Don't feel like you need to memorize these types, it is simply helpful to know about them.
Declaring a Variable
To declare or "make" a variable, you should follow one of these syntaxes.
String: variableNameString = "variableValue"
Int, Float: variableNameInt = someNumber
Bool: variableNameBool = True
or variableNameBool = False
Answer
name = "John"
age = 43
If you answered that correctly (without cheating) then good job ๐. If you got it wrong, don't worry about it, mistakes are part of the learning process!
Combining The Two
As the final part of this article, we are going to combine the two things we just learned, print statements (print()
) and variables.
What if we declare a variable, say first_name = "John"
. What if we wanted to print the value of first_name
to the console, how do you think we would do that? The way we can print the value of variables to the console is by saying print(variableName)
.
first_name
to the console?Answer
print(first_name)
Yep, it's that simple. Now here's another quiz to test your knowledge.
print("first_name")
and print(first_name)
?Answer
first_name
John
If you're wondering why that is, it's because the first print statement (print("first_name")
) is printing the literal string of "first_name"
whereas the second print statement (print(first_name)
) is printing the value of the variable called first_name
. Notice how the first print statement has first_name surrounded by " " (double quotes) which means Python interprets that as a string and not a variable name. Hence why the first print statement results in "first_name"
.
Conclusion
Thank you for taking some time today to learn some of the Python basics! If you have any questions, please feel free to leave them in the comments or comment on the YouTube video which should release at the same time as this article.
Accompanying YouTube Video: https://youtu.be/LVhv7bxaurU
Python Basics Series on YouTube: https://www.youtube.com/playlist?list=PLTuLuGnDqyqrAiA7xIFxDkFpEEIGVtEp_