Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

UNIT II – DATA, EXPRESSIONS, STATEMENTS

1) Python Interpreter and interactive mode


Overview of Python
• Python is a general-purpose interpreted, interactive, object-oriented, and high level programming language. It was
created by Guido van Rossum.
• The name it comes from the comedy show monty python’s flying circus is telecasted in BBC from 1970’s. Python
mean old world boas (snake family)
Advantages of Python
• Lot of inbuilt functions available in python
• Code size is small compare with C, C++ and Java
• It supports Dynamic datatype
• It supports Automatic memory management
• It is Easy to learn, maintain, extensive libraries
• Free and open source
Applications of python programming
• Microchip programming at Intel
• Making video games – Disney’s Toontown, Civilization IV, Vegastrike
• Youtube, Dropbox, 4D cinema, Bit Torrent
• Image Processing – 3dsMax, Maya, FreeCAD
• Google App. Engine
• Enterprise Resource Planning
• Linux OS, ubuntu, Fedora, RedHat
Difference between Compiler and Interpreter
Sl.No Compiler Interpreter
1 It takes entire program as input It takes single instruction as input
2 Intermediate Object code is generated No intermediate, directly convert to machine code
3 Memory requirement is high Less memory requirement
4 Conditional control statements are executes faster Conditional control statements are executes slower
5 Errors are displayed after entire program is Errors are displayed for every instructions interpreted.
checked. Example: C compiler Example: Python interpreter
Two ways to use the interpreter ( Python programs are executed by an interpreter )
• Interactive mode / Shell mode
>>> 2 + 2
4
• Script mode
You can store code in a file and use the interpreter to execute the contents of the file which is called a script
Python scripts have names that end with .py

2) Values and data types


• A value is a letter or a number that a program manipulates.
• Data type is set of values. The type of a value determines how it can be used in expressions.
• The values are classified into different data types: integer, string, float, Boolean and list
• If you are not sure what class a value falls into, Python has a function called type which can tell you.
>>> type("Hello, World!") # string – sequence of characters delimited by single or double quotes
<class 'str'>
>>> type(17) # integer – Whole numbers without decimal point, it can be positive or negative
<class 'int'>
>>> type(3.2) # float - Numbers with a decimal point
<class 'float'>
>>>print (2 == 2) # Boolean type having two values usually denoted True and False
True
>>>a=[1,2,3,4] # List type is a collection of sequence elements separated by comma between square bracket
>>>print(a)
[1,2,3,4]
Type converter functions
The int function can take a floating point number or a string, and turn it into an int.
>>> int(3.14)
3
The type converter float can turn an integer, a float, or a syntactically legal string into a float
>>> float(17)
17.0
The type converter str turns its argument into a string
>>> str(17)
'17'
3) Variables
• A variable is a name that refers to a value.
• The assignment operator ( = ) assign a value to a variable
>>> message = "Hello, World!"
>>> n = 17
>>> pi = 3.14159
• You can assign a value to a variable, and later assign a different value to the same variable.
>>> n = 19
>>> pi = 3.14
Variable names / Identifiers
• Variable names can contain both letters and digits, but they have to begin with a letter or an underscore.
• It is legal to use uppercase letters, but identifiers are case sensitive. Bruce and bruce are different variables.
>>> message = "Hello, World!"
>>> Message = "Hello, India!"
>>> mess_age= "Hello, Tamilnadu!”
4) Keywords
• A reserved word that is used by the compiler to parse program; you cannot use keywords like if, def, and while as
variable names.

5) Statement: An instruction that the Python interpreter can execute. Assignment (=), import, while, for and if statement.
Statements don’t produce any result.
Example: y = 3.14
6) Expression: An expression is a combination of values, variables, operators, and calls to functions. If you type an
expression at the Python prompt, the interpreter evaluates it and displays the result:
Example: >>> 1 + 1
2
>>> len("hello")
5
7) Operator: Operators is a symbol that represent computations like addition, multiplication, division and string
concatenation (+ is operator) Operand: The values the operator uses are called operands. (a, b and c are operands)
Example: >>>a=10; b=20; c=a+b; print(c)

Order of operations / Precedence of operators


• When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence.
• The acronym PEMDAS is a useful way to remember the order of operations:
• P – Parentheses, E – Exponentiation, M – Multiplication, D – Division, A – Addition, S – Subtraction
Example: >>> 2 ** 3 ** 2
512
>>> (2 ** 3) ** 2
64
8) Tuple assignment
Tuple assignment is used to swap the value directly between two variables without using temporary variable
Example: >>>a=10; b=20; a,b=b,a; print(a,b) Output: 20 10

9) Comments in python
All characters after the hash sign # and up to the end of the physical line are part of the comment and the python
interpreter ignores them
Example: >>> # This is a comment
>>> print(“Hello python”)
10) Modules
A module is a file containing Python function definitions and statements. The filename is the module name with
the suffix.py appended. Inbuilt modules are stored in standard python library like import math, import string, import time
Composition
• It is ability to take small building blocks and compose them into larger chunks.
• A function can contain another function is called function composition.
Example: Firstly, we’ll do the four steps one at a time:
response = input("What is your radius? ")
r = float (response)
area = 3.14159 * r**2
print ("The area is ", area)
Example: Now let’s compose the first two lines into a single line of code, and compose the
second two lines into another line of code.
r = float (input ("What is your radius? "))
print ("The area is ", 3.14159 * r**2)
11) Function
• A function is a block of organized, reusable code that is used to perform a single, related action.
• Functions provide better modularity for your application and a high degree of code reusing.
Uses / Advantages of function
• Maximizing code reuse and minimizing redundancy
• Procedural decomposition
12) Flow of execution
• Execution always begins at the first statement of the program. Statements are executed one at a time, in order
from top to bottom.
• A function call is like a detour in the flow of execution. Instead of going to the next statement, the flow jumps to
the function definition, executes all the statements there, and then comes back to pick up where it left off .
13) Types of function: 1. User defined function 2. Built in / Predefined function
User defined function:
• Function defined by users according to their requirements are called user defined functions
• The users can modify the functions according to their requirement
• The block of the function starts with a keyword def after which the function name with or without parameters
Elements of user defined function
• Function definition Example
• Function calling / invocation def add(a,b): # function definition
Defining a function Syntax c=a+b
def functionname(parameters): return c
body of the function result=add(10,20) #function calling
return expression print(“Answer :” , result)
Calling a function syntax Output
functionname(arguments) Answer: 30
Built in function
Functions already defined in the python programming language we can directly call them to perform a specific task
Examples: Mathematical function (sin, cos, tan, sqrt, pow), String function (lower, upper, sort, reverse...)
14) Parameters and Arguments
• Parameters and Arguments are the values or expressions passed to the functions between parentheses.
• The value of the argument is always assigned to a function variable known as parameter.
• At the time of function definition, we have to define some parameters if that function requires some arguments
to be passed at the time of calling.

Example
def add(a,b): # a and b are the parameters
c=a+b
return c
result=add(10,20) # 10 and 20 are the arguments
print(“Answer :” , result)
Output
Answer: 30
Four types of formal arguments:
• Required arguments: The number of arguments should match the defined number of parameters.
• Keyword arguments: The caller recognizes the arguments by the parameters names.
• Default arguments: We can assign a value to a parameter at the time of function definition. This value is
considered the default value to that parameter.
• Variable length arguments: There are many cases where we are required to process a function with more
number of arguments than we specified in the function definition.

Example for Required Example for Keyword Example for Default Example for Variable length
Arguments Arguments Arguments Arguments
def add(a,b): def add(a,b): def add(a,b=10): def add(a,*b):
c=a+b c=a+b c=a+b print(“Answer:”,a)
return c return c return c c=0
result=add(10,20) result=add(a=10,b=20) result=add(10,20) for i in b:
print(“Answer :” , result) print(“Answer :” , print(“Answer :” , result) c=c+b
result) result1=add(10) print(“Answer:”,c)
print(“Answer1:”,result1) print(10)
print(10,10,10)

Output: Output: Output: Output:


Answer:30 Answer:30 Answer:30 Answer:10
Answer1:20 Answer: 30

You might also like