You are on page 1of 5

# This is a sample Python program

import numpy as np # import a package numpy; use np as an alias

# This is an assignment statement; x is a variable; "=" is the


import numpy as np assignment sign; 2 is the value of x
x = 2
x = 2
print("Hello World!") print("Hello World!")
print(6 * 7) # print() is a built-in function
print(np.pi) # we use print() to show the result
print(x) print(6 * 7)
print(np.pi) # we use pi from np package
print(x)
def add_one(num): # a user-defined function
# NOTE THE INDENTATION!!! four blank spaces or a tab
num = num + 1 # store a process
return num # return a result

y = add_one(x) # store the result in a new variable


print(y) # print the variable

x = 1 # int
PI = 3.14 # float
condition = True # boolean
s = "CUHK-SZ" # string, both single quotes and double quotes
are ok

type(1) # int
type(False) # bool

bool(0) # False 5 ** 4 # power, note that ^ is not for power


bool(1) # True 7 // 2 # floor division, only keeping the integer part
bool(0.0) # False 7 % 2 # modulo, returning the remainder after division
bool(2) # True

int(-2.5) # -2; truncate the decimal part (not round)


int(2.5) # 2
str(1.234) # '1.234'
float('1.1234') # 1.1234
t = "I am a student in CUHK-Shenzhen. I am learning Python."
len(t) # length and index
t.capitalize() # t.upper(), t.lower(), t.title()
t.split()
t.find('student')
t.replace('am', '/')
t.find('am') # the find() function returns the index at which
the word shows (the first time) in the string, or -1 if it
does not shows
0.6 + 0.3
x = ' # $$ # '
len(x) # return the length
0.8999999999999999
x.strip()

x + t # the addition of strings, directly connecting them


s = x + t # use a variable s to store the result
print(s) # print the variable s
3 == 2 # equal to
3 != 2 # not equal to
3.5 > True # comparison between float and boolean
'A' == 'a' # False (Python is case-sensitive)

3 == 2 and 4 > 3 # False and True


3 != 2 or 4 < 3 # True or False
not 3.5 > True # not True
x >= 5 and x < 10 # 5 <= x < 10
# Refer to the truth-value table if you are not familiar

num = 5

if num <= 3:
if num <= 3:
info = "number no larger than 3"
info = "number no larger than 3"
else:
elif num <= 5:
info = "number larger than 3"
info = "number larger than 3 but no larger than 5"
elif num <= 10:
if num <= 3:
info = "number larger than 5 but no larger than 10"
info = "number no larger than 3"
else:
elif num <= 5:
info = "number larger than 10"
info = "number larger than 3 but no larger than 5"
else:
info = "number larger than 5"
for i in range(1, 5, 2): # range(1, 5, 2) represents (1, 3)
print(i)

range(start, end) # (start, start + 1, ..., end - 1)


range(5) # (0, 1, 2, 3, 4)
range(n) # (0, 1, ..., n - 1)
range(1, n + 1) # (1, 2, ..., n)
range(2, 9, 2) # (2, 4, 6, 8)

for ch in "hello world!": # string can be used for iteration


print(ch, end=' ') # set "end" to be only one blank space
instead of the '\n' by default. '\n' represents a new
line

j = 0
while j < 5: # while loop
print(j)
j = j + 1 # augmentation statement. j += 1 also works

def func(para): # define a function called func


para = para + 1 # augment para by 1
return para # return the value of para

var = func(10) # send the value of para to var


print(var) # print var

def func(para): # define a function called func


if para > 0:
para = para + 1
else:
para = -para + 1
return para

var = func(10) # send the value of para to var


print(var) # print var

You might also like