Mooc Seminar: Name Rajat Kushwaha St. Id 200211241 Section I'
Mooc Seminar: Name Rajat Kushwaha St. Id 200211241 Section I'
NameRAJAT KUSHWAHA
St. Id200211241
Section ‘I’
2. Strings
Immutable
Conceptually very much like a tuple
Regular strings use 8-bit characters. Unicode
strings use 2-byte characters. (All this
is changed in Python 3.)
3. List
Mutable ordered sequence of items of mixed types
The ‘in’ Operator
Boolean test whether a value is inside a collection
(often
called a container in Python:
>>> t = [1, 2, 4, 5]
>>> 3 in t
False
>>> 4 in t
True
>>> 4 not
in t
False
For strings,
tests for
substrings
>>> a =
'abcde'
>>> 'c' in a
True
>>> 'cd' in
a
True
>>> 'ac' in a
False
The + Operator
The + operator produces a new tuple, list, or string
whose
value is the concatenation of its arguments.
Extends concatenation from strings to other types
>>> (1, 2, 3) + (4, 5, 6)
(1, 2, 3, 4, 5, 6)
To convert between tuples and lists use the list() and tuple()
functions:
li = list(tu)
tu = tuple(li)
Dictionaries: a mapping collection type
Dictionaries: Like maps in Java
Dictionaries store a mapping between a set of keys
and a set of values.
Keys can be any immutable type.
Values can be any type
Values and keys can be of different types in a single dictionary
You can
define
modify
view
lookup
delete
the key-value pairs in the dictionary.
Creating and accessing dictionaries
>>> d[‘user’]
‘bozo’
>>> d[‘pswd’]
1234
>>> d[‘bozo’]
Traceback
(innermost
last):
File
‘<interacti
ve input>’
line 1,
in ?
Updating Dictionaries
>>> d = {‘user’:‘bozo’, ‘pswd’:1234}
>>> d[‘id’] = 45
>>> d
{‘user’:‘clown’, ‘id’:45, ‘pswd’:1234}
Note:
Use of indentation for blocks
Colon (:) after boolean expression
while Loops (as expected)
>>> x = 3
>>> while x < 5:
print x, "still in the loop"
x = x + 1
3 still in the loop
4 still in the loop
>>> x = 6
>>> while x < 5:
print x, "still in the loop"
>>>
break and continue
You can use the keyword break inside a loop to
leave the while loop entirely.
assert(number_of_players < 5)
for x in range(5):
print x
(Methods later)
Defining Functions
Function definition begins with def Function name and its arguments.
def get_final_answer(filename):
"""Documentation String"""
line1
line2 Colon.
return total_counter
...