Unit 3
Unit 3
Unit 3
9. print("Sum = ",sum(a,b))
Types of arguments
Output: Enter a: 10 There may be several types of arguments which can be passed at
Enter b: 20 the time of function call.
Sum = 30
1. Required arguments
The sum is 60
Value of sum outside the function: 0
Python String
Python string is the collection of the characters surrounded by
single quotes, double quotes, or triple quotes. The computer does
not understand the characters; internally, it stores manipulated
character as the combination of the 0's and 1's.
In Python, strings can be created by enclosing the character or the
sequence of characters in the quotes. Python allows us to use
single quotes, double quotes, or triple quotes to create the string.
Consider the following example in Python to create a string.
Syntax:
1. str = "Hi Python !"
Here, if we check the type of the variable str using a Python
script
1. print(type(str)), then it will print a string (str).
Consider the following example:
Creating String in Python 1. str = "HELLO"
We can create a string by enclosing the characters in single-
quotes or double- quotes. Python also provides triple-quotes to2. print(str[0])
represent the string, but it is generally used for multiline string3. print(str[1])
or docstrings.
4. print(str[2])
1. #Using single quotes
5. print(str[3])
2. str1 = 'Hello Python'
6. print(str[4])
3. print(str1)
7. # It returns the IndexError because 6th index doesn't exist
4. #Using double quotes
8. print(str[6])
5. str2 = "Hello Python" Output:
6. print(str2) H
E
7. #Using triple quotes L
L
8. str3 = '''''Triple quotes are generally used for
O
9. represent the multiline or IndexError: string index out of range
Output:
HELLO to print the actual meaning of escape
hello
characters such as "C://python". To define
Deleting the String any string as a raw string, the character r or
As we know that strings are immutable. We cannot delete or
remove the characters from the string. But we can delete the R is followed by the string.
entire string using the del keyword.
1. str = "JAVATPOINT" % It is used to perform string formatting. It
2. del str[1] makes use of the format specifiers used in C
Output:
TypeError: 'str' object doesn't support item deletion programming like %d or %f to map their
[] It is known as slice operator. It is used to 8. print('wo' not in str1) # prints false as wo is present in str1.
Sequence Type
String
1. a = 5 5. print(s)
Output:
2. print("The type of a", type(a)) string using double quotes
3. b = 40.5 A multiline
string
4. print("The type of b", type(b)) Consider the following example of string handling.
Example - 2
5. c = 1+3j
1. str1 = 'hello javatpoint' #string str1
6. print("The type of c", type(c))
2. str2 = ' how are you' #string str2
7. print(" c is a complex number", isinstance(1+3j,complex))
Output: 3. print (str1[0:2]) #printing first two character using slice operator
The type of a <class 'int'>
The type of b <class 'float'>
The type of c <class 'complex'> 4. print (str1[4]) #printing 4th character of the string
Set
Python Set is the unordered collection of the data type. It is Built-In Functions
iterable, mutable(can modify after creation), and has unique
elements. In set, the order of the elements is undefined; it may The Python interpreter supports many functions
return the changed sequence of the element. The set is created by that are built-in: sixty-eight,
using a built-in function set(), or a sequence of elements is passed Math
in the curly braces and separated by the comma. It can contain
Function Description
various types of values. Consider the following example.
abs() Returns absolute value of a number
1. # Creating Empty set
divmod() Returns quotient and remainder of integer division
2. set1 = set() max() Returns the largest of the given arguments
3. set2 = {'James', 2, 3,'Python'} or items in an iterable
4. #Printing Set value min() Returns the smallest of the given arguments
5. print(set2)
or items in an iterable
pow() Raises a number to a power
6. # Adding element to the set
round() Rounds a floating-point value
7. set2.add(10)
sum() Sums the items of an iterable
8. print(set2)
9. #Removing element from the set
Type Conversion
10. set2.remove(2) Function Description
11. print(set2) ascii() Returns a string containing a printable
Output: representation of an object
{3, 'Python', 'James', 2}
{'Python', 'James', 3, 2, 10} bin() Converts an integer to a binary string
{'Python', 'James', 3, 10} bool() Converts an argument to a Boolean value
chr() Returns string representation of character given
by integer argument
Python Lambda Functions complex()
Python Lambda function is known as the anonymous function Returns a complex number constructed from
that is defined without a name. Python allows us to not declare arguments
the function in the standard manner, i.e., by using float() Returns a floating-point object constructed
the def keyword. Rather, the anonymous functions are declared from a number or string
by using the lambda keyword. However, Lambda functions can
Function Description
hex() Converts an integer to a hexadecimal string Classes, Attributes, and Inheritance
int() Returns an integer object constructed from a Function Description
number or string classmethod() Returns a class method for a function
oct() Converts an integer to an octal string delattr() Deletes an attribute from an object
ord() Returns integer representation of a character getattr() Returns the value of a named attribute of an
repr() Returns a string containing a printable object
representation of an object hasattr() Returns True if an object has a given
str() Returns a string version of an object attribute
type() Returns the type of an object or creates a new isinstance() Determines whether an object is an instance
type object of a given class
issubclass() Determines whether a class is a subclass of
Iterables and Iterators a given class
Function Description property() Returns a property value of a class
all() Returns True if all elements of an iterable are setattr() Sets the value of a named attribute of an
true object
any() Returns True if any elements of an iterable are super() Returns a proxy object that delegates
true method calls to a parent or sibling class
enumerate() Returns a list of tuples containing indices and
values from an iterable Input/Output
filter() Filters elements from an iterable Function Description
iter() Returns an iterator object format() Converts a value to a formatted representation
len() Returns the length of an object
map() Applies a function to every item of an iterable input() Reads input from the console
next() Retrieves the next item from an iterator open() Opens a file and returns a file object
range() Generates a range of integer values print() Prints to a text stream or the console
reversed() Returns a reverse iterator
slice() Returns a slice object Variables, References, and Scope
sorted() Returns a sorted list from an iterable Function Description
zip() Creates an iterator that aggregates elements dir() Returns a list of names in current local scope
from iterables or a list of object attributes
Composite Data Type globals() Returns a dictionary representing the current
Function Description global symbol table
id() Returns the identity of an object
bytearray() Creates and returns an object of
the bytearray class locals() Updates and returns a dictionary representing
bytes() Creates and returns a bytes object (similar current local symbol table
to bytearray, but immutable) vars() Returns __dict__ attribute for a module, class,
dict() Creates a dict object or object
frozenset() Creates a frozenset object
list() Creates a list object Miscellaneous
object() Creates a new featureless object Function Description
set() Creates a set object callable() Returns True if object appears callable
tuple() Creates a tuple object compile() Compiles source into a code or AST object
1
Using lambda function with map()
The map() function in Python accepts a function and a list. It >>> b
gives a new list which contains all modified items returned by the
function for each item. 2
Consider the following example of map() function.
1. #program to filter out the list which contains odd nu >>> c
mbers
4
2. lst = (10,20,30,40,50,60)
3. square_list = list(map(lambda x:x**2,lst)) # the tupl When we put tuples on both sides of an assignment
e contains all the items of the list for which the lamb operator, a tuple unpacking operation takes place. The
values on the right are assigned to the variables on the left
da function evaluates to true according to their relative position in each tuple. As you
4. print(square_tuple) can see in the above example, a will be 1, b will be 2, and
5. Output: c will be 4.
6. (100, 400, 900, 1600, 2500, 3600)