FPP - Unit - 02 (Tuples... )
FPP - Unit - 02 (Tuples... )
Tuple is exactly same as List except that it is immutable. i.e. once we create
Tuple object, we cannot perform any changes in that object.
Hence Tuple is Read Only Version of List.
Duplicates are allowed.
Heterogeneous objects are allowed.
Items in a tuple are immutable.
We can preserve insertion order and we can differentiate duplicate objects by
using index. Hence index will play very important role in Tuple also.
Syntax – tuple_name = (item1, item2, item3….)
Creation of a tuple:
To resolve this problem, we can use ‘*’ symbol in front of any variable.
E.g. – t = (10,20,30,40)
a, b, *c = t # 30 and 40 both values will be assigned to variable c.
Dictionary:
We can use List, Tuple and Set to represent a group of individual objects as a
single entity.
If we want to represent a group of objects as key-value pairs then we should
go for Dictionary.
E.g.: roll no----name
phone number--address
Ip address---domain name
Duplicate keys are not allowed but values can be duplicated.
Heterogeneous objects are allowed for both key and values.
Insertion order is not preserved
Dictionaries are mutable
Dictionaries are dynamic in size.
Indexing and slicing concepts are not applicable
Creating a dictionary:
d= {} or d=dict ()
we can create empty dictionary. We can add entries as follows
d [100] ="Python"
d [200] ="Java"
d [300] ="C"
print(d) # {100: 'Python', 200: ‘Java', 300: 'C’}
d[key]=value
--If the key is not available then a new entry will be added to the dictionary
with the specified key-value pair.
--If the key is already available then old value will be replaced with new
value.
E.g.:
d={100:"python",200:"java",300:"c"}
print(d)
d [400] ="c++"
print(d) # {100:"python",200:"java",300:"c",400:” c++”}
d [100] ="sunny"
print(d)
del d[key]
It deletes entry associated with the specified key. If the key is not available
then we will get Key Error.
E.g.: d={100:"durga",200:"ravi",300:"shiva"}
print(d)
del d [100]
print(d)
del d [400]
d. clear () - To remove all entries from the dictionary
del d - To delete total dictionary.
Important functions of dictionary:
len (), clear (), get (), pop (), popitem (), copy (),
get () - To get the value associated with the key
d. get (key). If the key is available then returns the corresponding value
otherwise returns None. It won’t raise any error.
pop () - d. pop(key)
It removes the entry associated with the specified key and returns the
corresponding value. If the specified key is not available then we will
get Key Error.
popitem (): It removes an arbitrary item(key-value) from the dictionary
and returns it.
syntax – dict_name. popitem ()
keys (): It returns all keys associated with dictionary
values (): It returns all values associated with the dictionary
update (): d. update (x) All items present in the dictionary x will be
added to dictionary d.
Type casting: -
In type casting also known as type coercion, the programmer has to change
the data type as per their requirement manually. In this, the programmer
explicitly converts the data type using predefined functions like int (), float (),
str (), etc. There is a chance of data loss in this case if a particular data type is
converted to another data type of a smaller size.
The in-built Python Functions used for the conversion are given below, along
with their descriptions:
Function Description
int (x, base) Converts any data type x to an integer with the mentioned base
float (x) Converts x data type to a floating-point number
complex (real, imag) converts a real number to a complex number
str (x) converts x data type to a string
tuple (x) converts x data type to a tuple
list (x) converts x data type to a list
set (x) converts x data type to a set
dict (x) creates a dictionary and x data type should be a sequence of (key,
value) tuples
ord (x) converts a character x into an integer
hex (x) converts an integer x to a hexadecimal string
oct (x) converts an integer x to an octal string
chr (x) converts a number x to its corresponding ASCII (American
Standard Code for Information Interchange) character
1. int (x, base) - We can use this function to convert values from other types
to int.
E.g. int (10.55) #10
int (True) #1
int (“12”) #12
int (’10.5’) #Value Error
int (‘ten’) #Value Error
Note:
1. We can convert from any type to int except complex type.
2. If we want to convert str type to int type, compulsory str should contain
only integral value and should be specified in base-10.
2. float (): We can use float () function to convert other type values to
float type.
Note:
1. We can convert any type value to float type except complex type.
2. Whenever we are trying to convert str type to float type compulsory str
should be either integral or floating-point literal and should be specified only
in base-10.
4. str (): We can use this method to convert other type values to str type.
Operators:
1. Arithmetic Operators:
+ ==>Addition
- ==>Subtraction
* ==>Multiplication
/ ==>Division operator
% ===>Modulo operator
// ==>Floor Division operator
** ==>Exponent operator or power operator
Note: We can use +, * operators for str type also. If we want to use + operator
for str type then compulsory both arguments should be str type only
otherwise we will get error.
If we use * operator for str type then compulsory one argument should be int
and other argument should be str type.
Note: For any number x, x/0 and x%0 always raises "ZeroDivisionError".
2. Relational Operators:
>,>=,<,<=
3. Logical Operators:
and, or, not
4. Bitwise Operators:
&,|,^,~,<<,>>
We can apply these operators bitwise. These operators are applicable only for
int and Boolean type.
& ==> If both bits are 1 then only result is 1 otherwise result is 0
| ==> If at least one bit is 1 then result is 1 otherwise result is 0
^ ==>If bits are different then only result is 1 otherwise result is 0
~ ==>bitwise complement operator 1==>0 & 0==>1
<< ==>Bitwise Left shift
>> ==>Bitwise Right Shift
5. Assignment Operator:
6. Ternary Operator:
Syntax: x = first Value if condition else second Value
If condition is True then first Value will be considered else second Value will
be considered.
E.g. min=a if a<b else b
7. Special operators:
Note: We can use is operator for address comparison whereas == operator for
content comparison.
Operators Precedence:
If multiple operators present then which operator will be evaluated first is decided by
operator precedence.
() Parenthesis
** exponential operator
~, - Bitwise complement operator, unary minus operator
*, /, %, // multiplication, division, modulo, floor division
+, - addition, subtraction
<> Left and Right Shift & bitwise And
^ Bitwise X-OR
| Bitwise OR
>,>=, <, <=, ==, != ==>Relational or Comparison operators
=, +=, -=, *=... ==>Assignment operators
is, is not Identity Operators
in, not in Membership operators
not Logical not
and Logical and
or Logical or
Mathematical Functions: