Download presentation
Presentation is loading. Please wait.
1
Introduction to Python
Damian Gordon
2
print(“Hello, World”)
3
# PROGRAM HelloWorldProgramJoined: print(“Hello, World” + “ I’m here”) # END.
4
# PROGRAM HelloWorldProgram10Times: print(“Hello, World” * 10) # END.
5
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
6
# PROGRAM AddingNumbers: print(10 + 7) # END.
7
Some Simple Maths Division is a lot cooler, we can do three kinds of division, Regular Division Integer Division Division Remainder
8
# PROGRAM RegularDivision: print(“10 / 7 = “, 10 / 7) # END.
9
# PROGRAM RegularDivision: print(“10 / 7 = “, 10 / 7) # END.
This should give us:
10
# PROGRAM IntegerDivision: print(“10 // 7 = “, 10 // 7) # END.
11
# PROGRAM IntegerDivision: print(“10 // 7 = “, 10 // 7) # END.
This should give us: 1
12
which is how many times 7 divides evenly into 10
# PROGRAM IntegerDivision: print(“10 // 7 = “, 10 // 7) # END. which is how many times 7 divides evenly into 10 This should give us: 1
13
# PROGRAM DivisionRemainder: print(“10 % 7 = “, 10 % 7) # END.
14
# PROGRAM DivisionRemainder: print(“10 % 7 = “, 10 % 7) # END.
This should give us: 3
15
which is what is left over when we divide 7 into 10
# PROGRAM DivisionRemainder: print(“10 % 7 = “, 10 % 7) # END. which is what is left over when we divide 7 into 10 This should give us: 3
16
# PROGRAM VariablePrint: x = 6 print(x) # END.
17
# PROGRAM AddOneVariablePrint: x = 6 print(x + 1) # END.
18
# PROGRAM PrintMessage: print(“Please input a message: ”) NewMsg = input() print(NewMsg) # END.
19
# PROGRAM ConvertFromCelsiusToFahrenheit: print(“Please input your temperature in C:”) InputVal = int(input()); print(“That temperature in F is:”) print((InputVal *2) + 30) # END.
20
Convert Description Result
int(x) Convert variable into an integer, e.g. x = “10” 10 float(x) Convert variable into a real e.g. x = “10.5” 10.5 str(x) Convert variable into an string, e.g. x = 10 “10”
21
Using Variables The following words cannot be used as variable names:
and del from not while as elif global or with assert else if pass yield break except import print class exec in raise continue finally is return def for lambda try
22
Python: IF statement In Python the general form of the IF statement is as follows: if CONDITION: STATEMENT(S) else:
23
# PROGRAM SimpleIfStatement: x = 6 y = 7 if x > y: # THEN print(“x is bigger”) else: print(“y is bigger”) # ENDIF; # END.
24
# PROGRAM IsOddOrEven: x = int(input(“Please input the number\n”)) if (x % 2) != 0: # THEN print(x, “is odd”) else: print(x, “is even”) # ENDIF; # END.
25
is greater than or equal to
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
26
# PROGRAM BiggerOfThree: a = int(input(“Please input the first value\n”)) b = int(input(“Please second the second value\n”)) c = int(input(“Please second the third value\n”)) if a > b: # THEN if a > c: print(a, “is bigger than”, b, “ and ”, c) else: print(c, “is bigger than”, a, “ and ”, c) # ENDIF; if b > c: print(b, “is bigger than”, a, “ and ”, c) print(c, “is bigger than”, a, “ and ”, b) # END.
27
Python: IF-ESIF statement
In Python the general form of the IF-ESIF statement is as follows: if CONDITION: STATEMENT(S) elif CONDITION: else:
28
# PROGRAM MultiChoiceQuestion: InputValue = input("Please input your answer:\n") if InputValue == "a": # THEN print("Wrong Answer") elif InputValue == "b": elif InputValue == "c": print("Right Answer") elif InputValue == "d": else: print("Bad Option") # ENDIF; # END.
29
Python: WHILE loop The WHILE loop works as follows: while CONDITION:
STATEMENTS
30
# PROGRAM Print1To5: a = 1 while a
# PROGRAM Print1To5: a = 1 while a != 6: # DO print(a) a = a + 1 # ENDWHILE; # END.
31
# PROGRAM Sum1To5: a = 1 total = 0 while a
# PROGRAM Sum1To5: a = 1 total = 0 while a != 6: # DO total = total + a a = a + 1 # ENDWHILE; print(total) # END.
32
Python: WHILE loop The FOR loop works as follows: for RANGE:
STATEMENTS
33
# PROGRAM Print1To5For: for a in range(1,6): # DO print(a) # ENDFOR; # END.
34
# PROGRAM CheckPrime: a = int(input("Please input value:")) b = a - 1 IsPrime = True while b != 1: # DO if a % b == 0: # THEN IsPrime = False # ENDIF; b = b - 1 # ENDWHILE; if IsPrime: print(a, "is a prime number") else: print(a, "is not a prime number") # END.
35
# PROGRAM FibonacciNumbers: a = int(input("Please input value:")) FirstNum = 1 SecondNum = 1 while a != 1: # DO total = SecondNum + FirstNum FirstNum = SecondNum SecondNum = total a = a - 1 # ENDWHILE; print(total) # END.
36
######################### # Prime Checking Module # def IsItPrime(): a = int(input("Please input value: ")) b = a - 1 IsPrime = True while b != 1: # DO if a % b == 0: # THEN IsPrime = False # ENDIF; b = b - 1 # ENDWHILE; return IsPrime # END IsItPrime.
37
################ # Main Program # # PROGRAM CheckPrime: if IsItPrime() == True: # THEN print("Prime number") else: print("Not a prime number") # ENDIF; # END.
38
Arrays To declare an zero-filled array in Python we can do the following: Age = [0 for x in range(8)]
39
Arrays To declare an array with values in Python:
Age = [44, 23, 42, 33, 16, 54, 34, 18]
40
# PROGRAM SampleArrayProg: Age = [44, 23, 42, 33, 16, 54, 34, 18] for a in range(0,8): # DO print(Age[a]) # ENDFOR; # END.
41
# PROGRAM SequentialSearch: 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.
42
# PROGRAM BinarySearch: Age = [16, 18, 23, 31, 33, 34, 46, 54] SearchVal = int(input("Please input the search value: ")) first = 0 last = len(Age) IsFound = False Part 1 of 3
43
while first <= last and IsFound == False: # DO index = (first + last) // 2 if Age[index] == SearchVal: # THEN IsFound = True print("Value found") elif Age[index] > SearchVal: last = index - 1 else: first = index + 1 # ENDIF; # ENDWHILE; Part 2 of 3
44
if IsFound == False: # THEN print("Value not in array") # ENDIF; # END.
Part 3 of 3
45
# 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): if Age[index+1] < Age[index]: # THEN TempValue = Age[index+1] Age[index+1] = Age[index] Age[index] = TempValue # ENDIF; # ENDFOR; print(Age) # END.
46
# PROGRAM SelectionSort: Age = [44, 23, 42, 33, 18, 54, 34, 16] for outerindex in range(0,len(Age)): # DO MinValLocation = outerindex for index in range(outerindex,len(Age)): if Age[index] < Age[MinValLocation]: # THEN MinValLocation = index # ENDIF; # ENDFOR; if MinValLocation != outerindex: Age[outerindex], Age[MinValLocation] = Age[MinValLocation], Age[outerindex] print(Age) # END.
47
Multi-dimensional Arrays
We declare a multi-dimensional array as follows: Ages = [[0 for x in range(8)] for x in range(8)]
48
Multi-dimensional Arrays
Or like this: Ages = [[2,6,3],[7,5,9]]
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.