Functions

Functions are used to reusability of the code. In python we declare it with a keyword def. The syntax is

def function_name(arguement):

statement_1

statement_2

Let us see the example to add two numbers using function.

def add(a,b):

return a+b

x = add(10,20)

print(x)

30

Local and Global Scope

def change(a):

a = 20

print(a)

a = 2

print(“The value of ‘a’ before the function call “, a)

print(“The value of the in the function “)

change(a)

print(” The value of ‘a’ after the function call “, a)

Output:-

The value of ‘a’ before the function call 2

The value of ‘a’ in function 20

The value of ‘a’ after the function call 2

The value of the ‘a’ does not change. This is because the scope of the variable is within the function. To extent the scope of the variable we can use the keyword global.

def change(a):

global a = 20

print(a)

a = 2

print(“The value of ‘a’ before the function call “, a)

print(“The value of the in the function “)

change(a)

print(” The value of ‘a’ after the function call “, a)

Output:-

The value of ‘a’ before the function call 2

The value of ‘a’ in function 20

The value of ‘a’ after the function call 20

Default Arguement

def show(a, b=-1):

if a>b

print(“a is greater”)

else

print(“b is greater”)

show(10)

show(10,20)

In above program, we assigned the value of the ‘b’ as -1 because to set the least possible value. The output will be.

a is greater

b is greater

Keyword argument

def assign(a, b=10, c=20):

print((“a is “, a, “b is “, b, “c is “, c)

assign(2)

assign(1, b=2)

assign(1, b=2, c=3)

It is important that if the arguement is declared with a value then any arguement cannot be assigned. Like def(a, b=10, c) throws an error.

Output:-

a is 2 b is 10 c is 20

a is 1 b is 2 c is 20

a is 1 b is 2 c is 3

Programs Using Strings

To find a element in the array, find keyword is used. It returns a integer value either if it is found or not. We can use certain conditions like found the element which starts a letter with “C”. For this type of statements it returns a boolean value either True or False.

>>> s = ” He started to learn python “

>>> s.find(“to”)

3

>>> s.find(“and”)

-1

It returned a negative binary value because the array does not have element “and”.

>>> s.endswith(“python”)

True

>>> s.startswith(“He”)

True

Palindrome

Palindrome will compare the string and the corresponding reversed string. If two strings are same the it is said to be palindrome. “Madam” is a palindrome. The below program is to check whether the entered string is palindrome or not.

>>> s = input(” Enter the string “)

for c in s:

z = z + c

if(s == z):

print(” Palindrome “)

else:

print(” Not a Palindrome “)

Output will be :-

Enter the string MADAM

Palindrome

To find number of words in String

>>> s = input(” Enter the line : “)

>>> print(” Number of words is ” % (len(s.split(” “)))

Output :-

Enter the line : He started to learn python

5

To iterate each element in String

>>> s = input(” Enter the string “)

>>> for c in s:

… print(c)

Output :-

Enter the string : python

p

y

t

h

o

n

String

Strings are generally a sequence of character. It is declared within ” “. If you want to declare with multiple lines of string, then “”” “”” is used.

\n is used to move to the next line. If we have different line \ is used to combine them.

>>> a = ” He started to \n learn python ”

>>> print(a)

He started to

learn python

>>> a = ” He started to \

… learn python ”

>>> print(a)

He started to learn python.

To get the length of the string len() command is used

>>> a = “Python”

>>> len(a)

6

Different ways to handle string

There are many ways to handle a string. The some are.

>>> s = “learn python”

>>> s.title()

Learn Python

To display all contents in Caps, upper() keyword is used. To display all contents in small letter, lower() keyword is used

>>> s = “Python”

>>> s.upper()

PYTHON

>>> s.lower()

python

To change all the capital letters to small and small to capital. swapcase() is used.

>>> s = “PyThOn”

>>> s.swapcase()

pYtHoN

To check the condition, is keyword is used. isalnum() is used to check whether the string contains only alphanumeric or not.

Isalpha() is used to check whether the string contains only aphates. If it contains, then it will return either True or false.

Similarly we can check other conditions like.

>>> s = “1234”

>>> s.isdigit()

True

>>> s = “PYTHON”

>>> s.isupper()

True

>>> s = “Python”

>>> s.islower()

False

Dictionary

Dictionaries are defined by the keyword {}. It is used to collect the data and store in a sequence.

>>> data = { ‘Name’ : ‘Chandru’ , ‘Gender’ : ‘Male’ }

>>> data[Name]

Chandru

We can add element in the dictionary by

>>> data[‘Age’] = 18

{‘Name’ : ‘Chandru’ , ‘Gender’ : ‘Male’ , ‘Age’ : 18}

To delete the element in the dictionary

>>> del data[‘Age’]

{‘Name’ : ‘Chandru’ , ‘Gender’ : ‘Male’ }

To check whether the given element is present or not

>>> Arun in data

False

To combine two sets, the keyword dict is used

>>> dict(((‘India’,’America’) , (‘Chennai’,’Losangels’)))

{‘India’ : ‘Chennai’ , ‘America’ : ‘Losangels’}

To append the values in dictionary

>>> data = {}

>>> data.setdefault(‘Name’, []).append(‘Chandru’)

>>> data

{‘Name’ : [‘Chandru’]}

To print the values in dictionary

>>> for x,y in data.items():

… print(“%s is %s” % (x,y)

Name is Chandru

Gender is Male

Trip to Bessy Beach

Today Myself and my friends went to Besent nagar beach to chill ourselves. But the things were happened opposite to what we thought. The rain interrupted our journey, but we faced it and again started the journey. Then my friend hit the bike in post and the gear box were broken. After then we went to Mechanic shop and fixed it. After facing all this problems finally we caught big. That is we caught by the police for not wearing the helmet. He asked us to pay hundred rupees and left us. This day we thought to enjoy but it became opposite. Obviously this day added to the worst day list.

Data structures in Python

List

There are some inbuilt functions which makes the job easier. Using list we can modify the data in any order.

a.append(“Value”) is the general syntax which is used to add value in the list at the last position.For example we shall create a list,

>>>a = [1,2,3,4,5]

When we give – a.append(6), it will show the output as

a = [1,2,3,4,5,6]

To remove a element in list at last and to return its value, a.pop() command is used. When a.pop() given,it will show the output as

6

To remove the first element in the list, a.pop(0) is given,

1

To insert the element in list, a.insert(“Position”, “Value”). When a.insert(0,5) is given,

>>>[5,2,3,4,5]

To delete the element in the list, a.remove(“Key”) is used. a.remove(3) will give the output as

[5,2,4,5]

To reverse the element, a.reverse() is used.

[5,4,2,5]

To count the number of element in the list, a.count() is used.

4

In case if we want to join two lists, then the keyword extend is used. The syntax is a.extend(b)

>>>b = [6,7,8]

>>>a.extend(b)

[5,4,2,5,6,7,8]

Introduction to Set

Set is used when we have several duplicate values. I removes the duplicate value and displays only the original value. We can also perform some mathematical operations. Set can be created by using a keyword set.

>>>a = set(“abcdabcdef”)

{‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’}

>>>a

To perform mathematical operation,

>>>a = set(“abcd”)

>>>b = set(“cdef”(

>>>a – b

{‘a’ , ‘b’}

>>>a | b

{‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’}

>>>a & b

{‘c’, ‘d’}

To add a element in the set, add keyword is used.

>>>a.add(‘g’)

{‘a’, ‘b’, ‘c’, ‘d’, ‘g}

For Loop

For loop is used when a loop has to be several number of times.

a = [ ‘ He ‘ , ‘ loves ‘ , ‘ Python ‘ ]

for i in a:

print(i)

When we execute this program, it will give the output as :-

He
loves
python.

Also we can have a range in for loop in which the loop has to be executed for number of times.

Range

Range is used to set a bound for which the loop has to be executed.

for i in range(1, 4):

print(i)

The output will be

1

2

3

4

Continue

Continue statement is used when the loop has to be executed from the first.

while true:

a = int(input(” Enter the number “))

if a < 0:

continue

elif a = 0:

break

else:

print(” The square is %d ” % (n * n))

Thus when the number which we enter is less then zero, then the loop will be again executed from the first. The break statement is used to come out of the loop.

Else in For

We can also use else statement in for loop.

for i in range(1,5):

print(i, end=’ ‘)

else:

print(” Out of Range “)

The output of this program will be

1 2 3 4 5
Out of Range.