data-expressions-statements
data-expressions-statements
Data-expressions-statements
Two Marks
A value is one of the basic things ina programs like a letter or a number. The values are
belonging to different types. For example
>>> type(8Hello9)
<type 8str9>
>>> type(17)
<type 8float9>
2. Define variables.
A variable is a name that refers to a value. They can contain both letters and numbers but
they do not begin with a letter. The underscore 8_9 can appear in a name.
Example:
>>> n=176
A Boolean expression is an expression that is either true (or) false. The Operator == which
compares two operands and produces True if they are equal otherwise it is false.
>>> 5==5
True
>>> 5==6
False
4. Define String
>>> fruit=9banana9
Output:
5.Define List.
A List is a sequence of values. The values are characters in a list they can be of any type. The
values in a list are called elements.
Examples
>>> n=[17,35]
>>> n[0]=5
7. Define Expression
8. Define Tuples
A Tuple is a sequence of values. The values can be of any type, They are indexed by integers.
>>> t=(8a9,9b9,9c9)
>>> t[1:3]
>>(8a9,9b9)
a=5
b=6
a,b=b,a
print (a,b)
In – Evaluates to true, if it finds a variable in the specified sequence and false otherwise.
Not in- Evaluates to true, if it does not finds a variable in the specified sequence and false
otherwise.
12.Define Functions
Example
def print_1():
print (<welcome=)
13.Define Module
1.Function arguments
2.keyword arguments
3.Default arguments
1. Define Operators. What are the different types of Operators in python? Explain in
detail.
Operators are the constructs which can manipulate the value of operands.
Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator.
Types of Operator
Arithmetic Operators
Comparison (Relational) Operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
These operators compare the values on either sides of them and decide the relation among
them. They are also called Relational operators.
Bitwise operator works on bits and performs bit by bit operation. Assume if a = 60; and b =
13; Now in binary format they will be as follows −
a = 0011 1100
b = 0000 1101
-----------------
~a = 1100 0011
There are following logical operators supported by Python language. Assume variable a holds
10 and variable b holds 20 then
Python9s membership operators test for membership in a sequence, such as strings, lists, or
tuples. There are two membership operators as explained below
Identity operators compare the memory locations of two objects. There are two Identity
operators explained below:
The following table lists all operators from highest precedence to lowest.
Operator Description
** Exponentiation (raise to the power)
Complement, unary plus and minus (method names for the last two
~+-
are +@ and -@)
* / % // Multiply, divide, modulo and floor division
+- Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'
^| Bitwise exclusive `OR' and regular `OR'
<= < > >= Comparison operators
<> == != Equality operators
= %= /= //= -= += *=
Assignment operators
**=
is is not Identity operators
in not in Membership operators
A function is a block of organized, reusable code that is used to perform a single, related
action. Functions provide better modularity for application and a high degree of code reusing.
Defining a Function
Function blocks begin with the keyword def followed by the function name and
parentheses ( ( ) ).
Any input parameters or arguments should be placed within these parentheses. You
can also define parameters inside these parentheses.
The first statement of a function can be an optional statement - the documentation
string of the function or docstring.
The code block within every function starts with a colon (:) and is indented.
The statement return [expression] exits a function, optionally passing back an
expression to the caller. A return statement with no arguments is the same as return
None.
Syntax
By default, parameters have a positional behavior and you need to inform them in the same
order that they were defined.
Example
The following function takes a string as input parameter and prints it on standard screen.
Calling a Function
Defining a function only gives it a name, specifies the parameters that are to be included in
the function and structures the blocks of code.
Once the basic structure of a function is finalized, you can execute it by calling it from
another function or directly from the Python prompt. Following is the example to call
printme() function −
#!/usr/bin/python
All parameters (arguments) in the Python language are passed by reference. It means if you
change what a parameter refers to within a function, the change also reflects back in the
calling function. For example −
mylist = [10,20,30];
changeme( mylist );
print "Values outside the function: ", mylist
Values inside the function: [10, 20, 30, [1, 2, 3, 4]]
Values outside the function: [10, 20, 30, [1, 2, 3, 4]]
Function Arguments
You can call a function by using the following types of formal arguments:
Required arguments
Keyword arguments
Default arguments
Variable-length arguments
Required arguments
Required arguments are the arguments passed to a function in correct positional order. Here,
the number of arguments in the function call should match exactly with the function
definition.
print str
return;
printme()
Keyword arguments
Keyword arguments are related to the function calls. When you use keyword arguments in a
function call, the caller identifies the arguments by the parameter name.
This allows you to skip arguments or place them out of order because the Python interpreter
is able to use the keywords provided to match the values with parameters. You can also make
keyword calls to the printme() function in the following ways −
#!/usr/bin/python
Default arguments
A default argument is an argument that assumes a default value if a value is not provided in
the function call for that argument.
Variable-length arguments
You may need to process a function for more arguments than you specified while defining
the function. These arguments are called variable-length arguments and are not named in the
function definition, unlike required and default arguments.
An asterisk (*) is placed before the variable name that holds the values of all nonkeyword
variable arguments. This tuple remains empty if no additional arguments are specified during
the function call. Following is a simple example −
def sum(a,b):
return a+b
def sub(a,b):
return a-b
def mul(a,b):
return a*b
def div(a,b):
return(a//b)
print ("Subtraction",sub(8,4))
print ("Multiplication",mul(8,4))
print ("Division",div(8,2))
Output:
Addition 11
Subtraction 4
Multiplication 32
Division 4
def rotate(l,y=1):
if(len(l)==0):
return 1
y=y%len(l)
return l[y:]+l[:y]
print(rotate([1,2,3,4]))
print(rotate([2,3,4,1]))
print(rotate([3,4,1,2]))
print(rotate([3,2,1,4]))
Output:
[2, 3, 4, 1]
[3, 4, 1, 2]
[4, 1, 2, 3]
[2, 1, 4, 3]
def swap(a,b):
a,b=b,a
swap(4,5)
Output:
import math
p1 = [4, 0]
p2 = [6, 6]
print(distance)
Output:
6.324555320336759
5.Sum of n numbers
def sum(n):
s=0
for i in range(1,n):
s=s+i
return s
n=int(n)
Output:
Sum of n numbers 45
def sumofevenodd(n):
se=0
so=0
for i in range(1,n):
if i%2==0:
se=se+i
else:
so=so+i
return se,so
n=int(n)
Output:
def sum(farg,*args):
s=0
for i in args:
s=s+i
sum(1,4,5,6)
Output:
def someFunction(**kwargs):
if 'text' in kwargs:
print (kwargs['text'])
someFunction(text='welcome')
Output:
Welcome
9. Keyword arguments
def print_info(name,age=25):
print ("Name:",name)
print("Age",age)
print_info(name='Uma',age=35)
print_info(name='Reka')
Output:
Name: Uma
Age 35
Name: Reka
Age 25