INT213 Unit1
INT213 Unit1
PYTHON PROGRAMMING
P.Raja,
Assistant Professor in Embedded System Design(D4),
School of Electronics and Electrical Engineering
Unit I
Introduction : python programming language, introduction
to program and debugging, formal and natural language
Variables, Expression and Statements : Values and types,
variables, variables name and keywords, statements,
operators and operand, order of operations, operations on
string, composition and comments
Conditionals and Iteration : modulus operator, Boolean
expressions, logic operators, conditional, alternative
execution, nested conditionals and return statements, while
statements, encapsulation and generalization
Functions and recursion : function calls, type conversion
and coercion, math functions, adding new function,
parameters and argument, recursion and its use.
Difference between Compiler & Interpreter
IEEE spectrum list of top programming language 2021.
The list of programming languages is based on popularity.
Introduction - Python programming
language:
The History of Python:-
Python was created by Guido van Rossum in the Netherlands
in 1990 and was named after the popular British comedy
troupe Monty Python’s Flying Circus.
Van Rossum developed Python as a hobby, and Python has
become a popular programming language widely used in
industry and academia due to its simple, concise, and intuitive
syntax and extensive library.
Cont…
general-purpose programming language
you can use Python to write code for any programming task
in the Google search engine
In mission-critical projects at NASA
in transaction processing at the New York Stock Exchange.
Python is interpreted
Python is an object-oriented programming (OOP) language
Features of Python programming
Introduction - introduction to program and debugging :
Script:
A program stored in a file (usually one that will be interpreted).
Program:
A set of instructions that specifies a computation.
Algorithm:
A general purpose for solving a category of problem.
Bug:
An error in a program.
Cont…
Debugging:
The process of finding and removing any of the three kinds of
programming errors.
Syntax:
The structure of the program.
Syntax error:
An error in a program that makes it impossible to parse (and therefore
impossible to interpret).
Cont…
Runtime error:
An error that does not occur until the program has started to execute
but that prevents from continuing.
Exception:
Another name for run time error.
Semantic error:
An error in a program that makes it do something other than what the
program intended.
Semantics:
The meaning of the program.
Introduction - formal and natural
language:
Natural languages are the languages people speak, such as
English, Spanish, and French. They were not designed by people
(although people try to impose some order on them); they evolved
naturally.
Formal languages are languages that are designed by people for
specific applications. For example, the notation that mathematicians
use is a formal language that is particularly good at denoting
relationships among numbers and symbols. Chemists use a formal
language to represent the chemical structure of molecules. And most
importantly:
Casting: -
If you want to specify the data type of a variable,
this can be done with casting.
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
Cont…
Variable Names
• A variable can have a short name (like x and y) or a more
descriptive name (age, carname, total_volume).
• Rules for Python variables:
• A variable name must start with a letter or the underscore
character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters
and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are
three different variables)
Cont…
Camel Case
Each word, except the first, starts with a capital letter:
myVariableName = "John“
Pascal Case
Each word starts with a capital letter:
MyVariableName = "John“
Snake Case
Each word is separated by an underscore character:
my_variable_name = "John"
Cont…
Many Values to Multiple Variables
Python allows you to assign values to multiple variables in
one line:
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
One Value to Multiple Variables
And you can assign the same value to multiple variables in
one line:
x = y = z = "Orange"
print(x)
print(y)
print(z)
Cont…
Unpack a Collection
If you have a collection of values in a list, tuple etc. Python
allows you to extract the values into variables. This is
called unpacking.
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)
Output Variables
The Python print() function is often used to output variables.
x = "Python is awesome"
print(x)
Cont…
Global Variables
Variables that are created outside of a function (as
in all of the examples above) are known as global
variables.
Global variables can be used by everyone, both
inside of functions and outside.
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
Difference Between Identifier and Variable
Identifier
• All of them are not variables.
• They are used to name a variable, a function, a class, a structure, a
union.
• It is created to give a unique name to an entity.
• They can consist of alphabets, digits, and underscores.
• There is no punctuation or special symbol, except the underscore.
• It can be upper case or lower case.
• It can start with lower case letter, upper case letter or an underscore.
• It helps locate the name of the entity which is defined along with a
keyword.
Cont…
Variable
• It is used to give a name to a memory location.
• It holds a value.
• The names of variables are different.
• They help allot a unique name to a specific memory location.
Python Data Types
Operators and operand
Arithmetic Operators
Comparison (Relational) Operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
Arithmetic Operators
== Equal x == y
!= Not equal x != y
<< Zero fill Shift left by pushing zeros in from the right and let the
left shift leftmost bits fall off
>> Signed Shift right by pushing copies of the leftmost bit in from
right shift the left, and let the rightmost bits fall off
Python Operator Precedence
:d Decimal format
:e Scientific format, with a lower case e
:E Scientific format, with an upper case E
:f Fix point number format
:F Fix point number format, in uppercase format
(show inf and nan as INF and NAN)
:g General format
:G General format (using a upper case E for scientific notations)
:o Octal format
:x Hex format, lower case
:X Hex format, upper case
:n Number format
:% Percentage format
Operation on String
quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {}
dollars."
print(myorder.format(quantity, itemno, price))
Output:
I want 3 pieces of item 567 for 49.95 dollars.
Operation on String
String Methods
capitalize() Converts the first character to upper case
isupper() Returns True if all characters in the string are upper case
partition() Returns a tuple where the string is parted into three parts
rindex() Searches the string for a specified value and returns the last
position of where it was found
rsplit() Splits the string at the specified separator, and returns a list
startswith() Returns true if the string starts with the specified value
If Statement :-
if(condition):
#if statement
Example:
a = 10
b = 20
if a<b:
print("a is less than b")
Conditional Statements
If else statement :-
if(condition):
# if statement
else:
# else statement
Example :-
a = 10
b = 20
if a==b:
print("a and b are equal")
else:
print("a and b are not equal")
Conditional Statements
Nested if statement :-
if(condition):
if(condition):
# if statemen
Example:-
a= 1001
if a> 100:
print("Above 100")
if a > 1000:
print("and also above 1000")
Conditional Statements
Example:-
a=int(input("enter the a value"))#user give a valu
if a> 100:
print("Above 100")
if a > 1000:
print("and also above 1000")
else:
print("and also below 1000")
else:
print("below 100")
Conditional Statements
Short Hand If :-
if condition: statement
Example :-
if a > b: print("a is greater than b")
Short Hand If ... Else
statement1 if condition else statement2
statement1 if condition1 else statement2 if condition2
else statement3
Conditional Statements
Example :-
Loop Statements
For Loops :-
for val in sequence:
loop body
Example :-
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
for x in "banana":
print(x)
Loop Statements
For Loops :-
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
Loop Statements
Syntax:-
Functions
Syntax:-
Functions
Built-in function :-
range(), id(), type(), input(), eval() etc.
User-defined function :-
Functions
User-defined function :-
Functions
Function Arguments :-
1.Positional arguments
2.keyword arguments
3.Default arguments
4.Variable-length arguments
Positional Arguments
Functions
Function Arguments :-
keyword arguments
Functions
Function Arguments :-
keyword arguments
Functions
Function Arguments :-
Default arguments
Functions
Function Arguments :-
Variable-length arguments
Functions
Recursive Function :-
A recursive function is a function that calls itself, again and
again.
Functions
Anonymous/Lambda Function :-
Sometimes we need to declare a function without any name. The nameless
property function is called an anonymous function or lambda function.
Syntax of lambda function:
lambda: argument_list:expression
x = lambda a : a + 10
print(x(5))
x = lambda a, b, c : a + b + c
print(x(5, 6, 2))