Python for absolute beginners – Part3

Array

x = [1, 3, 2, 5, 8, 0]

  • x[2] ==> 2
  • x[-1] ==> 0
  • x[0:3] ==> [1, 3, 2]
  • x[3:] ==> [5, 8, 0]
  • x[:3] ==> [1, 3, 2]

some functions:

  • x.insert(2, 6) ==> [1, 3, 6, 2, 5, 8, 0]
  • x.remove(2) ==> [1, 3, 5, 8, 0]
  • x.clear() ==> []
  • x.pop() ==> [1, 3, 5, 8]
  • x.index(3) ==> 5
  • x.sort() ==> [0 ,1, 2, 3, 5, 8]
  • x.reverse() ==> [0 ,8, 5, 2, 3, 1]

Conditions – (if, elif, else)

if ( x > 7):
 print(“X is bigger than 7”)
elif ( x == 7):
 print(“X is equal to 7”)
else:
 print(“X is smaller than 7”)

Loops

WHILE
i = 0
while (i < 5):
 print(i)
 i += 1
 if (i == 2):
  print(“i is 2”)
  break

FOR
for item in “String”:
 print(item)
for item in [2, 5, 8, 0, 3]:
 print(item)
for item in range(5):
 print(item)

data = [ [2, 5, 7], [1, 3, 5], [0, 2, 4] ]
for row in data:
 for item in row:
  print(item)

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s