DEV Community

Cover image for PYTHON BASICS, PYTHON 101.
Ian Mike
Ian Mike

Posted on

PYTHON BASICS, PYTHON 101.

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
  • Automation
  • 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.

  1. 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.
>>>
Enter fullscreen mode Exit fullscreen mode
  1. Open your text editor and create a new file 'HelloWorld.py'
  2. Inside the file, type in this and run the file on the command line by typing Python HelloWorld.py
print("Hello World")
Enter fullscreen mode Exit fullscreen mode

The output should be something similar to this.

Hello World
Enter fullscreen mode Exit fullscreen mode

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:

  1. Create a new file, sum.py
  2. Type the following:
a = 7
b = 5
c = 9
print("The sum of the three numbers is", a+b+c)
Enter fullscreen mode Exit fullscreen mode

2.1 Execute on the command and you should get something like this

The sum of the three numbers is 21
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The # mark represents comments in Python and therefore are not executable lines of code. Comments help you understand your code easily.

Other examples in action you can try:
#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[0]) #print first city on the list
print("The second city is:",mylist[1]) #print second city on the list
print("The third city is:",mylist[2]) # print third city


Enter fullscreen mode Exit fullscreen mode


Why use or study Python?
  1. Python increases your productivity. Python allows you to solve complex problems in less time and fewer lines of code.

  2. Python is applicable in many areas across industries, from web applications to data science and machine learning.

  3. Python is quite easy to learn in comparison with other programming languages. Python syntax is clear.

  4. Python has a large ecosystem that includes lots of libraries and frameworks.

  5. Python is cross-platform. Python programs can run on Windows, Linux, and macOS operating systems.

  6. Python has a huge community. Whenever you get stuck, you can get help from an active community.

  7. Python developers are in high demand.

Conclusion:

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:

  1. Scrimba

  2. Python Tutorial

  3. YouTube Yes! YouTube.

I believe this article will be helpful to you who has come across it.

Best regards and happy learning. Cheers!

Top comments (0)