Chapter 4: Python Objects
INSIGHT
1. The 𝑝𝑟𝑖𝑛𝑡() function is used to print the message or a value of a variable.
2. The 𝑝𝑟𝑖𝑛𝑡() function has two parameters- sep & end.
3. The 𝑖𝑛𝑝𝑢𝑡() function is used to accept a value form user. By default, it accepts string value
4. We can convert the string value taken using 𝑖𝑛𝑝𝑢𝑡() function in other data types using 𝑖𝑛𝑡() or
𝑓𝑙𝑜𝑎𝑡() functions.
5. The symbols that are used to perform calculations on variables or constants are called operators.
6. Types of operators:
a. Arithmetic operators: +, −,∗,/,//, %,∗∗
b. Relational operators: >, <, >=, <=, ==, ! =
c. Logical operators: 𝑎𝑛𝑑, 𝑜𝑟, 𝑛𝑜𝑡
d. Augmented Assignment operators: +=, −=,∗=,/=,//=, % =,∗∗=
e. Membership operator: 𝑖𝑛, 𝑛𝑜𝑡 𝑖𝑛
f. Identity operators: 𝑖𝑠, 𝑖𝑠 𝑛𝑜𝑡
7. Conditional statement checks the condition and executes the statements accordingly. These are also
called decision-making statements.
8. Types of conditional statements are:
if statement if…..else statement if…..elif….else statement
syntax: syntax: syntax:
𝑖𝑓 < 𝑐𝑜𝑛𝑑 𝑖𝑠 𝑡𝑟𝑢𝑒 >: 𝑖𝑓 < 𝑐𝑜𝑛𝑑 𝑖𝑠 𝑡𝑟𝑢𝑒 >: 𝑖𝑓 < 𝑐𝑜𝑛𝑑1 𝑖𝑠 𝑡𝑟𝑢𝑒 >:
… . 𝑆𝑡𝑎𝑡𝑒𝑚𝑒𝑛𝑡𝑠 …. … . 𝑆𝑡𝑎𝑡𝑒𝑚𝑒𝑛𝑡𝑠 … . … . 𝑠𝑡𝑎𝑡𝑒𝑚𝑒𝑛𝑡𝑠 … .
𝑒𝑙𝑠𝑒: 𝑒𝑙𝑖𝑓 < 𝑐𝑜𝑛𝑑2 𝑖𝑠 𝑡𝑟𝑢𝑒 >:
… . 𝑆𝑡𝑎𝑡𝑒𝑚𝑒𝑛𝑡𝑠 …. … . 𝑠𝑡𝑎𝑡𝑒𝑚𝑒𝑛𝑡𝑠 … .
𝑒𝑙𝑠𝑒:
… . 𝑠𝑡𝑎𝑡𝑒𝑚𝑒𝑛𝑡𝑠 ….
9. Python is an object-oriented programming language. It is case-sensitive language.
10. An object is simply a collection of data (variables) and methods (functions) that act on those data.
Example of python objects: list, tuple, dictionary, variable etc.
11. We do not have to tell explicitly the data types. Python object takes the data type based on the data it
stores.
12. Python data types:
a. Numeric
i. Integer
ii. Float
iii. Complex
b. Iterables
i. String
ii. List
iii. Tuple
iv. Dictionary
c. Other
i. Boolean
ii. None
13. Integer data type stores integer values. Ex: >>> 𝑋 = 10
14. Float data type stores numbers with decimal. Ex: >>> 𝑋 = 25.30
15. Complex is a number which has a real and an imaginary part. Ex: >>> 𝑋 = 8 + 6𝑗
16. To know the data type of the Python object, we use 𝑡𝑦𝑝𝑒() function.
17. Iterables are those objects in Python that store more than one value under a single name. ex: string,
list, tuple, dictionary.
18. The objects that allow us to change individual elements in that object are called mutable objects. Ex:
list and dictionary
19. The objects that do not allow us to change individual elements in that object are called immutable
objects. Ex: string and tuple
20. Each element of an object (except dictionary) can be accessed using their index. Index can be
negative and positive. Elements of a dictionary can be accessed suing its key.
21. To know the total number of elements in an object, 𝑙𝑒𝑛() function is used.
22. A string can be added to another string using the ‘+’ operator. This is called concatenation.
A string can be repeated by multiplying it with a number using the ‘*’ operator. This is called
replication.
23. Strings are enclosed within double or single quotes.
Lists are enclosed in square brackets.
Tuples are enclosed in parentheses.
Dictionary is enclosed in curly brackets.
24. Ada Lovelace has been called the ‘World’s first computer programmer’.
25. To add a new element at the end of a list, 𝑎𝑝𝑝𝑒𝑛𝑑() function is used.
26. To remove an element from a list, 𝑝𝑜𝑝() function is used. This function takes index number as
arguments. If index is not given then it will remove the
last element.
27. To create a tuple
with a single value, we must put a comma after the value. Otherwise,
the value is taken as integer, not a tuple.
28. A dictionary is an object that stores values in the form of key:value
pair. Each pair is called an item.
It can be declared as:
Item
Name of the dictionary Key Value
29. Keys in a dictionary must be unique. Keys can be string, number or tuple. A list cannot be used as a
key.
30. Boolean data type has only two values, ‘𝑇𝑟𝑢𝑒’ and ‘𝐹𝑎𝑙𝑠𝑒’. They both have the first letter capital.
31. Slicing allows you to extract some specific number of elements from a given iterable (list, tuple,
string) and display the result. It has three parameters: start, end and stop.
Syntax: 𝑖𝑡𝑒𝑟𝑎𝑏𝑙𝑒 [𝑠𝑡𝑎𝑟𝑡 ∶ 𝑒𝑛𝑑 ∶ 𝑠𝑡𝑒𝑝]
32. Start: Starting index of the extraction. Default is index 0.
End (optional): Ending index of the extraction. Default is length (total number of elements) of the
iterable. Values are printed till End-1.
Step (optional): Step value to the next value printed. Default is 1.