Unit 3 Notes Python
Unit 3 Notes Python
Python Programming
Unit-3(Func ons and Strings)
Full Explana on
Explana on:
1. Everyone knows there are several func ons in mobile.
2. Func ons are tools that help us in our general life.
3. Similarly in programming Func ons are tools that make
programming easy.
4. Once a func on is created by the programmer for a
specific task, this func on can be called any me to
perform that task.
5. Func ons are of two types.
Pre-Defined Func ons (पहले से बने हए)
User-Defined Func ons (यजू र ारा बननाए हए)
6. It is similar to dividing a big task among various persons.
Advantages:
1. They reduce duplica on of code in a program.
2. They break the large complex problems into small parts.
3. They help in improving the clarity of code (i.e., make the code easy to understand).
4. A piece of code can be reused as many mes as we want with the help of func ons.
How to define a func on – Func on is defined by “def” keyword following by func on name
and parentheses.
Syntax of func on defini on- def func on_name ( ) :
Syntax of Func on Call- func on_name ( )
For example-
Important things for func ons-
1. Keyword - def
2. Func on name - same as iden fiers
3. : to mark the end of func on
4. Arguments – These are the values passed to func ons between parenthesis
5. Body of the func on – It is the place where instruc ons of func ons are placed
6. return value
7. Func on call – Here func on is called to start execu on
Explana on:
Step-1: When func on without “return”
Explana on:
Keyword Arguments-
1. Keyword arguments are related to the func on calls.
2. When we use keyword arguments in a func on call, the caller iden fies the arguments
by the parameter name.
3. This allows us 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.
Default arguments-
1. A default argument is an argument that assumes a default value if a value is not
provided in the func on call for that argument.
Explana on:
1. Everybody might heard of Scope of future.
2. Or your “Rishtedaar” asked to you that what is the scope of this profession.
3. In python scope is taken as a working area of any variable.
4. Two variables can have the same name only if they are declared in separate scopes.
5. A variable cannot be used outside its scopes.
6. There is a diagram that represents the Scope rules in python-
Topic-5: Strings, len() func on, Concatena on, Repeat, Slicing and Indexing.
Explana on:
1. String is a collec on of various characters arranged in a sequence.
2. Your name is a string- “SmartEngineerBabu”.
3. Strings, in Python, can be used as a single data type, or, alterna vely, can be accessed in
parts. This makes strings really useful and easier to handle in Python.
Indexing-
1. Indexing means referring to an element by its posi on.
2. apka_naam = ”Please Subscribe”
3. apka_naam = [‘P’,’l’,’e’,’a’,’s’,’e’,’ ‘,’S’,’u’,’b’,’s’,’c’,’r’,’i’,’b’,’e’]
4. In Python and in most of the programming languages indexing starts from [0].
Topic-6: Tuple.
Explana on:
1. Tuples are the sequence or series values of different types separated by commas (,).
2. Values in tuples can also be accessed by their index values, which are integers star ng
from 0.
2. We can also use slicing in order to print the con nuous values in a tuple.
3. Tuples can also be returned by the func on as return values of a func on.
2. Repe on: The repe on operator repeats the tuples a given number of mes.
Repe on is performed by the * operator in Python.
Topic-7: List
Explana on:
1. A list is also a series of values in Python.
2. In a list, all the values are of any type.
3. The values in a list are called elements or items.
4. A list is a collec on of items or elements.
5. The sequence of data in a list is ordered and can be accessed by their posi ons, i.e.,
indices.
For example:
>>> list = [1, 2, 3, 4]
>>> list [1]
2 # Output
>>> list [3]
4 # Output
6. Method of copying one list into another. There are two ways to make copy of a list.
a. Using [:] Operator:
b. Using built-in func on: Python has a built-in copy func on which can be used to
make copy of an exis ng list.
For Example:
>>> from copy import copy # Import library
>>> list_original = [1, 2, 3, 4]
>>> list_copy = copy(list_original) # Copying list
>>> print list_copy
[1, 2, 3, 4]
b. del operator - The del operator deletes the value on the provided index.
c. Remove operator –
i. We can remove the item using remove operator by value of that element.
ii. In order to delete more than one value from a list, del operator with slicing is
used.
Explana on:
1. Python represents all its data as objects. Mutability of object is determined by its type.
2. Some objects are mutable and some are immutable.
3. Mutable means whose value can be updated and immutable means whose value can not
be updated.
4. Mutable Objects - lists and dic onaries
5. Immutable Objects - integers, floats, strings and tuples
Example:
Dic onaries are mutable in Python: Dic onaries in Python are mutable that means the values
in a dic onary can be changed, added or deleted.
Strings are immutable: String are immutable which means that we cannot change any element
of a string.
Lists are mutable: Lists are mutable means that the value of any element inside the list can be
changed at any point of me with the help of index number.
Explana on:
1. List comprehension is used to create a new list from exis ng sequences.
2. It is a tool for transforming a given list into another list.
3. Using list comprehension, we can replace the loop with a single expression that
produces the same result.
4. This can be done using set builder nota ons of mathema cs.
Explana on:
1. Set is an unordered collec on of data known as set.
2. Set contains unique values.
3. It is similar to the set of mathema cs.
4. Some opera ons performed in Set are following-
a. Union- Union opera on performed on two sets returns all the elements from both
the sets. It is performed by using and operator.
b. Intersec on- Intersec on opera on performed on two sets returns all the element
which are common or in both the sets. It is performed by using ‘|’ operator.
c. Difference- Difference opera on performed on two sets set1 and set2 returns the
elements which are present on set1 but not in set2. It is performed by using ‘–’
operator.
d. Symmetric difference- Symmetric difference opera on performed on two sets
returns the element which are present in either set1 or set2 but not in both. It is
performed by using ^ operator.
Topic-11: Dic onary, Crea on, opera ons performed on dic onary and built in
dic onary methods.
Explana on:
1. The Python dic onary is an unordered collec on
of items or elements.
2. In dic onary, we store values in form of key and
Values.
3. The mapping of key and value is called key-value
pairing.
4. Syntax: Key: Value
2. Membership –
a. With the help of membership operator we can search that whether the key is present
in dic onary or not.
Built-in methods used in dic onary –
Explana on:
1. Lambda expressions is used to create the anonymous func on.
2. The anonymous func ons are the func ons created using a lambda keyword.
3. They are not defined by using def keyword. For this reason, they are called anonymous
func ons.
4. We can pass any number of arguments to a lambda form func ons, but s ll they return
only one value in the form of expression.
5. An anonymous func on cannot directly call print command as the lambda needs an
expression.
6. An anonymous func on is a single line statement func on.
7. Syntax: lambda [arg1 [,arg2, ......., argn]] : expression
The syntax of the lambda func on is a single statement
For example:
# func on defini on here
>>>mul = lambda val1, val2: val1*val2;
# func on call here
>>> print “value:”, mul (20,40)
Value: 800 # Output