In [ ]:
%autosave 0

Fundamentals of Python

Object and Variable

Everything (inclulding function) is an object in Python. Each object has type and optionally its own methods. Variable is not declared in Python. Variable does not have type but refers object.

Clause (Block)

The clause(block) of the python is grouped with indent (In case of C, clause is enclosed with brace {}). Same clause need to have same indent (number of spaces). Tab character can be used as a indent, but not recommended. PEP8 (guide line of python) defines 4 space as a recommended indent.

print() function takes any type of object. Integer, String, Float, List etc. The return value of print() function is None.


In [ ]:
print(1)
print('hello')

# please add something here ...

if statement

if and : (colon) are keyword to create if statement. Condition need to be specified between if and :. Statements in each clase need to have same indent.

if some-codition:
    statement-1
    statement-2
    ...
elif some-condition:
    statement-3
else:
    statement-4
    statement-5

Syntax of the if statement of python is similar to C, except that {} are not used to define clause and elif is used insted of else if. elif and else clause is optional and multiple elif clauses can be defined.


In [ ]:
x = int(input('Please enter your score(0-100): '))

if x >=80:
    print("You gained A. Conglaturation!!!")
elif x >=70:
    print("You are B. OK")
elif x >=60:
    print("You are C")
    print('Please try to study harder')
else:
    print("Sorry. You failed.")

while statement

Syntax of the while statement of python is similar to C. python's while statement can optionally have a else clause, which is executed if while loop exited other than break.


In [ ]:
n = 5

while n > 0:
    print(n)
    n -= 1

In [ ]:
p = int(input('Please input number: '))

x = [2,3,5,7,11, None]
i = 0

while x[i] != None:
    if x[i] == p:
        print(p, 'is found')
        break
    i += 1
else:  # while loop exited other than break
    print(p, 'not found')

for statement

Syntex of the for statement is different from C. python's for statement takes iterable object. List, Tuple and String are iterable object. Some functions such as range() returns iterable object.

for statement also can have else clause, which is executed if for loop exited except break. This feature is useful to detect the condition that something is not found in the List etc.


In [ ]:
list_a = [1,2,3, 'abc']
tuple_a = (4, 'a', 1.23)  # List and Tuple can contain different type of objects as element
string_a = 'this is a string_a'

for x in list_a:
    print(x)
print()
    
for x in tuple_a:
    print(x)
print()
    
for x in string_a:
    print(x)   # each print() prints 1 character(string with lengh:1)
print()
    
for x in (1,2,3,4,5):
    print(x)
print()
        
for x in range(5):  # range(5) returns 0, 1, 2, 3, 4 one by one
    print(x)

In [ ]:
p = int(input('Please input number: '))

for x in [2,3,5,7,11]:
    if x == p:
        print(p, 'is found')
        break
else:  # for loop exited except break
    print(p, 'not found')

def (function definition)

Function is defined using keyword def. Simple example of functions are as follows.


In [ ]:
def func1():  # Function without parameter
    print('function-1')
    
def func2(parm1, parm2): # 0 or more parameters can be passed
    print(parm1)
    print(parm2)
    
def func3(a, b):
    return a+b   # Function returns 1 or more value

func1()   # add () to call function
func2('abc', 100)
print('value of func3', func3(1,2))   # return value of function can be an argument to other function

Hands on

  • Check the usage of range() function using help()
  • print 1 to 10 vertically

In [ ]:
help(range)

In [ ]:
for i in range(??, ??):  ## change ?? to proper value
    print(i)
  • Check the usage of print() function using help()
  • print 1 to 10 horizontally

In [ ]:
help(print)

In [ ]:
# Change ??
for i in range(??, ??):
    print(i, ???=??)
  • print following tables
1x1= 1 2x1= 2 3x1= 3 4x1= 4 5x1= 5 6x1= 6 7x1= 7 8x1= 8 9x1= 9
1x2= 2 2x2= 4 3x2= 6 4x2= 8 5x2=10 6x2=12 7x2=14 8x2=16 9x2=18
1x3= 3 2x3= 6 3x3= 9 4x3=12 5x3=15 6x3=18 7x3=21 8x3=24 9x3=27
1x4= 4 2x4= 8 3x4=12 4x4=16 5x4=20 6x4=24 7x4=28 8x4=32 9x4=36
1x5= 5 2x5=10 3x5=15 4x5=20 5x5=25 6x5=30 7x5=35 8x5=40 9x5=45
1x6= 6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 7x6=42 8x6=48 9x6=54
1x7= 7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 8x7=56 9x7=63
1x8= 8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 9x8=72
1x9= 9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81

In [ ]:
for row in range(??, ??):
    for col in range(??, ??):
        print(????)
    ????