SlideShare a Scribd company logo
Python
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,
imperative and functional programming or procedural styles.
 It features a dynamic type system and automatic memory management and has a
large and comprehensive standard library.
 Python interpreters are available for installation on many operating systems, allowing
Python code execution on a wide variety of systems.
 Python is a multi-paradigm programming language: object-oriented programming
and structured programming are fully supported, and there are a number of language
features which support functional programming and aspect-oriented programming
Brief History
 Invented in the Netherlands, early 90s by Guido van Rossum
 Named after Monty Python
 Open sourced from the beginning, managed by Python Software
Foundation
 Considered a scripting language, but is much more
 Scalable, object oriented and functional from the beginning
 Used by Google from the beginning
Features of Python
 Simple
 Easy to Learn
 Free and Open Source
 High-level Language
 Portable
 Interpreted
 Object Oriented
 Extensible
 Embeddable
Python Installation Steps
 Step-1: Browse the link to download the python latest version
https://www.python.org/
 Step-2: Install it by double click and choose the system path to
install
 Step-3: Finally , Click the Finish button .
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
# Python Program - Get Integer Input from User
while True:
print("Enter '0' for exit.")
val = int(input("Enter any number: "))
if val == 0:
break
else:
print("You have just entered:", val)
print()
1-Introduction to Python, features of python, history of python(1).pptx
Get Floating-point Input from User
This python program shows how to get floating-point input from user in
python:
# Python Program - Get Floating-point Input from User
while True:
print("Enter '0' for exit.")
val = float(input("Enter any number: "))
if val == 0:
break
else:
print("You have just entered:", val)
print()
1-Introduction to Python, features of python, history of python(1).pptx
Get String Input from User
Here is another program shows how to get string input from user in python:
# Python Program - Get String Input from User
while True:
print("Enter 'x' for exit.")
val = raw_input("Enter any string: ")
if val == 'x':
break
else:
print("You have just entered:", val)
print()
1-Introduction to Python, features of python, history of python(1).pptx
Complete Version of Getting Input from
User
Here is the complete version of getting input from user in python:
# Python Program - Get Input from User - Complete Version
while True:
print("Enter '0' for exit.")
val = int(input("Enter any Number: "))
flt = float(input("Enter any Floating-point Number: "))
strg = raw_input("Enter any String: ")
if val == 0:
break
else:
print "Integer:",val
print "Floating-point:",flt
print "String:",strg
1-Introduction to Python, features of python, history of python(1).pptx
Variables in Python
 Variables in Python, are the reserved memory locations to store the values
in a Python program.
 It means that, whenever you are creating a variable in Python
programming, you are reserving some space in the memory for that
variable.
 Based on variable's data type, python interpreter allocates the memory and
decides what can be stored in the reserved memory. Here is an example
assigning values to the variables in Python.,
1-Introduction to Python, features of python, history of python(1).pptx
Python Variables - Example Program
# Python Variables - Example Program
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
1-Introduction to Python, features of python, history of python(1).pptx
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
II. Strings type
III. List type
IV. Tuple 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.
The syntax of the del statement is −
del var1[,var2[,var3[....,varN]]]].
 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
print("num1 = ", num1, " and num2 = ", num2);
use of del keyword in python
 # Python Number Data Type - Example Program
 num1 = 10
 num2 = 20
 print "num1 = ", num1, " and num2 = ", num2
 del num1, num2
 print "num1 = ", num1, " and num2 = ", num2
1-Introduction to Python, features of python, history of python(1).pptx
String Data Type
 Strings in Python, are contiguous set of characters between quotation marks.
 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
 # Python Strings - Example Program
 str1 = 'Hello Python'
 str2 = "This is Python Strings Tutorial"
 print(str1, "n", str2);
String Examples
# Python String - Example Program
str = 'Hello Python'
print (str) # this will print the complete string
print (str[0]) # this will print the first character of the string
print (str[2:8]) # this will print the characters starting from 3rd to 8th
print (str[3:]) # this will print the string starting from the 4th character
print (str * 3) # this will print the string three times
print (str + "String") # this will print the concatenated string
1-Introduction to Python, features of python, history of python(1).pptx
List Data type
 A list in Python, contains items separated by commas and enclosed within
square brackets.
 A list basically contains items separated by commas and enclosed within
the square brackets [ ]. Items in the list needn't be of the same type.
 Examples:
list1 = ['computer', 'programming', 1957, 2070, 3242];
list2 = [1, 2, 3, 4, 5];
list3 = ["a", "b", "c", "d", "e"];
Python List Example
# Python Lists - Example Program
list1 = ["python", "list", 1952, 2323, 432]
list2 = ["this", "is", "another", "list"]
print list1 # this will print the complete list
print list1[1:4] # this will print the elements starting from 2nd till 4th
print list1[1:] # this will print the elements starting from the 2nd element
print list1[0] # this wil print the first element of the list
print list1 * 2 # this will print the list two times
print list1 + list2 # this will print the concatenated list
1-Introduction to Python, features of python, history of python(1).pptx
Tuples Data Type
 A tuple is another sequence data type that is similar to the list. A tuple consists of a
number of values separated by commas. Unlike lists, however, tuples are enclosed
within parentheses.
 The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] )
and their elements and size can be changed, while tuples are enclosed in parentheses (
( ) ) and cannot be updated. Tuples can be thought of as read-only lists.
 Examples:
tuple1 = ("python", "tuple", 1952, 2323, 432);
tuple2 = (1, 2, 3, 4, 5);
tuple3 = ("a", "b", "c", "d", "e");
Tuple Example
# Python Tuple - Example Program
tuple1 = ("python", "tuple", 1952, 2323, 432);
print tuple1 # this will print the complete tuple
print tuple1[1:4] # this will print the elements starting from 2nd till 4th
print tuple1[1:] # this will print the elements starting from the 2nd
element
print tuple1[0] # this wil print the first element of the tuple
print tuple1 * 2 # this will print the tuple two times
1-Introduction to Python, features of python, history of python(1).pptx
Dictionary Data type
 Python's dictionaries are kind of hash table type. They work like
associative arrays or hashes found in Perl and consist of key-value
pairs.
 A dictionary key can be almost any Python type, but are usually
numbers or strings. Values, on the other hand, can be any arbitrary
Python object.
 Dictionaries are enclosed by curly braces ({ }) and values can be
assigned and accessed using square braces ([]).
Dictionary Example
# Python Dictionary - Example Program
dictionary1 = {}
dictionary1['one'] = "This is one"
dictionary1[2] = "This is two"
smalldictionary = {'name': 'Devraj','id':9388, 'branch': 'cs'}
print dictionary1[2] # this will print the values for 2 key
print dictionary1['one'] # this will print the value for 'one' key
print smalldictionary # this will print the complete dictionary
print smalldictionary.keys() # this will print all the keys
print smalldictionary.values() # this will print all the values
1-Introduction to Python, features of python, history of python(1).pptx
Determine Variable's Type in Python
 You can use the function type() available in Python, to determine the type of variable in Python.
 Here is an example showing the use of type() function in determining variable's type in Python
# Python Data Types - Example Program
i=10
print(type(i))
f=324.423
print(type(f))
b=True
print(type(b))
str="Python Data Types"
print(type(str))
1-Introduction to Python, features of python, history of python(1).pptx
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 - Example
Program
num1 = 23
num2 = 10
res = 0
print("If num1 = 23 and num2 = 10. Then,");
res = num1 + num2
print("num1 + num2 = ", res)
res = num1 - num2
print("num1 - num2 = ", res)
res = num1 * num2
print("num1 * num2 = ", res)
res = num1 / num2
print("num1 / num2 = ", res)
res = num1 % num2
print("num1 % num2 = ", res)
#changing the values of num1 and num2
num1 = 2
num2 = 3
print("nIf num1 = 2 and num2 = 3. Then,");
res = num1 ** num2
print("num1 ** num2 = ", res)
#again changing the values of num1 and num2
num1 = 10
num2 = 5
print("nIf num1 = 10 and num2 = 5. Then,");
res = num1 // num2
print("num1 // num2 = ", res)
1-Introduction to Python, features of python, history of python(1).pptx
Comparison operators in Python
Operator Meaning
==
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 - Example
Program
num1 = 23
num2 = 10
res = 0
print "If num1 = 23 and num2 = 10. Then,";
if num1 == num2 :
print "num1 is equal to num2";
else:
print "num1 is not equal to num2";
if num1 != num2 :
print "num1 is not equal to num2";
else:
print "num1 is equal to num2";
if num1 < num2 :
print "num1 is less than num2";
else:
print "num1 is not less than num2";
if num1 > num2 :
print "num1 is greater than num2";
else:
print "num1 is not greater than num2";
if num1 <= num2 :
print "num1 is either less than or equal to num2";
else:
print "num1 is neither less than or equal to num2";
if num1 >= num2 :
print "num1 is either greater than or equal to num2";
else:
print "num1 is neither greater than or equal to num2";
# changing the values of num1 and num2
num1 = 40
num2 = 40
print"nIf num1 = 40 and num2 = 40. Then,";
if num1 <= num2 :
print "num1 is either less than or equal to num2";
else:
print "num1 is neither less than or equal to num2";
if num1 >= num2 :
print "num1 is either greater than or equal to num2";
else:
print "num1 is neither greater than or equal to num2";
1-Introduction to Python, features of python, history of python(1).pptx
Python Assignment Operators - Example
Program
num1 = 25
num2 = 10
res = 0
print "If num1 = 25 and num2 = 10. Then,";
res = num1 + num2
print "num1 + num2 = ", res;
res += num1
print "res + num1 = ", res;
res -= num1
print "res - num1 = ", res;
res *= num1
print "res * num = ", res;
res /= num1
print "res / num1 = ", res;
# changing the values of res
res = 2
res %= num1
print "res % num1 = ", res;
res **= num1
print "res ** num1 = ", res;
res //= num1
print "res // num1 = ", res;
1-Introduction to Python, features of python, history of python(1).pptx
Bitwise operators
# Python Operators - Python Bitwise Operators - Example Program
num1 = 60
num2 = 13
res = 0
print "If num1 = 60 and num2 = 13. Then,";
res = num1 & num2;
print "num1 & num2 = ", res;
res = num1 | num2;
print "num1 | num2 = ", res;
res = num1 ^ num2;
print "num1 ^ num2 = ", res;
res = ~num1;
print "~num1 = ", res;
res = ~num2;
print "~num2 = ", res;
res = num1 << 2;
print "num1 << 2 = ", res;
res = num2 << 2;
print "num2 << 2 = ", res;
res = num1 >> 2;
print "num1 >> 2 = ", res;
res = num2 >> 2;
print "num2 >> 2 = ", res;
# changing the values of num1 and num2
num1 = 60
num2 = 0
print "nIf num1 = 60 and num2 = 0. Then,";
res = num1 & num2;
print "num1 & num2 = ", res;
res = num1 | num2;
print "num1 | num2 = ", res;
1-Introduction to Python, features of python, history of python(1).pptx
Logical operators
# Python Operators - Python Logical Operators - Example Program
num1 = 100
num2 = 200
res = 0
print("If num1 = 100 and num2 = 200. Then,");
if(num1 and num2):
print("Both num1 and num2 are true");
else:
print("Either num1 or num2 is not true, or both are not true");
if(num1 or num2):
print("Either num1 or num2 is true, or both are true");
else:
print("Neither num1 nor num2 is true");
# changing the values of num1 and num2
num1 = 5
num2 = 0
print("nIf num1 = 5 and num2 = 0. Then,");
if(num1 and num2):
print("Both num1 and num2 are true");
else:
print("Neither num1 nor num2 is true");
if(num1 or num2):
print("Either num1 or num2 is true, or both are
true");
else:
print("Neither num1 nor num2 is true");
if not(num1 and num2):
print("Neither num1 nor num2 is true");
else:
print("Both num1 and num2 are true");

More Related Content

Similar to 1-Introduction to Python, features of python, history of python(1).pptx (20)

PDF
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
KosmikTech1
 
PPTX
Python (Data Analysis) cleaning and visualize
IruolagbePius
 
PDF
Python 3.x quick syntax guide
Universiti Technologi Malaysia (UTM)
 
PPT
Python - Module 1.ppt
jaba kumar
 
PPTX
Python for Beginners(v1)
Panimalar Engineering College
 
PPT
Python Programming Introduction demo.ppt
JohariNawab
 
PPTX
python ppt | Python Course In Ghaziabad | Scode Network Institute
Scode Network Institute
 
PDF
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...
manikamr074
 
PPTX
Python basics
Manisha Gholve
 
PPTX
python notes for MASTER OF COMPUTER APPLIICATION_ppt.pptx
yuvarajkumar334
 
PDF
CPPDS Slide.pdf
Fadlie Ahdon
 
ODP
Python slide.1
Aswin Krishnamoorthy
 
PPTX
IOT notes,................................
taetaebts431
 
DOCX
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
faithxdunce63732
 
PPTX
Python knowledge ,......................
sabith777a
 
PPTX
Programming in Python
Tiji Thomas
 
PPTX
Python Basics
Adheetha O. V
 
PDF
Code In PythonFile 1 main.pyYou will implement two algorithms t.pdf
aishwaryaequipment
 
PPTX
Introduction to python
MaheshPandit16
 
PDF
How To Tame Python
Mohd Anwar Jamal Faiz
 
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
KosmikTech1
 
Python (Data Analysis) cleaning and visualize
IruolagbePius
 
Python 3.x quick syntax guide
Universiti Technologi Malaysia (UTM)
 
Python - Module 1.ppt
jaba kumar
 
Python for Beginners(v1)
Panimalar Engineering College
 
Python Programming Introduction demo.ppt
JohariNawab
 
python ppt | Python Course In Ghaziabad | Scode Network Institute
Scode Network Institute
 
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...
manikamr074
 
Python basics
Manisha Gholve
 
python notes for MASTER OF COMPUTER APPLIICATION_ppt.pptx
yuvarajkumar334
 
CPPDS Slide.pdf
Fadlie Ahdon
 
Python slide.1
Aswin Krishnamoorthy
 
IOT notes,................................
taetaebts431
 
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
faithxdunce63732
 
Python knowledge ,......................
sabith777a
 
Programming in Python
Tiji Thomas
 
Python Basics
Adheetha O. V
 
Code In PythonFile 1 main.pyYou will implement two algorithms t.pdf
aishwaryaequipment
 
Introduction to python
MaheshPandit16
 
How To Tame Python
Mohd Anwar Jamal Faiz
 

Recently uploaded (20)

PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
Digital water marking system project report
Kamal Acharya
 
PPTX
Distribution reservoir and service storage pptx
dhanashree78
 
PDF
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
PPTX
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPT
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
PPTX
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PPTX
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Digital water marking system project report
Kamal Acharya
 
Distribution reservoir and service storage pptx
dhanashree78
 
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
Design Thinking basics for Engineers.pdf
CMR University
 
Ad

1-Introduction to Python, features of python, history of python(1).pptx

  • 2. 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.
  • 3. 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.
  • 4. Support in Python  Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles.  It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.  Python interpreters are available for installation on many operating systems, allowing Python code execution on a wide variety of systems.  Python is a multi-paradigm programming language: object-oriented programming and structured programming are fully supported, and there are a number of language features which support functional programming and aspect-oriented programming
  • 5. Brief History  Invented in the Netherlands, early 90s by Guido van Rossum  Named after Monty Python  Open sourced from the beginning, managed by Python Software Foundation  Considered a scripting language, but is much more  Scalable, object oriented and functional from the beginning  Used by Google from the beginning
  • 6. Features of Python  Simple  Easy to Learn  Free and Open Source  High-level Language  Portable  Interpreted  Object Oriented  Extensible  Embeddable
  • 7. Python Installation Steps  Step-1: Browse the link to download the python latest version https://www.python.org/  Step-2: Install it by double click and choose the system path to install  Step-3: Finally , Click the Finish button . Python Screen
  • 8. 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.
  • 9. 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:
  • 10. 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 # Python Program - Get Integer Input from User while True: print("Enter '0' for exit.") val = int(input("Enter any number: ")) if val == 0: break else: print("You have just entered:", val) print()
  • 12. Get Floating-point Input from User This python program shows how to get floating-point input from user in python: # Python Program - Get Floating-point Input from User while True: print("Enter '0' for exit.") val = float(input("Enter any number: ")) if val == 0: break else: print("You have just entered:", val) print()
  • 14. Get String Input from User Here is another program shows how to get string input from user in python: # Python Program - Get String Input from User while True: print("Enter 'x' for exit.") val = raw_input("Enter any string: ") if val == 'x': break else: print("You have just entered:", val) print()
  • 16. Complete Version of Getting Input from User Here is the complete version of getting input from user in python: # Python Program - Get Input from User - Complete Version while True: print("Enter '0' for exit.") val = int(input("Enter any Number: ")) flt = float(input("Enter any Floating-point Number: ")) strg = raw_input("Enter any String: ") if val == 0: break else: print "Integer:",val print "Floating-point:",flt print "String:",strg
  • 18. Variables in Python  Variables in Python, are the reserved memory locations to store the values in a Python program.  It means that, whenever you are creating a variable in Python programming, you are reserving some space in the memory for that variable.  Based on variable's data type, python interpreter allocates the memory and decides what can be stored in the reserved memory. Here is an example assigning values to the variables in Python.,
  • 20. Python Variables - Example Program # Python Variables - Example Program 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
  • 22. 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 II. Strings type III. List type IV. Tuple type V. Dictionary type
  • 23. 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. The syntax of the del statement is − del var1[,var2[,var3[....,varN]]]].  You can delete a single object or multiple objects by using the del statement. For example − del var del var_a, var_b
  • 24. 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)
  • 25. Python Number Example # Python Number Data Type - Example Program num1 = 10 num2 = 20 print("num1 = ", num1, " and num2 = ", num2);
  • 26. use of del keyword in python  # Python Number Data Type - Example Program  num1 = 10  num2 = 20  print "num1 = ", num1, " and num2 = ", num2  del num1, num2  print "num1 = ", num1, " and num2 = ", num2
  • 28. String Data Type  Strings in Python, are contiguous set of characters between quotation marks.  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.
  • 29. String Example  # Python Strings - Example Program  str1 = 'Hello Python'  str2 = "This is Python Strings Tutorial"  print(str1, "n", str2);
  • 30. String Examples # Python String - Example Program str = 'Hello Python' print (str) # this will print the complete string print (str[0]) # this will print the first character of the string print (str[2:8]) # this will print the characters starting from 3rd to 8th print (str[3:]) # this will print the string starting from the 4th character print (str * 3) # this will print the string three times print (str + "String") # this will print the concatenated string
  • 32. List Data type  A list in Python, contains items separated by commas and enclosed within square brackets.  A list basically contains items separated by commas and enclosed within the square brackets [ ]. Items in the list needn't be of the same type.  Examples: list1 = ['computer', 'programming', 1957, 2070, 3242]; list2 = [1, 2, 3, 4, 5]; list3 = ["a", "b", "c", "d", "e"];
  • 33. Python List Example # Python Lists - Example Program list1 = ["python", "list", 1952, 2323, 432] list2 = ["this", "is", "another", "list"] print list1 # this will print the complete list print list1[1:4] # this will print the elements starting from 2nd till 4th print list1[1:] # this will print the elements starting from the 2nd element print list1[0] # this wil print the first element of the list print list1 * 2 # this will print the list two times print list1 + list2 # this will print the concatenated list
  • 35. Tuples Data Type  A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.  The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.  Examples: tuple1 = ("python", "tuple", 1952, 2323, 432); tuple2 = (1, 2, 3, 4, 5); tuple3 = ("a", "b", "c", "d", "e");
  • 36. Tuple Example # Python Tuple - Example Program tuple1 = ("python", "tuple", 1952, 2323, 432); print tuple1 # this will print the complete tuple print tuple1[1:4] # this will print the elements starting from 2nd till 4th print tuple1[1:] # this will print the elements starting from the 2nd element print tuple1[0] # this wil print the first element of the tuple print tuple1 * 2 # this will print the tuple two times
  • 38. Dictionary Data type  Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs.  A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.  Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]).
  • 39. Dictionary Example # Python Dictionary - Example Program dictionary1 = {} dictionary1['one'] = "This is one" dictionary1[2] = "This is two" smalldictionary = {'name': 'Devraj','id':9388, 'branch': 'cs'} print dictionary1[2] # this will print the values for 2 key print dictionary1['one'] # this will print the value for 'one' key print smalldictionary # this will print the complete dictionary print smalldictionary.keys() # this will print all the keys print smalldictionary.values() # this will print all the values
  • 41. Determine Variable's Type in Python  You can use the function type() available in Python, to determine the type of variable in Python.  Here is an example showing the use of type() function in determining variable's type in Python # Python Data Types - Example Program i=10 print(type(i)) f=324.423 print(type(f)) b=True print(type(b)) str="Python Data Types" print(type(str))
  • 43. 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
  • 44. Python Arithmetic Operators # Python Operators - Python Arithmetic Operators - Example Program num1 = 23 num2 = 10 res = 0 print("If num1 = 23 and num2 = 10. Then,"); res = num1 + num2 print("num1 + num2 = ", res) res = num1 - num2 print("num1 - num2 = ", res) res = num1 * num2 print("num1 * num2 = ", res) res = num1 / num2 print("num1 / num2 = ", res) res = num1 % num2 print("num1 % num2 = ", res) #changing the values of num1 and num2 num1 = 2 num2 = 3 print("nIf num1 = 2 and num2 = 3. Then,"); res = num1 ** num2 print("num1 ** num2 = ", res) #again changing the values of num1 and num2 num1 = 10 num2 = 5 print("nIf num1 = 10 and num2 = 5. Then,"); res = num1 // num2 print("num1 // num2 = ", res)
  • 46. Comparison operators in Python Operator Meaning == 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
  • 47. Comparison Operators - Example Program # Python Operators - Comparison Operators - Example Program num1 = 23 num2 = 10 res = 0 print "If num1 = 23 and num2 = 10. Then,"; if num1 == num2 : print "num1 is equal to num2"; else: print "num1 is not equal to num2"; if num1 != num2 : print "num1 is not equal to num2"; else: print "num1 is equal to num2"; if num1 < num2 : print "num1 is less than num2"; else: print "num1 is not less than num2"; if num1 > num2 : print "num1 is greater than num2"; else: print "num1 is not greater than num2"; if num1 <= num2 : print "num1 is either less than or equal to num2"; else: print "num1 is neither less than or equal to num2"; if num1 >= num2 : print "num1 is either greater than or equal to num2"; else: print "num1 is neither greater than or equal to num2"; # changing the values of num1 and num2 num1 = 40 num2 = 40 print"nIf num1 = 40 and num2 = 40. Then,"; if num1 <= num2 : print "num1 is either less than or equal to num2"; else: print "num1 is neither less than or equal to num2"; if num1 >= num2 : print "num1 is either greater than or equal to num2"; else: print "num1 is neither greater than or equal to num2";
  • 49. Python Assignment Operators - Example Program num1 = 25 num2 = 10 res = 0 print "If num1 = 25 and num2 = 10. Then,"; res = num1 + num2 print "num1 + num2 = ", res; res += num1 print "res + num1 = ", res; res -= num1 print "res - num1 = ", res; res *= num1 print "res * num = ", res; res /= num1 print "res / num1 = ", res; # changing the values of res res = 2 res %= num1 print "res % num1 = ", res; res **= num1 print "res ** num1 = ", res; res //= num1 print "res // num1 = ", res;
  • 51. Bitwise operators # Python Operators - Python Bitwise Operators - Example Program num1 = 60 num2 = 13 res = 0 print "If num1 = 60 and num2 = 13. Then,"; res = num1 & num2; print "num1 & num2 = ", res; res = num1 | num2; print "num1 | num2 = ", res; res = num1 ^ num2; print "num1 ^ num2 = ", res; res = ~num1; print "~num1 = ", res; res = ~num2; print "~num2 = ", res; res = num1 << 2; print "num1 << 2 = ", res; res = num2 << 2; print "num2 << 2 = ", res; res = num1 >> 2; print "num1 >> 2 = ", res; res = num2 >> 2; print "num2 >> 2 = ", res; # changing the values of num1 and num2 num1 = 60 num2 = 0 print "nIf num1 = 60 and num2 = 0. Then,"; res = num1 & num2; print "num1 & num2 = ", res; res = num1 | num2; print "num1 | num2 = ", res;
  • 53. Logical operators # Python Operators - Python Logical Operators - Example Program num1 = 100 num2 = 200 res = 0 print("If num1 = 100 and num2 = 200. Then,"); if(num1 and num2): print("Both num1 and num2 are true"); else: print("Either num1 or num2 is not true, or both are not true"); if(num1 or num2): print("Either num1 or num2 is true, or both are true"); else: print("Neither num1 nor num2 is true"); # changing the values of num1 and num2 num1 = 5 num2 = 0 print("nIf num1 = 5 and num2 = 0. Then,"); if(num1 and num2): print("Both num1 and num2 are true"); else: print("Neither num1 nor num2 is true"); if(num1 or num2): print("Either num1 or num2 is true, or both are true"); else: print("Neither num1 nor num2 is true"); if not(num1 and num2): print("Neither num1 nor num2 is true"); else: print("Both num1 and num2 are true");