What is Python?
Python is a high level programming language that has gained popularity in the recent years and has been used in various technological fields such as:
- Web development
- Building desktop applications
- Machine Learning and Artificial Intelligence.
Python is an interpreted language meaning that you need to convert written program code or source code to the machine language that the computer can understand. The Python interpreter turns the source code line by line into the machine code when the Python program executes.
Before writing a program in Python, I recommend downloading Python version 3 from here and a text editor of your choice either SublimeText, Visual Studio Code or you can install an Integrated Development Environment(IDE) like Pycharm. Python files must be saved in a .py extension, ~filename~.py. Lets begin!
After downloading Python, set the path to the Python interpreter in your system environment variables. A detailed tutorial is given here.
For demonstration purposes, we will only make short and precise programs. Let's create a hello world program.
- If Python has been installed successfully and added to path, you should get this kind of result on your command line after running the Python command in the installed directory.
C:\Python>python Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
- Open your text editor and create a new file 'HelloWorld.py'
- Inside the file, type in this and run the file on the command line by typing Python HelloWorld.py
The output should be something similar to this.
There we go. Your first Python program.
Python has several data types including text(strings), sequence(lists, tuples), numerical(integers, float) and boolean(true of false). For our second program, we'll calculate the sum of three integers. Instructions are as follows:
- Create a new file, sum.py
- Type the following:
a = 7 b = 5 c = 9 print("The sum of the three numbers is", a+b+c)
2.1 Execute on the command and you should get something like this
The sum of the three numbers is 21
Congratulations for making it this far!
Other arithmetic operations you can perform are:
print(45 - 23) # 22 (Subtraction) print(10 * 10) # 100 (Multiplication) print(85 / 17) # 5.0 (Division) print(3 ** 2) # 9 (Exponentiation) Read as 3 raised to power 2
The # mark represents comments in Python and therefore are not executable lines of code. Comments help you understand your code easily.
#Conditions in Python: if, else #Example 1 a = int (input("Enter a number to check if it's less than 10:> ")) if (a < 10): print ("a is smaller than 10") else: print ("a is greater than 10") # Strings in Python #Example 2 firstname = str (input("Enter your first name: ")) secondname = str (input("Enter your second name: ")) print ("Your name is", firstname, secondname,".") #Lists mylist =['Nairobi','Mombasa','Kisumu'] print("Major cities in Kenya are:",mylist) print("The first city is:",mylist) #print first city on the list print("The second city is:",mylist) #print second city on the list print("The third city is:",mylist) # print third city
Python increases your productivity. Python allows you to solve complex problems in less time and fewer lines of code.
Python is applicable in many areas across industries, from web applications to data science and machine learning.
Python is quite easy to learn in comparison with other programming languages. Python syntax is clear.
Python has a large ecosystem that includes lots of libraries and frameworks.
Python is cross-platform. Python programs can run on Windows, Linux, and macOS operating systems.
Python has a huge community. Whenever you get stuck, you can get help from an active community.
Python developers are in high demand.
Python cannot be summarized in one article which is why I encourage you to keep on practicing and day by day you will get better and better. You can check out further tutorials from the following recommendable sites:
I believe this article will be helpful to you who has come across it.
Best regards and happy learning. Cheers!