2.6 Some other features of Python

2.6.1 Loops

Programming languages usually allows us to repeat things, over and over. This is a very important feature. One of the ways to do this is using something called a for loop. Here are some examples:

Example 1

for n in names:
  print(n)
## Yih-Cherng
## Chammika
## Zhi Han
## Linda
## Robert
## Max
## Hai Xiang
## Jing Xian

Example 2

for n in names:
  print('---'+ n + '---')
## ---Yih-Cherng---
## ---Chammika---
## ---Zhi Han---
## ---Linda---
## ---Robert---
## ---Max---
## ---Hai Xiang---
## ---Jing Xian---

Example 3

for n in names:
  print(n.upper())
## YIH-CHERNG
## CHAMMIKA
## ZHI HAN
## LINDA
## ROBERT
## MAX
## HAI XIANG
## JING XIAN

2.6.2 Our Own Functions

Lets define a function

def funny(n):
  print('Hahaha'*n)

Lets use our function

funny(4)
## HahahaHahahaHahahaHahaha
funny(10)
## HahahaHahahaHahahaHahahaHahahaHahahaHahahaHahahaHahahaHahaha