2. Python Basics

English translation

Contents

2.1 Comments

# helloworld
print('hello world')
hello world
'''
Function name: helloworld()
This function prints 'hello world'
'''
def helloworld():
    print('hello world')

helloworld()
hello world

2.2 Indentation and code blocks

print('start')
for i in range(10):
    if i>5:
        print('i = %d > 5' % i)
        # The if block ends here.
    elif i<5:
        print('i = %d < 5' % i)
    else:
        print('i = %d = 5' % i)
    # The for block ends here.
print('end')
start
i = 0 < 5
i = 1 < 5
i = 2 < 5
i = 3 < 5
i = 4 < 5
i = 5 = 5
i = 6 > 5
i = 7 > 5
i = 8 > 5
i = 9 > 5
end
def function1():
    print('  function start')
    print('  function end')
# The function body ends here.

print('script start')
function1()
print('script end')
script start
  function start
  function end
script end

2.3 Input and output

a = input()
print(a)
10
10
i = 4
j = 4.5
k = 'abc'
print('i=%d j=%.1f k=%s' % (i,j,k))
i=4 j=4.5 k=abc