0% found this document useful (0 votes)
172 views

PYTHON Introduction

The document provides information about the Python programming language. It discusses: - The history of Python, including its creation in the late 1980s in the Netherlands. - An introduction to Python, describing it as a widely used, high-level programming language that emphasizes code readability. - The support Python provides for multiple programming paradigms like object-oriented, imperative, and functional programming. - Details about Python interpreters being available on many operating systems, allowing code execution on a variety of systems.

Uploaded by

ananthkalvi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
172 views

PYTHON Introduction

The document provides information about the Python programming language. It discusses: - The history of Python, including its creation in the late 1980s in the Netherlands. - An introduction to Python, describing it as a widely used, high-level programming language that emphasizes code readability. - The support Python provides for multiple programming paradigms like object-oriented, imperative, and functional programming. - Details about Python interpreters being available on many operating systems, allowing code execution on a variety of systems.

Uploaded by

ananthkalvi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

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()
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()
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()
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
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.,
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
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


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
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
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
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
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))
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)

num1 = 23 #changing the values of num1 and num2


num2 = 10 num1 = 2
res = 0 num2 = 3

print("If num1 = 23 and num2 = 10. Then,"); print("\nIf num1 = 2 and num2 = 3. Then,");

res = num1 + num2 res = num1 ** num2


print("num1 + num2 = ", res) print("num1 ** num2 = ", res)

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,");

res = num1 / num2 res = num1 // num2


print("num1 / num2 = ", res) print("num1 // num2 = ", res)
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 - 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;

print "If num1 = 25 and num2 = 10.


# changing the values of res
Then,";
res = 2

res = num1 + num2


res %= num1
print "num1 + num2 = ", res;
print "res % num1 = ", res;

res += num1 res **= num1


print "res + num1 = ", res; print "res ** num1 = ", res;

res -= num1 res //= num1


print "res - num1 = ", res; print "res // num1 = ", res;
Bitwise operators
# Python Operators - Python Bitwise Operators - Example res = num1 << 2;
Program print "num1 << 2 = ", res;

num1 = 60 res = num2 << 2;


num2 = 13 print "num2 << 2 = ", res;
res = 0
res = num1 >> 2;
print "If num1 = 60 and num2 = 13. Then,"; print "num1 >> 2 = ", res;

res = num1 & num2; res = num2 >> 2;


print "num1 & num2 = ", res; print "num2 >> 2 = ", res;

res = num1 | num2; # changing the values of num1 and num2


print "num1 | num2 = ", res; num1 = 60
num2 = 0
res = num1 ^ num2;
print "num1 ^ num2 = ", res; print "\nIf num1 = 60 and num2 = 0. Then,";

res = ~num1; res = num1 & num2;


print "~num1 = ", res; print "num1 & num2 = ", res;

res = ~num2; res = num1 | num2;


print "~num2 = ", res; print "num1 | num2 = ", res;
Logical operators
# Python Operators - Python Logical Operators - Example
Program
print("\nIf num1 = 5 and num2 = 0. Then,");
num1 = 100
num2 = 200 if(num1 and num2):
res = 0
print("Both num1 and num2 are true");
print("If num1 = 100 and num2 = 200. Then,"); else:
print("Neither num1 nor num2 is true");
if(num1 and num2):
print("Both num1 and num2 are true");
else:
if(num1 or num2):
print("Either num1 or num2 is not true, or both are not print("Either num1 or num2 is true, or both
true"); are true");
else:
if(num1 or num2):
print("Either num1 or num2 is true, or both are true"); print("Neither num1 nor num2 is true");
else:
print("Neither num1 nor num2 is true"); if not(num1 and num2):
# changing the values of num1 and num2
print("Neither num1 nor num2 is true");
num1 = 5 else:
num2 = 0 print("Both num1 and num2 are true");

You might also like