You are on page 1of 24

Python

Presented by:
M Uday Kumar
M Tech VLSI 1 year
CONTENTS:

1. What is python?
2. Why python?
3. Applications of python?
4. Variables
5. Tokens
6. Types of operators
7. Data-types
8. Flow control
9. Functions
10.File handling
What is Python?

Python is a high-level, general purpose,interpreted and


dynamically typed programming language.

Python is a versatile language used in so many domains.

Created by Guido Van Rossum and first released in 1991.


Why Python?
● It is simple to use.
● It is a free and open-source programming language.
● It is easy to understand.
● Fewer code lines.
● Less time.
● It has a large library.
Applications of Python:
● Web Applications
● Scientific and Numeric Computing
● Machine Learning
● Games and 3D Applications
● Automation
Variables:
● A Variable is a reserved memory location to store values.

● We can assign values to variables without declaring data-type.


Eg : a=2
name=”python”
average=22.50

● Multiple assignment of variables is possible.


Eg : x,y,z=10,20,30
Tokens:
● It is the basic component of the source code.
● There are four types of tokens. They are:
1.Keywords
2.Identifiers
3.Literals
4.Operators
Keywords:
● Python keywords are special reserved words.
● Each keyword has a special meaning and has specific operation.
● Never use keywords as a variable.
● Some of the keywords are:

def return continue break

class if else lambda

and or import for


Identifiers:

● Identifiers are the names used to identify a variable,function,class or an


object.

Rules for identifiers:


● Keywords cannot be used as identifiers.
● An identifier cannot start with a digit.
● We cannot use special symbols like !, @, #, $, % etc. in our identifier.
● Identifiers can be a combination of letters in lowercase (a to z) or

uppercase (A to Z) or digits (0 to 9) or an underscore _.


Literals:

● Literals are the constants used in python.


● There are four types of literals.
They are: 1.string literals
2.numeric literals
3.boolean literals
4.special literals
Types of operators:

1)Arithmetic operator
2)Assignment operator
3)Comparsion operator
4)Logical operator
5)Bitwise operator
6)Identity operator
1.Arithmetic operators:
● Arithmetic operators are used to perform mathematical operations like
addition, subtraction, multiplication etc.
Eg: + ,- ,*,/ ,%

2.Assignment operators:

● Assignment operators are used in Python to assign values to variables.


Eg: =,+=,-=,*=
3.Comparsion operators:
● Comparison operators are used to compare values. It either returns
True or False according to the condition.
Eg: <,>,<=,>=,!=
4.Logical operators:

● Logical operators are the and, or, not operators.

5.Bitwise operators:

● Bitwise operators act on operands as if they were string of binary digits.


It operates bit by bit.
Eg: &,>>,<<,~,|

6.Identity operators:
● ‘is’ and ‘is not’ are the identity operators in Python. They are
used to check if two values (or variables) are located on the
same part of the memory.
Python Datatypes

Immutable
Mutable

numbers strings tuples lists dictionaries sets


find( ): returns the position of string.
Eg: name=”that”
name.find(‘at’) #output: 2

 replace( ): replaces the one string/character with the other string/character.


Eg: name.replace(‘at’,’it’) #output: thit

 type( ): returns the type of variable.


Eg: type(name) #output:<class 'str'>

 split(): creates a split basis on a character


Eg:>>> name="1,2,3" #output:['1', '2', '3']
>>> name.split(",")

 count( ): returns the count of the character in the string


 Eg: >>> name="apple"
>>> name.count('p') #output:2
 upper(): converts the character/string to upper case

 max()/min(): returns the maximum/minimum(ASCII) value

Tuples: They are group of values within ( )


Eg: group=(‘a’,’b’,’c’,’d’)

 Concatenation(): Adds two strings/characters


Eg:>>> group1=('c,'d')
>>> group=('a','b')
>>> group+=group1 output:('a', 'b', 'c', 'd')
List:
• They are group of values within [ ]
● Lists can consist of a collection of mixed data types, stored by relative

positions.

● append(): to add any values to the list.


Eg: mylist.append( )

● insert(): to insert particular value at particular index.


Eg: mylist.insert(index,value)

Dictionaries:
● A group of values written in { }.
eg: mydict={1: ‘pen’ , 2: ‘ball’}

● len(): returns the length of the dictionary.


len(mydict)
● key(): it returns all the keys in the dictionary.

Sets: sets are the unordered collection of items within { }

● Union: union of two sets


syntax: set1 | set2

● Intersection: common values


syntax: set1 & set2

● Difference:removes set2 elements from set1


syntax: set1 - set2
Flow control:

● If-else
● Nested if-else
● for
● while
● break
● continue
If-else:
Syntax: if(condition) :
statements 1
else
statements 2
...
Nested if-else:

Syntax: if(condition) :
statements 1
elif
statements 2
else
Statements 3
....
for:

Syntax: for iterating_var in sequence:


execute statements

while():

Syntax: while(condition is true):


statements
Function:

● A function is a block of organised,reusable set of instructions


that is used to perform specific operations.

● Two types of functions:1.User Defined function


2.Built-in function

● Syntax: def fun_name(arg1,arg2,...):


statements..
return

Lambda function: function with no name

Syntax: lambda arguments : expressions


File Handling:
1)Open
2)Read
3)write/create
4)Delete

“x”-create: creates a new file


“w”-write:overwrites any existent data in the file
“r”-read:reads the file
“a”-append:will append to the end of the file

Eg:f=open(“file loaction“,”w/r/a/x”)
Thank you

You might also like