Chapter 1 Review of Python Basics
Chapter 1 Review of Python Basics
By-
Neha Tyagi
PGT CS
KV 5 Jaipur II Shift
ScriptMode
Interactive
Mode
Keywords
Keywords are those words which provides
a special meaning to interpreter.
These are reserved for specific functioning.
These can not be used as identifiers,
variable name or any other purpose.
Available keywords in Python are-
Graphical View
CORE
DATA TYPE
Floating
Integer Complex String Tuple List Dictionary
Point
Boolean
Reference Object
variable
• Mutable (Changeable)
– lists, dictionaries and sets.
• Immutable (Non-Changeable)
– integers, floats, Booleans, strings and tuples.
• In above given expression, first 7*8 will be calculated as 56, then 56 will
be divided by 5 and will result into 11.2, then 11.2 again divided by 2
and will result into 5.0.
*Only in case of **, associativity will be followed from right-to-left.
» Empty Statements
• pass
» Simple Statements (Single Statement)
• name=input (“Enter your Name “)
• print(name) etc.
» Compound Statements
• <Compound Statement Header>:
<Indented Body containing multiple simple
statements/compound statements>
• Here, Header line starts with the keyword and ends at colon (:).
• The body consists of more than one simple Python statements or
compound statements.
Neha Tyagi, KV 5 Jaipur II Shift
Statement Flow Control
• In a program, statements executes in sequential
manner or in selective manner or in iterative
manner.
Sequential Selective Iterative
Output Output
– not in operator-
5 not in [1,2,3,4] will return True.
OUTPUT
2. By taking Input
Output
word = “RESPONSIBILITY”
• Long lists-
even = [0, 2, 4, 6, 8, 10 ,12 ,14 ,16 ,18 ,20 ] This is a Tuple
• Nested list -
L = [ 3, 4, [ 5, 6 ], 7]
Another method
eval ( ) function identifies type of the passed string and then return it.
Another example
String Values
Important 1:membership
operator (in, not in) works
in list similarly as they work
in other sequence.
Important 2: + operator
adds a list at the end of
other list whereas *
operator repeats a list.
Value didn’t
change in string.
Error shown. Value got changed
in list specifying
list is mutable
List.extend(<list>) Append the list (passed in the form of argument) at the end of list
with which function is called.
List.pop(<index>) Delete and return the element of passed index. Index passing is
optional, if not passed, element from last will be deleted.
List.remove(<value>) It will delete the first occurrence of passed value but does not
return the deleted value.
List.clear ( ) It will delete all values of list and gives an empty list.
List.count (<item>) It will count and return number of occurrences of the passed element.
List.reverse ( ) It will reverse the list and it does not create a new list.
List.sort ( ) It will sort the list in ascending order. To sort the list in descending
order, we need to write----- list.sort(reverse =True).
• Long tuple:
• Nested tuple:
• Membership operator:
• Working of membership operator “in” and “not in” is same as
in a list. (for details see the chapter- list manipulation).
• Concatenation and Replication operators:
• + operator adds second tuple at the end of first tuple. * operator repeats
elements of tuple.
Neha Tyagi, KV 5 Jaipur II Shift
Accessing a Tuple
• Accessing Individual elements-
• Traversal of a Tuple –
for <item> in <tuple>:
#to process every element.
OUTPUT
• Tuple Replication-
teachers={“Rajeev”:”Math”, “APA”:”Physics”,”APS”:”Chemistry:”SB”:”CS”}
<dictionary>[<key>] = <value>
By passing List
Nesting in Dictionary
look at the following example carefully in which element of a dictionary is
a dictionary itself.
WAP to create a dictionary containing names of employee as key and their salary as
value. Output
False
json.dumps(<>,indent=<n>)
Here a dictionary is
created of words
and their frequency.
In the above given example, you can see that change is done in
the values of similar keys whereas dissimilar keys got joined with
their values.