In this post, I am going to walk you through the basics of the Python programming language. We are going to do a few small and simple programs as a basis for a better understanding of what it we are going through and through. We are going to go through, among other things, on the syntax of Python, variables, if statements, loops, and more. There are at least 5 reasons to learn program in Python.
A Brief Introduction to the Syntax
The whole of the Python’s syntax based on the proper indentation of the code. Many of the other programming languages use curly braces to mark code blocks belongs to the if-statements and loops and doesn’t give a damn about indentation. This does not use Python, instead it is governed everything, the indentation. This is the nice … this code will always be neat formatted and clearly legible.
The Importance of Indentation
In the beginning, however, the indentation seems a little strange for those who are completely new to the programming. Especially confusing if you happen to be mixing spaces and the tab character, which is indentation. In a Python program, you must be consistent with the indentation. Either you use the tab character in the text, or the use the space bar the number of spaces must be the same for all the program).
Where will it be when the code is indented? As soon as we are in a loop or an if statement, or the other line that ends with a colon (:), the lines associated with the if-statement or loop are indented. As soon as the indentation stops, we are outside of a loop or if-statement again.
#!/usr/bin/env python3 if (3 < 7): print("Three is less than 7") print("Inside the if-statement ") print("Now, we’re outside the if-statement")
The above simple script will produce the following output:
Long lines are typically divided up into short lines and we do not need to indent these. However, you should do so anyway because it’s easier to see that they are both the lines relate to each other.
print("Hey there " + "Steve & Pete")
The text (Python strings) will be printed on the same row:
Take note of the above, we always have to have the matching parenthesis around the function call (this should be in programming languages). It is easy to miss as the new one the programmer and the reasons are often a lot of headache. In the code above, we have parenthesis on the first line, and an end parenthesis on the second line. Also note that strings are enclosed in quotation marks, as the majority of the other is the language. The strings are merged together using the plus sign (i.e., “+”).
Comments in Python
When commenting the code in Python, the text is completely ignored by Python, and comments are used to documenting the code. Without comments, a program would be very, very difficult to understand. In Python, we write individual comments, with the bulwark (#). Longer comments. that extend over several lines, are written with a three-piece single quote character (‘).
#!/usr/bin/env python3 # This is a comment on one line '''This comment is on many rows. Could be useful if we need to write a lot about our code (or Python function) ''' print("Hello and Hello”)
How to Declare Variables in Python
The variables are declared and implemented in one and the same element in the shell. See the examples here at the bottom. We need, therefore, is not itself explicitly set out what must be a whole number, floating-point numbers, and so on. If a variable is set to a floating-point number, is also the data type automatically to a floating-point number.
#!/usr/bin/env python3 name = "Pete" # A string pi = 3.141592 # Float weekdays = 7 # Integer print(type(name)) print(type(pi)) print(type(weekdays))
The script above will print the data types of the variables we have created:
When we use the variables in the code, type in the name of the variable, we have don’t have to use the dollar sign or the like, in order to indicate that the is the variable that we want to get to.
#!/usr/bin/env python3 name = "Donald Duck" # String pi = 3.141592 # Float weekdays = 7 # Integer print("The number of days per week is: " + str(weekdays)) diameter = 5 circumference = diameter * pi print("The circumference is " + str(circumference) + " if the diameter is " + str(diameter))
Running this program will print according to the image below.
Note that in the above code, we had to tell Python that we want to write out the variables for the days of the week, the circumference and the diameter of as Python strings, with the function str(). This is because the same print() statement can not mix strings and other types such as integers and floating-point numbers. Therefore, we need to convert the variables in the print function to strings, rather than integers, and floating-point numbers. However, this will not change the variables to strings, they are still integer and floating-point numbers.
Briefly about If-statements and Loops in Python
Just like in any other language, we can be nested multiple levels of a loops, and if statements. Now it is all the more important to keep track of the indentation so everything will work. Often, if-statements are put (or needed) inside a loop in order to test the different values.
Python’s for loop
We have to indent both the loop and the if statement, as shown in the example below.
#!/usr/bin/env python3 for x in range(1,21): if(x % 2 != 0): print(x)
Here, we used a for loop to loop through all the numbers between 1 20. range()-this functions steps you through the numbers that we give, from the first number (1), up to but not including the last number (21). Only the numbers, which gives a remainder when dividing by two will be printed on the screen. These are the numbers that are odd.
Python’s while loop
There is another type of loop, as well, namely the while loop which is to be used in a little bit different way than in the for loop. The while for-loop, loop-the-loops through the pre-determined number of elements, loops, while loop until such a statement not any longer is true.
#!/usr/bin/python3 counter = 0 while (counter <= 10): print("We’re now on " + str(counter)) counter = counter + 1 print("Done!!!")
You can see the output of this script using the while loop:
In the above code, we also saw that the loop in lines are always terminated by a colon (:).
Working with Pythons if statements
In this section, we are going to learn more about Pythons if statements. For instance, we often need to make a program that can take a variety of paths in an if-statement, and not only a single choice.
In such cases, we use the elif , and else. These if statements belong together, and the the code is run from top to bottom. In the first if , or elif statement that is true, the code will stop (or break the if statements and continue with other code). Note, elif it is an abbreviation of else if.
#!/usr/bin/env python3 place = input("Where do you live?") if (place == "Duckburg"): print(“You’re a Disney character”) elif (place == "Romulus"): print("You¨re a Romulan from Star Trek") elif (place == "Gotham City"): print("Is this Batman?") else: print("Maybe you live in the real world?”)
If we run the piece of code, above, and reply “Duckburg”, “Romulus” or in “Gotham City” that are replies related to the if or elif statements. If we reply something other else, such as “New York”, it will be caught by the else statement.
Here, we also saw that all if lines end with a colon (:), and all of the code that pertaining to the if statements are indented.
Also, be sure to enter a double equal sign (= = ), as in the code above when we are going to make a comparison. This is a common mistake for a beginner Python. Do you use only one equal sign, it means that we are to assign a value to a variable. The double equals sign is the one that is the comparing operator equals.
Python can be used for so many cool things. For instance, you can get user input in Python from the keybord! One thing this post did not cover is that Python contains a library of modules and packages. If you need to get a list of all installed Python packages, check that post out.
That was a very brief introduction to some of the most important parts of the Python language and to programming. In future posts, we may go into detail into the different aspects of this Python tutorial.