Professional Documents
Culture Documents
L2 Extended
L2 Extended
Read
Unicode 16 for Devanagari script
About Unicode organization
ASCII and Unicode
Example:
len(‘Hello’) is 5, indexed from 0 through 4
>>>‘John’[0]
J
>>>‘John’[3]
n
>>>’John’[4]
-- IndexError: string index out of range since len(‘John’) is 4
>>>‘John’[-1]
n
Read this as ‘what comes
>>>‘John’[:] before 0’, viz.
John len(‘John’) -1 = 3
>>>‘John’[:2]
Joh Jo
Examples:
>>> s1 = "Hello World“
>>> print(s1[4:11:2])
oWrd
>>> s2 = "Hello“
>>> print(s2[1:len(s2):1]) # same as print(s2[1:5:1])
ello
See also:
https://tinyurl.com/yukwsf22
17
Input multiple data items
One way to input no. of data items:
>>>input(‘x= ‘)
x= 123
>>>print(x)
123
>>>Input(‘y= ‘)
y= 345
>>>print(y)
345
• Example:
T1 = float(input('Time 1? '))
T2 = float(input('Time 2? '))
print('T1, T2 ', T1, T2)
if(T1 < T2):
minT = T1
else:
minT = T2
print(T1, T2, minT)
• Example:
T1 = float(input('Time 1? '))
T2 = float(input('Time 2? '))
print('T1, T2 ', T1, T2)
if(T1 < T2):
minT = T1
else:
minT = T2
print(T1, T2, minT)
• Example:
INC = float(input(‘Your Income? '))
Tax = 0
if INC > 200000:
Tax = 10000 + 0.2*(INC-200000)
elif INC > 100000:
Tax = 0.1*(INC-100000)
print(‘Income is ‘, INC, ‘Tax is ‘, Tax)
• Example:
if x%2 == 0:
if x%3 == 0:
print(x, ‘is divisible by 2 and 3’)
else:
print(x, ‘is divisible by 2 but not by 3’)
elif x%3 == 0:
print(x, ‘is divisible by 3 but not by 2’)
• In Python
while C:
S
• In Python:
# Find the largest n such that 2^n ≤ 50
n = 0
x = 2**n
while x <= 50:
n = n+1
x = 2**n
print('largest n such that 2^n <= 50 is ', n-1)
Test of condition n x x<=50
• Question: what will be the output?
1 st 0 1 TRUE
2 nd 1 2 TRUE
3 rd 2 4 TRUE
4 th 3 8 TRUE
5 th 4 16 TRUE
6 th 5 32 TRUE
7 th 6 64 FALSE
Iteration
• Example: computing square root y = √x, where x > 0
Somewhat informal version of an algorithm
1. Start with a guess, g = x/2 # for instance
2. if |g*g – x| is small
then [conclude g = √x; output(g); stop]
else [compute new guess g = (g + x/g)/2; repeat step 2]
Example outcomes:
Let x = 3
or x = 16
Round g |g*g-x|
1 8 48
2 5 9
3 4.1 0.81
4 4.00122 0.009758
Intro
Intro to
to Programming
Programming Monsoon
Jan-May 2021
2023 30
Iteration
Example outcomes:
Let x = 3
or x =Round
16 g |g*g-x|
1 8 48
2 5 9
3 4.1 0.81
4 4.00122 0.009758
Intro
Intro to
to Programming
Programming Monsoon
Jan-May 2021
2023 32
A note on indentation
Beware: In Python indentation matters:
In pseudo code:
# compute the SQRT of 2.0
tolerance = 1.0 e-15;
lower = 0.0;
upper = 2.0;
uncertainty = upper-lower;
while uncertainty > tolerance do
[middle = (lower + upper)/2;
if middle**2 < 2.0
then lower = middle
else upper = middle;
print(lower, upper);
uncertainty = upper-lower
]
Iteration
• Example: computing square root x = √k, k > 0, or solving equation x^2 - k = 0
Another algorithm, based on “Newton-Raphson method” where we try solve for g such
that equation g*g - b = 0
-b 34
Iteration
• Example: computing square root x = √k, k > 0, or solving equation x^2 - k = 0
Another algorithm, based on “Newton-Raphson method” where we try solve for g such
that equation g*g - b = 0
-b 35
Iteration – break command
• break commend
• Used to terminate the loop when break statement is encountered
• Improves efficiency (need not wait until loop terminates)
• Control is transferred to statement following loop
Output:
132 is divisible by 11 and 12
• In Python:
for k in [sequence]:
S
• Example:
pets = [‘cat’,’dog’,’cow’]
for k in pets:
print(k)
• Another example:
for k in [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]:
if (k%2 == 0 and k%3 == 0):
print(k, ‘is divisible by 2 and 3’)
print(‘That is it’)
• In Python:
for k in [sequence]:
S
• Example:
pets = [‘cat’,’dog’,’cow’]
for k in pets:
print(k)
• Another example:
for k in [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]:
if (k%2 == 0 and k%3 == 0):
print(k, ‘is divisible by 2 and 3’)
print(‘That is it’)
• In Python:
for k in [sequence]:
S
• Example:
pets = [‘cat’,’dog’,’cow’]
for k in pets:
print(k)
• Another example:
for k in [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]:
if (k%2 == 0 and k%3 == 0):
print(k, ‘is divisible by 2 and 3’)
print(‘That is it’)
for k in [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]:
if (k%2 == 0 and k%3 == 0):
print(k, ‘is divisible by 2 and 3’)
print(‘That is it’)
for k in range(1,19):
if (k%2 == 0 and k%3 == 0):
print(k, 'is divisible by 2 and 3')
print('that is it')
for k in range(1,19):
Is effectively the same as:
for k in [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]:
Why?
Why?