Python is a very powerful and easy to understand High Level Programming Language.
It is general purpose programming language which requires only interpreters and no compilers.
It is an object oriented language. Everything in python is an object.
Creator of Python was Guido van Rossum during 1985- 1990.
Python Supports following Programming Paradigms:
It has a large and comprehensive Standard Library.
Python is both Strongly Types and Dynamically Typed.
It Focuses on Readability and Productivity.
Python runs on all major operating systems including Windows, Mac Os or Linux. It is Cross platform.
Python is Opensource.
Python uses Whitespaces for Indentation.
If you are familiar with programming languages such as C or JAVA, you will find out that some expressions in Python are similar to them while others are not.
Python is used by famous organizations like:
Syntax of Python
Syntax of python is very easy to understand and write, to print “hello world”. You can write
print (“Hello World”)
This article will guide you through the process of Installing Python in your computer.
You can click on the respective links to Go to the Download Page and select the Python version of your choice.
Currently the version of Python is 3.6.5.
Once you are finished with downloading the executable file you can simply install Python by running it.
Python will create a Folder in your default or custom drive.
Python will also install IDLE, it is an integrated development environment for Python.
You can also run python in Command Prompt from the Python 3.6 File created in your All Programs >> Python 3.6 Folder.
Two other files will also be installed in the package which are the documentation files.
Here are the names of documentation files created in windows installation of Python
In the previous part of this tutorial we explained the process of installing Python on your computer. In this tutorial we will explain about the Basic Syntax of Python language.
Once Python is installed in your computer you can use one of the following tools to code and execute python.
You can search for Python in your computer programs and you will find these two options.
IDLE is an integrated development environment for Python.
Other One is the Command Prompt Windows for Python Coding.
IDLE is a preferred option for Python Coding because it has a Coding Editor with built in auto suggest tool. It has nice color coding to differentiate between various elements of a code.
Python Uses a built-in function named print to print any statement.
If we want to print “Welcome to Python Tutorial” we can use the following command.
print (“Welcome to Python Tutorial by Slidescope”)
You can see the Screen Shot of IDLE Window.
The line with three Greater Than Signs >>> is the input line
The next line in Blue is the Output.
You can see the Statement to be printed is written in Round Brackets and Double Quotes.
You can also use single quotes.
Value written inside Quotes Represent a String Data type.
To print integer or float type values we don’t have to use single or double quotes.
Syntax to Print an Integer Value:
After writing the Input we have to press enter to execute the command and present output.
Please try using these commands in your practice and let us know if you have any doubts in the comment section below.
If you are not familiar about Data Types in Programming Languages then Refer to Our Next Tutorial which explains the data types topic.
Official Website of Python Is www.Python.org
Variables are used to store values in Python. These variables can be used later on in the program for computing purpose.
Variables have a name, value and a memory location in computer.
X = 56
Here X is the Variable Name and 56 is its value.
= is known as assignment operator.
The meaning of this statement is – A value 56 of integer datatype has been assigned to a variable named X.
Another Example :
>>> name = “Rohit”
Here variable name is name
Here value of variable is “Rohit”
<class – str>
How to Print a Variable
Output : Rohit
We can use variables to perform mathematical operations.
>>> x = 56
>>> y = 67
>>> print(x + y)
Output : 123
Variable values can also be changed.
>>> name = “Mohan”
>>> name = “Sohan” # We have changed the previously assigned value.
You can call variables – value-holder.
These values can be of any datatype.
>>> # Valid Variable Names
>>> first_name = “Mohan”
>>> FirstName = “Mohan”
>>> Firstname = “Mohan”
>>> firstName = “Mohan”
>>> # Invalid Variable Names
>>> first name = “Mohan” # using spaces is not allowed in variable names
SyntaxError: invalid syntax
>>> first-name = “Mohan” # using – sign means subtraction so it is not allowed
# No symbol is allowed except _ (underscore)
>>> We cannot start a variable name with number
>>> 1name = “Mohan” # invalid
>>> name1 = “Mohan” # valid
Operators in Python language
Operators are used to perform specific actions in programming languages. Here are names of some common operator in Python:
>>> x = 56
>>> y = 45
>>> x + y
>>> a = 345
>>> b = 123
>>> a – b
>>> u = 45
>>> v = 3
>>> u * v
>>> a * b * u
>>> people = 6
>>> quantity = 42
>>> quantity / people
>>> x = 5
>>> y = 2
>>> y ** x
>>> x ** y
>>> x = 22
>>> y = 7
>>> x / y
>>> x % y # Finding the remainder
>>> 343 % 4
>>> 343 / 4 # Division
>>> 343 % 4 # Remainder
>>> 343 // 4 # Quotient
Here are the Assignment Operators in Python Language
Assignment Operators are used to assign values to variables. In simple words, putting value in a variable. These values can be of any data type.
Equal to operator is used to assign any value to a variable.
>>> x = 5
>>> name = ‘Mohit’
Here value of 5 (integer) is assigned to variable x
Value of Mohit (String type) is assigned to variable name.
Note – the value to be assigned (defined) should be written on right hand side of = sign.
Here x = 5 is correct but 5 = x is incorrect.
See an example:
>>> a = 65
>>> b = a
>>> u = 65
>>> u = v #Will raise Error
>>> x = 56
>>> x += 56
Here x + = 56 means x = x + 56
>>> g = 58
>>> g -= 8 # g = g-8 | g = 58 – 8
We are assigning a value of g – 8 to g itself.
>>> a = 4
>>> a *= 5 # a = a * 5
>>> df = 45
>>> df /= 5 # df = df / 5
In programming comparison operators are used to compare two or more values. These are comparison operators used in Python.
To check whether one value is equal to other value. The answer is returned in Boolean format.
>>> name = “Mohit” # Assignment
>>> name == “Mohit” # Comparison
True # Output
>>> x = 5 # Assigment
>>> 5 == x # Comparsion – LHS is equal to RHS here
*Use of If Condition
Syntax of if
>>> name = “Ramesh”
>>> if(name == “Ramesh”):
print(“Welcome Ramesh Babu”)
Welcome Ramesh Babu #output
>>> x = 67
>>> if(x > 65):
print(x, ” is greater than 65″)
67 is greater than 65
Example with With if and else :
x = 68
if(x > 65):
print(x , ” is g t 65 “)
print(x , ” is ngt 65 “)
# 68 is g t 65
>>> t = 678 # assignment
>>> t >= 789 # comparison
False # output
>>> t >= 678
>>> t >= 677
>>> cb = 56
>>> cb < 45
>>> cb = 56
>>> cb <= 56
>>> cb <= 78
How to Take User Input in Python
We can take user input in python using the console window or we can also create GUI using python.
To create GUI windows in Python we use a library or module called Tkinter.
To take user input in python using console window we use and built in function called:
name = input(“Enter Name”)
# Here name is the variable where we want to store of the value which user will enter in the console screen.
“Enter Name” is an instruction to user.
input() function creates string values only.
name = input(“Enter Name”)
pwd = input(“Enter Password”)
print(“Your Password is correct”)
print(“You are not mohan”)
Exercise : Create a program in python to calculate the area of a triangle.
a=float(input(“enter a in cm”))
b=float(input(“enter b in cm”))
c=float(input(“enter c in cm”))
print(“area is ” ,(s*(s-a)*(s-b)*(s-c))**0.5, “cm^3”)
Typecasting in Python
It is the process of changing one datatype to another data type.
Functions used to do typecasting.
str(x) – To change x to string
int(x) – To change x to integer ( x must be float or bool but not complex or string)
float(x) – To change x to float ( x must be int or boolean)
complex(x) – To change x to complex number. ( x must be numeric or bool)
bool(x) – To change values to True or False ( 1 is true and 0 is False )
Here x is the variable which has to be typecasted.
>>>X = “15” # Here x is string
15 # X is now integer