Java (2)

Here we look at common Python operations.

Strings in Python

Now let’s look at some common String operations in Python.

s = "Almost summer break"
s[:3]
'Alm'
s[4:7]
'st '
s.upper()
'ALMOST SUMMER BREAK'
s.lower()
'almost summer break'
array = s.split()
print(array)
['Almost', 'summer', 'break']

Lists in Python

alist = []
alist.append("Jeannie")
alist.append("Rohit")
alist.append("Lida")
alist.append("Steve")
alist.append("Dan")
alist.append("Sam")
print(alist)
['Jeannie', 'Rohit', 'Lida', 'Steve', 'Dan', 'Sam']
alist.insert(3, "Iris")
print(alist)
['Jeannie', 'Rohit', 'Lida', 'Iris', 'Steve', 'Dan', 'Sam']
alist[2]
'Lida'
alist[5] = "Steve"
print(alist)
['Jeannie', 'Rohit', 'Lida', 'Iris', 'Steve', 'Steve', 'Sam']

Dictionaries in Python

csCourses = dict()
csCourses[237] = "Computer Organization"
csCourses[134] = "Intro to Computer Science"
csCourses[136] = "Data Structures"
csCourses[256] = "Algorithms"
csCourses[237]
'Computer Organization'
csCourses.get(134)
'Intro to Computer Science'
134 in csCourses
True
361 in csCourses.keys()
False
"Data Structures" in csCourses.values()
True

Conditional Statements

a = 1 
b = 2
if a < b:
    print("a < b")
a < b
if a > b:
    print("a > b")
else:
    print("a < b")
a < b
c = 3
if a > b and a > c:
    print("a is largest")
elif b > a and b > c:
    print("b is largest")
else:
    print("c is largest")
c is largest