Introduction to Python

 

Hello programmers, welcome to my first post for python. Today, we will discuss about python and start learning python in a regular way. Python is an interpreted high level programming language that is used in various systems now-a-days. Some of its applications are robotics, machine learning, automizing tasks, and cybersecurity etc. 



Python Syntax

Python as like other languages has its own syntax developed by Guido van Rossum, creator of python. Python. Today we will start with some basic programs to learn how python works. So, without wasting of time let's get started. 

Our First Python Program

We will start by writing our first python program of Hello world. 

>>> print ("Hello! to the world of programming")
Hello! to the world of programming


Here, we are displaying string Hello! to the world of programming to the console. You should have python IDE installed in your computer. You can also use single or double quotes. But it is better to use double quotes for good programming practice. As in other languages like java where double quotes are used nor single quotes with strings. 


Python Loops and Conditions

Python has condition statements like if else, elif that can be used to give condition to program. Let's understand it by example of showing grades according to percentage.

grades = eval(input("[+] Enter your grades: \n>>> "))

if grades >= 90:
    print("Congratulations! Your grade is A+")
elif grades >= 80:
    print("Congratulations! Your grade is B")
elif grades >= 70:
    print("Congratulations! Your grade is C")
elif grades >= 60:
    print("Congratulations! Your grade is D")
else:
    print ("You fail")


Here in this example, we have taken an input of user about its grades or marks and use eval to convert it into integer statement. So that it can be compared by '>', '<' operators. if else and elif conditions are implemented to get results. elif denotes else if and we can also use else if instead of elif but it is not good way to program. If no condition is correct, else condition executes. So, this is basic python so far.



Loops

Python has basically two types of loops. for loop and while loop. Both are used to execute statements in the way to work with conditions.

i = 0
while i < 10:
    print ("Loop executing" , i + 1, "time")
    i += 1  

Output of Code

>>> 
================= RESTART: C:/Users/MAHAD/Downloads/practice.py ================
Loop executing 1 time
Loop executing 2 time
Loop executing 3 time
Loop executing 4 time
Loop executing 5 time
Loop executing 6 time
Loop executing 7 time
Loop executing 8 time
Loop executing 9 time
Loop executing 10 time


Here, while loop has been executed 10 times and condition has been executed 11 times. First value of i is zero and it is going to increase by 1 after each cycle. When i approaches 11 loop conditions becomes false and loop stops. If you give wrong condition to loop it will never stop.





Post a Comment

Previous Post Next Post