Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Unit 3 Notes Python

Uploaded by

jainsajal519
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Unit 3 Notes Python

Uploaded by

jainsajal519
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Smart Engineer Babu

Python Programming
Unit-3(Func ons and Strings)
Full Explana on

Topic-1: Func on and It’s advantages.

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

Topic-2: Execu on of func on.

Explana on:
Step-1: When func on without “return”

Step-2: When we retrieve the output using “return” command

Step-3: When func on is treated as an object


a. Func ons in Python are themselves an object, and an object has some value.
b. When we run the command “print square” it returns the value of the object.
c. Since we have not passed any argument, we do not have any specific func on to run
hence it returns a default value (0x021B2D30) which is the loca on of the object.
Topic-3: Keyword and default arguments.

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.

Topic-4: Scope rules in Python.

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.

len() func on-


1. len () is a built-in func on in Python. When used with a string, len returns the length or
the number of characters in the string.
2. Blank symbol and special characters are considered in the length of the string.

Concatena on opera on-


1. Concatena ng opera on is used in-order to add two strings end-to-end.

Repeat / replicate opera on-


1. Lists can be replicated or repeated or repeatedly concatenated with the asterisk (*)
operator.
Slicing-
1. Slicing in a string means to remove some part of a string
and store it in another variable.
2. A piece or subset of a string is known as slice.
3. It is like ge ng a piece sliced out of a full pizza.

4. Similarly, we can slice a string with some step.

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.

3. We can create tuples in python in following ways-


a. A tuple with integer data items:
>>> tuple = (4, 2, 9, 1)
b. A tuple with items of different data types:
>>>tuple_mix = (2, 30, “Python”, 5.8, “Program”)
c. Nested tuple:
>>>nested_tuple = (“Python”, [1, 4, 2], [“john”, 3.9])
d. Tuple can also be created without parenthesis:
>>>tuple = 4.9, 6, ‘house’
>>>print tuple
(4.9, 6, ‘house’) # Output
Accessing values in tuples:
1. The value of Tuples can be accessed in following way – [index].

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.

Basic opera ons of tuples are:


1. Concatena on: It is used for merging two or more tuples.

2. Repe on: The repe on operator repeats the tuples a given number of mes.
Repe on is performed by the * operator in Python.

3. In operator: In operator is used to search an element in a tuple. In operator returns


result in Boolean form, that means-
If element exists - returns True,
If element does not exist – returns False
4. Itera on: It referred as traversing the tuples in loop.

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]

7. Methods of dele ng element from a list are following.


a. pop operator – We can remove any element from the list by knowing the index of
that element.

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.

Built-in list operators are as follows: Similar to tuples


Topic-8: Mutable sequences.

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.

Topic-9: list comprehension.

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.

Topic-10: Sets and Opera ons performed on sets.

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

Crea on of dic onary-


1. Crea ng a dic onary is simple in Python, there are three ways related to dic onary
crea on.

Opera ons performed in dic onary-


1. Traversing –
a. Traversing in dic onary is done on the basis of keys.
b. For traversing, for loop is used, which iterates over the keys in the dic onary and
prints the corresponding values using keys.

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 –

Topic-12: Lambda expression.

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

You might also like