PYTHON Introduction
PYTHON Introduction
History
• Python was considered in the late 1980s and its implementation
was started in December 1989 by Guido van Rossum at CWI in
the Netherlands as a successor to the ABC programming
language capable of exception handling and interfacing with the
Amoeba operating system.
• Van Rossum is Python's principal author, and his continuing
central role in deciding the direction of Python is reflected in the
title given to him by the Python community.
Introduction
• Python is a widely used general-purpose, high-level
programming language.
• Its design philosophy emphasizes code readability, and its syntax
allows programmers to express concepts in fewer lines of code
than would be possible in languages such as C++ or Java.
• The language provides constructs intended to enable clear
programs on both a small and large scale.
Support in Python
• Python supports multiple programming paradigms, including object-oriented,
• It features a dynamic type system and automatic memory management and has a
and structured programming are fully supported, and there are a number of language
Python Screen
Simple Program
• Python is a very simple language, and has a very
straightforward syntax. It encourages programmers to
program without boilerplate (prepared) code. The simplest
directive in Python is the "print" directive - it simply prints
out a line (and also includes a newline, unlike in C).
• There are two major Python versions, Python 2 and Python
3. Python 2 and 3 are quite different.
• For example, one difference between Python 2 and 3 is
the print statement. In Python 2, the "print" statement is
not a function, and therefore it is invoked without
parentheses. However, in Python 3, it is a function, and
must be invoked with parentheses.
Basic Syntax
• Basic syntax of a python program is too simple than other
languages.
• Let's take an example, here the following program prints "Hello
Python, I am Python Basic Syntax" as output:
Coding:
Output:
Read User Input from Keyboard in
Python
• To get input from user in python, you have to use input() function. You can get any type of
input using this input() function in python, you have only to place the type before the
statement to get the desired type of input using the input() statement in python.
• Get Integer Input from User
while True:
if val == 0:
break
else:
This python program shows how to get floating-point input from user
in python:
m=54
n=45
r=0
r=m+n
print "sum = ", r
r=m-n
print "Subtract = ", r
r=m*n
print "Multiply = ", r
r=m/n
print "Divide = ", r
Data types in Python
• Basically data type represents the type of value and determines how the value can be
used in a python program. Since all the data values are encapsulated in relevant object
classes.
• Everything in Python, is simply an object and every object has an identity, a type and a
value. There are following five standard data types available in Python programming:
I. Numbers type
V. Dictionary type
Number type
• Number data types store numeric values.
• Number objects are created when you assign a value to them.
For example −
var1 = 1
var2 = 10
• You can also delete the reference to a number object by using the del statement.
• You can delete a single object or multiple objects by using the del statement.
For example −
del var
del var_a, var_b
Python supports four different numerical
types
• int (signed integers)
• long (long integers, they can also be represented in octal and
hexadecimal)
• float (floating point real values)
• complex (complex numbers)
Python Number Example
# Python Number Data Type - Example Program
num1 = 10
num2 = 20
• num1 = 10
• num2 = 20
• You are free to use either pairs of single or double quotes in Python
programming. Here is an example of string.
• Strings in Python are identified as a contiguous set of characters represented
in the quotation marks. Python allows for either pairs of single or double
quotes. Subsets of strings can be taken using the slice operator ([ ] and [:] )
with indexes starting at 0 in the beginning of the string and working their
way from -1 at the end.
• The plus (+) sign is the string concatenation operator and the asterisk (*) is
the repetition operator.
String Example
dictionary1 = {}
dictionary1['one'] = "This is one"
dictionary1[2] = "This is two"
i=10
print(type(i))
f=324.423
print(type(f))
b=True
print(type(b))
print(type(str))
Operators in Python
Operators in Python, are used to perform mathematical and logical
operations.
There are the following types of operators available in Python:
• Arithmetic Operators
• Logical Operators
• Comparison (Relational) Operators
• Assignment Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
Python Arithmetic Operators
# Python Operators - Python Arithmetic Operators res = num1 % num2
- Example Program print("num1 % num2 = ", res)
print("If num1 = 23 and num2 = 10. Then,"); print("\nIf num1 = 2 and num2 = 3. Then,");
res = num1 - num2 #again changing the values of num1 and num2
print("num1 - num2 = ", res) num1 = 10
num2 = 5
res = num1 * num2
print("num1 * num2 = ", res) print("\nIf num1 = 10 and num2 = 5. Then,");
This operator checks if the value of the two operands are equal or not. If equal, then the condition
==
becomes true, otherwise false
This operator checks if the value of the two operands are equal or not. If not equal, then the
!= condition becomes true, otherwise false
This operator checks if the value of the left operand is greater than the value of the right operand
> or not. If yes, then the condition becomes true, otherwise false
This operator checks if the value of the left operand is less than the value of the right operand or
< not. If yes, then the condition becomes true, otherwise false
This operator checks if the value of the left operand is greater than or equal to the value of the
>= right operand or not. If yes, then the condition becomes true
This operator checks if the value of the left operand is less than or equal to the value of the right
<= operand or not. If yes, then the condition becomes true
Comparison Operators - Example
Program
# Python Operators - Comparison Operators - if num1 > num2 :
Example Program print "num1 is greater than num2";
else:
print "num1 is not greater than num2";
num1 = 23
num2 = 10 if num1 <= num2 :
res = 0 print "num1 is either less than or equal to num2";
else:
print "num1 is neither less than or equal to num2";
print "If num1 = 23 and num2 = 10. Then,";
if num1 >= num2 :
if num1 == num2 : print "num1 is either greater than or equal to num2";
else:
print "num1 is equal to num2";
print "num1 is neither greater than or equal to num2";
else:
print "num1 is not equal to num2"; # changing the values of num1 and num2
num1 = 40
num2 = 40
if num1 != num2 :
print "num1 is not equal to num2"; print"\nIf num1 = 40 and num2 = 40. Then,";
else:
if num1 <= num2 :
print "num1 is equal to num2";
print "num1 is either less than or equal to num2";
else:
if num1 < num2 : print "num1 is neither less than or equal to num2";
print "num1 is less than num2";
if num1 >= num2 :
else: print "num1 is either greater than or equal to num2";
print "num1 is not less than num2"; else:
print "num1 is neither greater than or equal to num2";
Python Assignment Operators - Example
Program
res *= num1
num1 = 25 print "res * num = ", res;
num2 = 10
res = 0 res /= num1
print "res / num1 = ", res;