UNIT-1python Introduced
UNIT-1python Introduced
Founder:
Guido van Rossum in 1991
Python versions
1. Python 1.0 – January 1994
2. Python 2.0 – October 2000
3. Python 3.0- December 2008
Software Requirements:
Editor is required to write a program.
Need to install Interpreter (IDLE) and I.D.E (for GUI purpose).Various IDE are available. Ex: pycharm, Atom,
Anaconda, etc.
Latest version is 3.7. It can be downloaded from official website of python.
For example:
x=2
print(x)
x=4.5
print(x)
x=’a’
Comments
For single line comment #
For multi line comment “ “ “ line1
Line 2 “““
Variable assignment
X=5
x,y=3,4
x=”a”
User Input
Syntax: var_name=input()
Default data type is string, so to convert it into other type, we need to do explicit casting.
Ex: var_name=int(input())
var_name=str(input())
x=int(input(“enter value”))
Multiple values in single line:
Ex: x,y=int(input()), int(input())
print(x,y)
User Output:
print(“ message”)
print(“message”+var_name)
Operators
Same as any other programming languages. New operator in python is Membership operator.
1. in example: x=[“apple”,”banana”]
print(“banana” in x)
output: true
2. not in example: x=[“apple”,”banana”]
print(“banana” not in x)
output: false
Branching Conditions:
Looping:
#for loop:
Syntax: for variable_name in range (start value, end value, step size):
Statements
Ex: for i in range(5): Output: 0,1,2,3,4
print(i)
Ex: for i in range(5,10): Output: 5,6,7,8,9
print(i)
Ex: for i in range(10,5,-1): Output: 10,9,8,7,6
print(i)
Ex: for i in range(5,10,2): Output: 5,7,9
print(i)
#while:
Syntax: while condition:
Statements
3. Default Arguments:
Ex: def display(name,age,salary=5000):
print(name+age+salary)
n=”audi”
a=35
s=500000
display(salary=s,name=n,age=a)
display(name=n,age=a) #call to default argument of salary
Functions can be treated like objects of any other type, e.g., int or list. They have types, e.g., the
expression type (fact) has the value <type 'function'>; they can appear in expressions, e.g., as the right-
hand side of an assignment statement or as an argument to a function; they can be elements of lists.
Example:
def app(f,l):
for i in range(len(l)):
l[i]=f(l[i])
def fact(n):
if n==1:
return 1
else:
return(n*fact(n-1))
l=[1,-2,3.33,5]
print("list=",l)
app(abs,l)
print("list after abs=",l)
app(int,l)
print("list after int=",l)
print("factorial=",fact(4))
app(fact,l)
print("list after factorial recursion=",l)
Output:
List = [1, -2, 3.33, 5]
Properties:
1. Lambda functions can have any numbers of arguments but only one expression, which will be
evaluated.
2. One is free to use lambda functions wherever function objects are required.
3. These functions are syntactically restricted to a single expression.
map()
Syntax: map(function, iterable, ...)
map() Parameter
function : map() passes each item of the iterable to this function.
iterable : iterable which is to be mapped
You can pass more than one iterable to the map() function.
filter():
Example: l=[1,4,5,6,2,7,8,34,25]
x=filter(lambda y:y>4,l)
print(list(x)) #output: [5, 6, 7, 8, 34, 25]
Write a program to find out odd numbers from given list.
zip():
Example: l1=[1,2,3,4,5]
l2=['a','b','b','d']
l4=['e','e','e','e']
l3=zip(l1,l2,l4)
print(list(l3)) #Output: [(1, 'a', 'e'), (2, 'b', 'e'), (3, 'b', 'e'), (4, 'd', 'e')]
Example: l1=[1,2,3,4,5]
l2=['a','b','b','d']
l3=zip(l1,l2)
print(dict(l3)) #Output: {1: 'a', 2: 'b', 3: 'b', 4: 'd'}
Scope of variables:
Default scope of variable is global.
Prof. Zalak Trivedi, CE Dept, ITE, IU
Variables defined in certain block are having scope local to that block.
Example :
Modules:
A module is a .py file containing Python definitions and statements. We could create, for example, a
file circle.py containing following code:
pi = 3.14159
def area(radius):
return pi*(radius**2)
def circumference(radius):
return 2*pi*radius
def sphereSurface(radius):
return 4.0*area(radius)
def sphereVolume(radius):
return (4.0/3.0)*pi*(radius**3)
import circle
print circle.pi
print circle.area(3)
print circle.circumference(3)
print circle.sphereSurface(3)
will print
3.14159
28.27431
18.84954
113.09724
Identifiers whose names starts with two underscores (__) are known as private identifiers.
Specific stride in slicing operation: In the slice operation,you can specify a third argument as the
stride,which refers to the number of characters to move forward after the first character is retrieved
from the string.
01234567891011121314151617181920212223242526272829
Ex: str = “welcome to the world of python”
Print(“str[2:10]=”,str[2:10]) #lcome to
Print(“str[2:10:1]=”,str[2:10:1]) #lcome to
Print(“str[2:10:2]=”,str[2:10:2]) #loet
Print(“str[2:13:4]=”,str[2:13:4]) #le
#list type
name=["indus","uni","iite","ahmedabad"]
print (name[2:4])
print (name)
print l.upper()
print l.replace("u","a")
#check existence
x = ["apple", "banana"]
print("banana" in x)
x.append("orange")
print (x)
x.insert(1,"lemon")
print (x)
#tupples:
output : banana
Once a tuple is created, you cannot change its values. Tuples are unchangeable.
#Add Items
Once a tuple is created, you cannot add items to it. Tuples are unchangeable.
thistuple = ("apple", "banana", "cherry")
thistuple[3] = "orange" # This will raise an error
print(thistuple)
#Remove Items
Tuples are unchangeable, so you cannot remove items from it, but you can delete the tuple completely:
If the item to remove does not exist, remove() will raise an error, where as discard() doesn’t.
#Accessing Items
x = thisdict ["model"]
output: mustang
There is also a method called get() that will give you the same result:
x = thisdict.get("model")
Testing:
Purpose of testing is to show that bugs exist, not to show that a program is bug-free.
Test case/ Test suite:
The key to testing is finding a collection of inputs, called a test suite, that has a high likelihood of
revealing bugs, yet does not take too long to run. The key to doing this is partitioning the space of
all possible inputs into subsets that provide equivalent information about the correctness of the
program, and then constructing a test suite that contains one input from each partition.
Types: Black box, Glass box/White box, Unit testing, integrated testing.
Black box: Heuristics based on exploring paths through the specification fall into a class called black-
box testing.
Glass box/White box: Heuristics based on exploring paths through the code fall into a class called
glass-box testing.
Unit Test: During this phase testers construct and run tests designed to ascertain whether individual
units of code (e.g., functions) work properly.
Integrated test: integration testing, which is designed to ascertain whether the program as a whole
behaves as intended.
Drivers simulate parts of the program that use the unit being tested,
Stubs simulate parts of the program used by the unit being tested.
• Check the reasonableness of the environment and arguments supplied by the caller (calling a function
with inappropriate arguments are a common error),
• Modify arguments and global variables in a manner consistent with the specification, and
In industry, the testing process is often highly automated. Testers do not sit at terminals typing
inputs and checking outputs. Instead, they use test drivers that autonomously
• Set up the environment needed to invoke the program (or unit) to be tested,
• Invoke the program (or unit) to be tested with a predefined or automatically generated sequence of
inputs,
1. Overt → covert:
An overt bug has an obvious manifestation, e.g., the program crashes or takes far longer
(maybe forever) to run than it should.
A covert bug has no obvious manifestation. The program may run to conclusion with no
problem—other than providing an incorrect answer.
Many bugs fall between the two extremes, and whether or not the bug is overt can depend upon how
carefully one examines the behavior of the program.
2. Persistent → intermittent:
A persistent bug occurs every time the program is run with the same inputs.
An intermittent bug occurs only some of the time, even when the program is run on the same inputs
and Seemingly under the same conditions.
Defensive Programming: Good programmers try to write their programs in such a way that
programming mistakes lead to bugs that are both overt and persistent. This is often called
defensive programming.