Introduction to Python Damian Gordon printHello World PROGRAM








































![# PROGRAM Sequential. Search: Age = [44, 23, 42, 33, 18, 54, 34, 18] # PROGRAM Sequential. Search: Age = [44, 23, 42, 33, 18, 54, 34, 18]](https://slidetodoc.com/presentation_image/297a34fc3afb3c1fa6c2d37c251c2702/image-41.jpg)



![# PROGRAM Bubblesort: Age = [44, 23, 42, 33, 18, 54, 34, 16] for # PROGRAM Bubblesort: Age = [44, 23, 42, 33, 18, 54, 34, 16] for](https://slidetodoc.com/presentation_image/297a34fc3afb3c1fa6c2d37c251c2702/image-45.jpg)
![# PROGRAM Selection. Sort: Age = [44, 23, 42, 33, 18, 54, 34, 16] # PROGRAM Selection. Sort: Age = [44, 23, 42, 33, 18, 54, 34, 16]](https://slidetodoc.com/presentation_image/297a34fc3afb3c1fa6c2d37c251c2702/image-46.jpg)

![Multi-dimensional Arrays • Or like this: Ages = [[2, 6, 3], [7, 5, 9]] Multi-dimensional Arrays • Or like this: Ages = [[2, 6, 3], [7, 5, 9]]](https://slidetodoc.com/presentation_image/297a34fc3afb3c1fa6c2d37c251c2702/image-48.jpg)
- Slides: 48

Introduction to Python Damian Gordon

print(“Hello, World”)

# PROGRAM Hello. World. Program. Joined: print(“Hello, World” + “ I’m here”) # END.

# PROGRAM Hello. World. Program 10 Times: print(“Hello, World” * 10) # END.

Code Description \ Print a backslash ’ Print a single quote ” Print a double quote a Play a beep n Print a new line t Print a tab

# PROGRAM Adding. Numbers: print(10 + 7) # END.

Some Simple Maths • Division is a lot cooler, we can do three kinds of division, – Regular Division – Integer Division – Division Remainder

# PROGRAM Regular. Division: print(“ 10 / 7 = “, 10 / 7) # END.

# PROGRAM Regular. Division: print(“ 10 / 7 = “, 10 / 7) # END. This should give us: 1. 428571

# PROGRAM Integer. Division: print(“ 10 // 7 = “, 10 // 7) # END.

# PROGRAM Integer. Division: print(“ 10 // 7 = “, 10 // 7) # END. This should give us: 1

# PROGRAM Integer. Division: print(“ 10 // 7 = “, 10 // 7) # END. This should give us: 1 which is how many times 7 divides evenly into 10

# PROGRAM Division. Remainder: print(“ 10 % 7 = “, 10 % 7) # END.

# PROGRAM Division. Remainder: print(“ 10 % 7 = “, 10 % 7) # END. This should give us: 3

# PROGRAM Division. Remainder: print(“ 10 % 7 = “, 10 % 7) # END. This should give us: 3 which is what is left over when we divide 7 into 10

# PROGRAM Variable. Print: x = 6 print(x) # END.

# PROGRAM Add. One. Variable. Print: x = 6 print(x + 1) # END.

# PROGRAM Print. Message: print(“Please input a message: ”) New. Msg = input() print(New. Msg) # END.

# PROGRAM Convert. From. Celsius. To. Fahrenheit: print(“Please input your temperature in C: ”) Input. Val = int(input()); print(“That temperature in F is: ”) print((Input. Val *2) + 30) # END.

Convert Description int(x) Convert variable into an integer, e. g. x = “ 10” int(x) Convert variable into a real e. g. x = “ 10. 5” float(x) Convert variable into an string, e. g. x = 10 str(x) float(x) str(x) Result 10 10. 5 “ 10”

Using Variables • The following words cannot be used as variable names: and as assert break del elif else except from global if import not or pass print class continue def exec finally for in is lambda raise return try while with yield

Python: IF statement • In Python the general form of the IF statement is as follows: if CONDITION: STATEMENT(S) else: STATEMENT(S)

# PROGRAM Simple. If. Statement: x = 6 y = 7 if x > y: # THEN print(“x is bigger”) else: print(“y is bigger”) # ENDIF; # END.

# PROGRAM Is. Odd. Or. Even: x = int(input(“Please input the numbern”)) if (x % 2) != 0: # THEN print(x, “is odd”) else: print(x, “is even”) # ENDIF; # END.

Operator Description != is not equal to == is equal to > is greater than < is less than >= is greater than or equal to <= is less than or equal to

# a b c PROGRAM Bigger. Of. Three: = int(input(“Please input the first valuen”)) = int(input(“Please second the second valuen”)) = int(input(“Please second the third valuen”)) if a > b: # THEN if a > c: # THEN print(a, else: print(c, # ENDIF; else: if b > c: # THEN print(b, else: print(c, # ENDIF; # END. “is bigger than”, b, “ and ”, c) “is bigger than”, a, “ and ”, b)

Python: IF-ESIF statement • In Python the general form of the IF-ESIF statement is as follows: if CONDITION: STATEMENT(S) elif CONDITION: STATEMENT(S) else: STATEMENT(S)

# PROGRAM Multi. Choice. Question: Input. Value = input("Please input your answer: n") if Input. Value == "a": # THEN print("Wrong Answer") elif Input. Value == "b": # THEN print("Wrong Answer") elif Input. Value == "c": # THEN print("Right Answer") elif Input. Value == "d": # THEN print("Wrong Answer") else: print("Bad Option") # ENDIF; # END.

Python: WHILE loop • The WHILE loop works as follows: while CONDITION: STATEMENTS

# PROGRAM Print 1 To 5: a = 1 while a != 6: # DO print(a) a = a + 1 # ENDWHILE; # END.

# PROGRAM Sum 1 To 5: a = 1 total = 0 while a != 6: # DO total = total + a a = a + 1 # ENDWHILE; print(total) # END.

Python: WHILE loop • The FOR loop works as follows: for RANGE: STATEMENTS

# PROGRAM Print 1 To 5 For: for a in range(1, 6): # DO print(a) # ENDFOR; # END.

# PROGRAM Check. Prime: a = int(input("Please input value: ")) b = a - 1 Is. Prime = True while b != 1: # DO if a % b == 0: # THEN Is. Prime = False # ENDIF; b = b - 1 # ENDWHILE; if Is. Prime: # THEN print(a, "is a prime number") else: print(a, "is not a prime number") # ENDIF; # END.

# PROGRAM Fibonacci. Numbers: a = int(input("Please input value: ")) First. Num = 1 Second. Num = 1 while a != 1: # DO total = Second. Num + First. Num = Second. Num = total a = a - 1 # ENDWHILE; print(total) # END.

############# # Prime Checking Module # ############# def Is. It. Prime(): a = int(input("Please input value: ")) b = a - 1 Is. Prime = True while b != 1: # DO if a % b == 0: # THEN Is. Prime = False # ENDIF; b = b - 1 # ENDWHILE; return Is. Prime # END Is. It. Prime.

######## # Main Program # ######## # PROGRAM Check. Prime: if Is. It. Prime() == True: # THEN print("Prime number") else: print("Not a prime number") # ENDIF; # END.

Arrays • To declare an zero-filled array in Python we can do the following: Age = [0 for x in range(8)]

Arrays • To declare an array with values in Python: Age = [44, 23, 42, 33, 16, 54, 34, 18]

# PROGRAM Sample. Array. Prog: Age = [44, 23, 42, 33, 16, 54, 34, 18] for a in range(0, 8): # DO print(Age[a]) # ENDFOR; # END.
![PROGRAM Sequential Search Age 44 23 42 33 18 54 34 18 # PROGRAM Sequential. Search: Age = [44, 23, 42, 33, 18, 54, 34, 18]](https://slidetodoc.com/presentation_image/297a34fc3afb3c1fa6c2d37c251c2702/image-41.jpg)
# PROGRAM Sequential. Search: Age = [44, 23, 42, 33, 18, 54, 34, 18] for a in range(0, len(Age)): # DO if Age[a] == 18: # THEN print("User", a, "is 18") # ENDIF; # ENDFOR; # END.

Part 1 of 3 # PROGRAM Binary. Search: Age = [16, 18, 23, 31, 33, 34, 46, 54] Search. Val = int(input("Please input the search value: ")) first = 0 last = len(Age) Is. Found = False

while first <= last and Is. Found == False: # DO index = (first + last) // 2 if Age[index] == Search. Val: # THEN Is. Found = True print("Value found") elif Age[index] > Search. Val: # THEN last = index - 1 else: first = index + 1 # ENDIF; # ENDWHILE; Part 2 of 3

Part 3 of 3 if Is. Found == False: # THEN print("Value not in array") # ENDIF; # END.
![PROGRAM Bubblesort Age 44 23 42 33 18 54 34 16 for # PROGRAM Bubblesort: Age = [44, 23, 42, 33, 18, 54, 34, 16] for](https://slidetodoc.com/presentation_image/297a34fc3afb3c1fa6c2d37c251c2702/image-45.jpg)
# PROGRAM Bubblesort: Age = [44, 23, 42, 33, 18, 54, 34, 16] for outerindex in range(0, len(Age)): # DO for index in range(0, len(Age)-1): # DO if Age[index+1] < Age[index]: # THEN Temp. Value = Age[index+1] = Age[index] = Temp. Value # ENDIF; # ENDFOR; print(Age) # END.
![PROGRAM Selection Sort Age 44 23 42 33 18 54 34 16 # PROGRAM Selection. Sort: Age = [44, 23, 42, 33, 18, 54, 34, 16]](https://slidetodoc.com/presentation_image/297a34fc3afb3c1fa6c2d37c251c2702/image-46.jpg)
# PROGRAM Selection. Sort: Age = [44, 23, 42, 33, 18, 54, 34, 16] for outerindex in range(0, len(Age)): # DO Min. Val. Location = outerindex for index in range(outerindex, len(Age)): # DO if Age[index] < Age[Min. Val. Location]: # THEN Min. Val. Location = index # ENDIF; # ENDFOR; if Min. Val. Location != outerindex: Age[outerindex], Age[Min. Val. Location] = Age[Min. Val. Location], Age[outerindex] # ENDFOR; print(Age) # END.

Multi-dimensional Arrays • We declare a multi-dimensional array as follows: Ages = [[0 for x in range(8)]
![Multidimensional Arrays Or like this Ages 2 6 3 7 5 9 Multi-dimensional Arrays • Or like this: Ages = [[2, 6, 3], [7, 5, 9]]](https://slidetodoc.com/presentation_image/297a34fc3afb3c1fa6c2d37c251c2702/image-48.jpg)
Multi-dimensional Arrays • Or like this: Ages = [[2, 6, 3], [7, 5, 9]]