Programming in Python
Week-3 Content
Data types
Strings in Python
● Strings can be surrounded by single or double quotations.
● Can be multiline with 3 double/single quotation marks.
● Are stored in the memory as arrays
● Python doesn’t have character data type, the characters are
also internally stored as strings of length 1.
● You can access any characters of the string using indices.
● str_name[index] will give you the character at that index in
the string.
● str_name[start_index: end_index] will give substring
● Strings are immutable
Strings & Accessing characters
alpha = ‘c’ c
alpha
0
alpha = ‘coffee’ c
alpha
0
o f f e e
1 2 3 4 5
index
alpha[4]
e
4
f f e
2 3 4
alpha[2:5]
P Y T H O N
C L A S S E S
str1 =
str2 =
0 1 2 3 4 5
0 1 2 3 4 5 6
+
=
P Y T H O N
0 1 2 3 4 5
C L A S S E S
6 7 8 9 10 11 12
add =
N
5
str1[5]
S
9
add[9]
P Y T H O Nstr1 =
0 1 2 3 4 5
* 3
multiply =
P Y T H O N
0 1 2 3 4 5
Y T H O N
6 7 8 9 10 11
P Y T H O N
12 13 14 15 16 17
P
Now:
print(“Addition of %s and %s is %s”, %(str1, str2, add))
Initially:
print(“Addition of ” + str1 + “ and “ + str2 + “ is: “ + add )
Now: can be done for integers too
print(“Addition of %i and %i is %i”, %(num1, num2, add))
Python Lists
● Store multiple values in a single variable
● A single list may contain items of any data type
● len() can be used to get the length of the list
● append() can be used to add an element to an existing list
● remove() can be used to remove the first occurence of the
specified item in the list
● Each element of the list has a definite position - index starting from
0
● Lists are mutable
List Slicing
1 2 3 1 1 1
0 1 2 3 4 5
-6 -5 -4 -3 -2 -1
3 1 1 1
2 3 4 5
-4 -3 -2 -1
list[2:6]
3 1 1
2 3 4
-4 -3 -2
list[-4:-1]
3 1 1 1
2 3 4 5
-4 -3 -2 -1
list[2:]
1 2
0 1
-6 -5
list[:-4]
Tuples
● Identical to lists but created using ()
● Tuples are immutable
● Indexing and slicing works similar to strings and lists
● Use tuples instead of list when you don’t want data to be modified
● When the amount of data is too huge, tuples work faster than lists
Operator Precedence and Associativity in Python
● Combination of values, variables, operators and function calls makes a valid
expression.
● When there are more than 1 operators in an expression, rules of precedence
are used.
● If more than 1 operator is in the same group and hence has same precedence,
associativity is used to evaluate the expression.
● ** has right-to-left associativity, rest all operators have left-to-right associativity
Homework Questions

Programming in python - Week 3

  • 1.
  • 2.
  • 3.
    Strings in Python ●Strings can be surrounded by single or double quotations. ● Can be multiline with 3 double/single quotation marks. ● Are stored in the memory as arrays ● Python doesn’t have character data type, the characters are also internally stored as strings of length 1. ● You can access any characters of the string using indices. ● str_name[index] will give you the character at that index in the string. ● str_name[start_index: end_index] will give substring ● Strings are immutable
  • 5.
  • 6.
    alpha = ‘c’c alpha 0 alpha = ‘coffee’ c alpha 0 o f f e e 1 2 3 4 5 index alpha[4] e 4 f f e 2 3 4 alpha[2:5]
  • 7.
    P Y TH O N C L A S S E S str1 = str2 = 0 1 2 3 4 5 0 1 2 3 4 5 6 + = P Y T H O N 0 1 2 3 4 5 C L A S S E S 6 7 8 9 10 11 12 add = N 5 str1[5] S 9 add[9]
  • 8.
    P Y TH O Nstr1 = 0 1 2 3 4 5 * 3 multiply = P Y T H O N 0 1 2 3 4 5 Y T H O N 6 7 8 9 10 11 P Y T H O N 12 13 14 15 16 17 P
  • 9.
    Now: print(“Addition of %sand %s is %s”, %(str1, str2, add)) Initially: print(“Addition of ” + str1 + “ and “ + str2 + “ is: “ + add ) Now: can be done for integers too print(“Addition of %i and %i is %i”, %(num1, num2, add))
  • 10.
    Python Lists ● Storemultiple values in a single variable ● A single list may contain items of any data type ● len() can be used to get the length of the list ● append() can be used to add an element to an existing list ● remove() can be used to remove the first occurence of the specified item in the list ● Each element of the list has a definite position - index starting from 0 ● Lists are mutable
  • 11.
    List Slicing 1 23 1 1 1 0 1 2 3 4 5 -6 -5 -4 -3 -2 -1 3 1 1 1 2 3 4 5 -4 -3 -2 -1 list[2:6] 3 1 1 2 3 4 -4 -3 -2 list[-4:-1] 3 1 1 1 2 3 4 5 -4 -3 -2 -1 list[2:] 1 2 0 1 -6 -5 list[:-4]
  • 12.
    Tuples ● Identical tolists but created using () ● Tuples are immutable ● Indexing and slicing works similar to strings and lists ● Use tuples instead of list when you don’t want data to be modified ● When the amount of data is too huge, tuples work faster than lists
  • 13.
    Operator Precedence andAssociativity in Python ● Combination of values, variables, operators and function calls makes a valid expression. ● When there are more than 1 operators in an expression, rules of precedence are used. ● If more than 1 operator is in the same group and hence has same precedence, associativity is used to evaluate the expression. ● ** has right-to-left associativity, rest all operators have left-to-right associativity
  • 18.