Python3 Basics
Python?:
There are many programming languages you can choose from and each one has its own strength and weakness but if you're just starting out Python is a potentially good choice. Python was created by a man named Guido van Rossum and was developed with the idea that readability was important. The name Python was inspired by the show Monty Python and The Flying Circus. This blog is for anyone whether you are someone who has no experience in programming or someone looking to enhance your understanding.
What is an IDE?:
Before we jump into some python basics you will need some sort of IDE in order to start writing code. IDE stands for Integrated Development Environment. An IDE contains everything someone who wants to write code could need such as a text editor to write your code in and a debugger to test your code for errors. I will be using visual studio code to write and execute my code however there are other IDE’s out there such as Pycharm so feel free to play around and choose the best one suited to you. Once you have installed your IDE we can get started with writing our very first piece of code!
Hello World Program:
To begin, let's make a new file called Hello.py and type into the text editor the words print(“Hello World!”). Remember to type print() exactly as is or you will get an error message from the console when you run it.
print('Hello World!')
This should output in your terminal: Great, we just wrote our very first piece of code but how did the terminal know to print out the words Hello World? Well, I won't go too in-depth into how it works but there is something called an interpreter and compiler. The interpreter is basically what translates what we write in English to byte code which is then handed over to the compiler which translates the code into 1’s and 0’s which makes it machine-readable.
Math Operators:
Here are some basic math operations that you can perform in python.
print(3+2) # + Addition
print(3*2) # * Multiplication
print(3-2) # - Subtraction
print(4/2) # / Division
print(3**2) # ** Exponent
print(3%2) #% Remainder
Variables:
One of the most important parts of programming is having a place to store things in memory and that is where variables come in! You can think of a variable as sort of like a container that can store values in them. Once you set a value to something you have basically created a variable.
Number = 1
Firstname = 'John'
Let's use the function type() and print in order to see what we get back from each of the variables.
Number = 1
Firstname = 'John'
print(Number)
print(type(Number))
print(Firstname)
print(type(Firstname))
Output:
As you can see above the type() function returned what type of data the variable contained while print() gave us back the data the variables contained.
Data Types And Type Conversion:
Let's get a bit into some data types! There are many different types of data but I will be going over floats, integers, and strings. When we first wrote print(‘Hello World’) We told them to print out a line of strings which was indicated with the quotations surrounding it. We can use the function int() to convert any numbers within a string into an integer.
Num = '1'
print(type(Num))
print(type(int(Num)))
print(type(Num))
Output:
Notice how when we got the type of the variable Num it was a string but then we used the function int() to get back the type became int but then when we called it again it was still a string? Variables contained in memory keep their type value unless they are reassigned which can be done with the equal sign (=). What we just did to get back the int type is called type conversion!
Number = 1
print(type(Number))
print(type(str(Number)))
print(type(float(Number)))
Number = '1'
print(type(Number))
Output:
Comparison Operator and Boolean:
Boolean values are an important part of programming as they can help you in determining whether to run a piece of code or not. In Python there are two keywords that are used when you talk about boolean and they are either True or False. In order to do comparison you use the == operator. A common mistake I used to make was using = for comparison however = is an assignment operator while == is a comparison so don’t fall for it!
print(False == True)
print(False == False)
print(1==2)
Output: In line 1 of the code, we basically compared the False keyword to the keyword True and got back False which is to be expected since true does not equal to False. We can use Boolean for a variety of situations and I'll try to cover some of the basic use cases for boolean.
Here are some boolean operators:
3 == 2 # == (comparison)
2 > 3 # > greater then
3 < 4 # < less then
3 != 4 # != Not equal to
3 >= 4 # >= Greater then or equal to
3 <= 4 # <= Less then or equal to
If And Else Statements:
If and else statements in Python usually utilize boolean values in order to run pieces of code. An if and else statement is basically like saying execute this piece of code only if the condition that is given is true otherwise execute something else if it is not true.
if (2+2 == 4):
print('2 + 2 is equal to 4')
else:
print('That is not true!')
if (2+2 == 5):
print('2+2 is equal to 5')
else:
print('That is not true!')
Output: As you can see from the code above 2+2 is equal to 4 therefore it evaluates to true while the 2nd if-else statements evaluate to false so it executes the else statement instead. One thing to note about the if-else statements is the code under them which have spacing and that has to do with the fact that indentation matters.
While Statements:
While statements can be used to loop over something a certain amount of times until the condition that it has been given is no longer true.
I = True
A = 'Hello'
while(I == True):
if(A == 'Hello'):
print('Start of the loop')
A = 3
else:
print('End of the loop')
I = False
print(I)
Output:
First, I declared two variables I and A with two different values. I then used the while statement and gave it a condition that says as long as I is equal to the value True then keep running the block of code within the loop. Within the while loop, I have an if and else statement that says if A is equivalent to the value ‘Hello’ then print out ‘start of the loop’ and set the variable A to the number 3. Inside the else statement if A is not equal to ‘Hello’ then print out ‘End of the loop’ and set I to False. After the else statement executes I is no longer equal to True thus making the statement I==True false which causes the loop to end.
Here's an example of a while loop that executes 5 times.
I = 0
while(I != 5):
print(I)
I += 1
Output:
The code above basically says something like as long as the variable I is not equal to 5 then keep running the code within it. As you can see from the output the while loop basically kept running the code within it until the variable I was equal to five.
This is the end of the blog! If you stuck around and read this blog to the end I hope you learned something or at least found this reading to be worthwhile. That’s it for now, thanks for reading!