Skip to content
Snippets Groups Projects
Commit 9de92ea0 authored by Ashwin Maran's avatar Ashwin Maran
Browse files

Upload New File

parent 882bdee3
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
## Problem 1
%% Cell type:code id: tags:
``` python
print("a" < "z", "ax" < "ay", "abc" < "abCd")
print("a" < "Z", "x2" < "x1", "zero" < "999")
print("x" < "x", "abcX" < "abcY", "10" < "999")
print("0" < "x", "abcX" < "aBcY", "1000" < "999")
print("1" < "0", "abc" < "abcd", "88888888888" < "9")
```
%% Output
True True False
False False False
False True True
True False True
False True True
%% Cell type:markdown id: tags:
## Problem 2 left column
%% Cell type:code id: tags:
``` python
print("dog".upper())
print("Dog".lower())
print(" paint ".strip())
print(" paint ".rstrip())
print("abcd".startswith("ab"))
```
%% Output
DOG
dog
paint
paint
True
%% Cell type:markdown id: tags:
## Problem 2 right column
%% Cell type:code id: tags:
``` python
print("abcd".endswith("bc"))
print("abcd".find("a"))
print("abcd".find("c"))
print("abcd".find("B"))
print("Python".find("th"))
```
%% Output
False
0
2
-1
2
%% Cell type:markdown id: tags:
## Problem 3
%% Cell type:code id: tags:
``` python
msg = "Hello"
x = "num= 13" # let's assume there is a space after the =
print("abc"[0])
print("abc"[2])
print("abc"[-1])
print(msg[4])
# print(msg[5]) # will cause error - see following cells
# print(msg[len(msg)]) # will cause error - see following cells
print(x[len(x) - 1])
print(x[3])
print(x[1] + x[2])
```
%% Output
a
c
c
o
3
=
um
%% Cell type:code id: tags:
``` python
print(msg[5])
```
%% Output
---------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[5], line 1
----> 1 print(msg[5])
IndexError: string index out of range
%% Cell type:code id: tags:
``` python
print(msg[len(msg)])
```
%% Output
---------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[6], line 1
----> 1 print(msg[len(msg)])
IndexError: string index out of range
%% Cell type:markdown id: tags:
## Problem 4
%% Cell type:code id: tags:
``` python
p = "= "
print("abcde"[0:2])
print("abcde"[2:6])
print("abcde"[2:9])
print(msg[:2])
print(msg[2:])
print(msg[-2:])
print(msg[:msg.find('=')])
print(msg[msg.find(' ')+1:])
print(msg[msg.find(p)+len(p):])
```
%% Output
ab
cde
cde
He
llo
lo
Hell
Hello
ello
%% Cell type:markdown id: tags:
## Problem 5
%% Cell type:code id: tags:
``` python
msg = "301"
A = ""
B = ""
for character in msg:
print(msg)
A = A + character + "."
B = character + B
# What is in A afterwards?
print(A)
# What is in B afterwards?
print(B)
```
%% Output
301
301
301
3.0.1.
103
%% Cell type:markdown id: tags:
## Problem 6
%% Cell type:code id: tags:
``` python
s = "PYTHON"
for i in range(len(s)):
print(s[:i+1])
```
%% Output
P
PY
PYT
PYTH
PYTHO
PYTHON
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment