Python Basics¶

I. Primitive Data types (Boolean, Number, String)¶

Boolean¶

In [ ]:
True
Out[ ]:
True
In [ ]:
False
Out[ ]:
False
In [ ]:
True and False
Out[ ]:
False
In [ ]:
True or False
Out[ ]:
True
In [ ]:
not True
Out[ ]:
False
In [ ]:
x = 24
x > 50   # six different comparisons: >, >=, ==, <, <=, !=
x != 30
Out[ ]:
True

Number¶

In [ ]:
24
Out[ ]:
24
In [ ]:
2.35
Out[ ]:
2.35
In [ ]:
24 + 36
Out[ ]:
60
In [ ]:
2.35 * 3
Out[ ]:
7.050000000000001
In [ ]:
41/2
Out[ ]:
20.5
In [ ]:
41//2
Out[ ]:
20
In [ ]:
41%2
Out[ ]:
1

String (immutable)¶

In [ ]:
"34 Main Street"
Out[ ]:
'34 Main Street'
In [ ]:
'34 Main Street'
Out[ ]:
'34 Main Street'
In [ ]:
'''abc 
def
'''
Out[ ]:
'abc \ndef\n'
In [ ]:
len("34 Main Street")
Out[ ]:
14

Assignment Statement¶

In [ ]:
x = 24
y = len("34 Main Street")
print(x,y)
y = 36
print(x,y)
24 14
24 36
In [ ]:
s = "abracadabra"
print(s)
# slice operation
x1 = s[4:7]
print(x1)
t = "hello"
# concatenation
u = s + t
print(u)
# find
i = s.find("xad")  # finds the first occurrence of "cad" in s
print(i)
abracadabra
cad
abracadabrahello
-1
In [ ]:
s = "This is a long sentence in English long"
s.find("long")
Out[ ]:
10

Conditional Statement¶

In [ ]:
x = 100
# if-statement
if x > 50:
    print("x is more than 50")
print("I am done")
x is more than 50
I am done
In [ ]:
x = 10
if x > 50:
    print("x is more than 50")
print("I am done")
I am done
In [ ]:
x = 10
# if-else statement
if x > 50:
    print("x is more than 50")
    print("haha")
else:
    print("x is not more than 50")
print("I am done")
x is not more than 50
I am done
In [ ]:
score = 85
# if-elif-else statement
if score >= 90:
    print("A")
elif score >= 80 and score < 90:
    print("B")
elif score >= 70:
    print("C")
elif score >= 60:
    print("D")
else:
    print("F")
B

II. Collection Types (built-in data structures: list, tuple, dictionary, set, range, file)¶

List¶

In [ ]:
#      0  1  2  3  4  5
xs = [10,20,30,40,50,60]
ys = [90,100]
print(xs)
print(len(xs))
print(xs[2:4])
print(xs+ys)
print(xs.index(40))
print("abracadabra".index("cad"))
print(xs[3])
[10, 20, 30, 40, 50, 60]
6
[30, 40]
[10, 20, 30, 40, 50, 60, 90, 100]
3
4
40
In [ ]:
## list modifications
xs = [10,20,30,40,50]
print(xs)
xs.append(70)
print(xs)
xs.insert(1,90)
print(xs)
xs.remove(90)
print(xs)
[10, 20, 30, 40, 50]
[10, 20, 30, 40, 50, 70]
[10, 90, 20, 30, 40, 50, 70]
[10, 20, 30, 40, 50, 70]

Tuple (essentially a list)¶

two main differences:

  • tuple immutable
  • tuple usually contains different types of values
In [ ]:
xs = ("John",24,"CSC")
print(xs[1:3])
(24, 'CSC')

for-loop Statement¶

In [ ]:
for x in [10,20,30,40]:
    print(x)
    print(2*x)
10
20
20
40
30
60
40
80
In [ ]:
n = 9
for x in [1,2,3,4,5,6,7,8,9,10]:
    print(n,"*",x,"=",n*x)
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
In [ ]:
n = 9
for x in range(1,20):
    print(n,"*",x,"=",n*x)
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
9 * 11 = 99
9 * 12 = 108
9 * 13 = 117
9 * 14 = 126
9 * 15 = 135
9 * 16 = 144
9 * 17 = 153
9 * 18 = 162
9 * 19 = 171

while-loop statement¶

In [ ]:
x = input("Enter Earlier Time: ")
print(x)
44

Dictionary¶

set¶

range¶

file¶

III. Functions¶

IV. Classes and Objects¶