Chapter 1 and 2 Cs
Chapter 1 and 2 Cs
TOUR 1 & 2
DYNAMIC TYPING STATIC TYPING
print() input()
Used to give output Used to get input
print ("Hello") a = input("Enter data: ")
2
LIST STRING
A collection of ordered, mutable A sequence of characters enclosed in
elements in Python. quotes.
Can store multiple data types and
Immutable (cannot be changed once
allows modifications (add, remove,
created).
update).
[1,2,3,4,5] "Hello world"
3
Mutable Datatype Immutable Datatype
The value stored in the variable can The value stored in the variable cannot
be altered without changing the be altered without changing the
memory address of memory address of
the variable the variable
list, dictionary (partially mutable) String
4
IMPLICIT CONVERSION EXPLICIT CONVERSION
5
for loop while loop
Counting loop Conditional loop
Ends when the count is over Ends when the condition is met
for i in range(2): a=1
print (i) while a < 10:
print (a)
a += 1
break continue
Used to terminate the loop when a Skips the current iteration and moves
certain condition is met. to the next iteration.
Ends the loop Does not end the loop
if a == 10: if a == 10:
break continue
6
== is
Checks if values of two objects are Checks if two objects refer to the same
equal. memory location.
Compares the content (value) of Compares the memory address (identity)
objects. of objects.
a = [1, 2, 3]; b = [1, 2, 3]; print(a == b) print(a is b)
>>> True >>> False (Different memory locations)
extend() append()
Adds a single element (as a whole) to Adds multiple elements from an iterable
the list. to the list.
Inserts the element as a single item. Unpacks and adds individual elements.
lst = [1, 2]; lst.append([3, 4]); print(lst) lst = [1, 2]; lst.extend([3, 4]); print(lst)
>>> [1, 2, [3, 4]] >>> [1, 2, 3, 4]
7
find() count()
Returns the index of the first Returns number of occurence of a
occurrence of a substring. substring
Returns -1 if the substring is not
found. Returns 0 if the substring is not found.
string.find(substring, start, end) string.count(substring, start, end)
count() index()
Returns the number of occurrences Returns the index of the first
of a substring or element. occurrence of a substring or element.
Returns 0 if the substring or Raises value error if substring is not
element is not found found
string.count(substring, start, end) string.index(substring, start, end)
8
split() partition()
Splits the string into three parts: before
Splits the string in list from the first
the separator, the separator itself, and
occurence of the substring given
after the separator.
Requires a separator; if not found,
If no separator is provided, splits at
returns the original string and two
whitespace by default.
empty strings.
string.split(separator, maxsplit) string.partition(separator)
s = "apple,orange,banana" s = "apple,orange,banana"
print(s.split(",")) print(s.partition(","))
>>> ['apple', 'orange', 'banana'] >>> ('apple', ',', 'orange,banana')
9
pop() remove()
Removes and returns the element at
removes the first occurence of the Value
the specified index
Removes last element if the index is not
Raises ValueError if value is not given
given
Index is given as parameter Value is given as parameter
list.pop(index) list.remove(value)
Removes a specific key-value pair Removes and returns the last inserted
using the key. key-value pair.
Requires a key as an argument. No arguments required.
11
sort() sorted()
Sorts the list in-place (modifies the Returns a new sorted list without
original list). changing the original.
12