Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Introduction to Python Damian Gordon print(“Hello, World”) # PROGRAM HelloWorldProgramJoined: print(“Hello, World” + “ I’m here”) # END. # PROGRAM HelloWorldProgram10Times: 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 AddingNumbers: 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 RegularDivision: print(“10 / 7 = “, 10 / 7) # END. # PROGRAM RegularDivision: print(“10 / 7 = “, 10 / 7) # END. This should give us: 1.428571 # PROGRAM IntegerDivision: print(“10 // 7 = “, 10 // 7) # END. # PROGRAM IntegerDivision: print(“10 // 7 = “, 10 // 7) # END. This should give us: 1 # PROGRAM IntegerDivision: print(“10 // 7 = “, 10 // 7) # END. This should give us: 1 which is how many times 7 divides evenly into 10 # PROGRAM DivisionRemainder: print(“10 % 7 = “, 10 % 7) # END. # PROGRAM DivisionRemainder: print(“10 % 7 = “, 10 % 7) # END. This should give us: 3 # PROGRAM DivisionRemainder: print(“10 % 7 = “, 10 % 7) # END. This should give us: 3 which is what is left over when we divide 7 into 10 # PROGRAM VariablePrint: x = 6 print(x) # END. # PROGRAM AddOneVariablePrint: x = 6 print(x + 1) # END. # PROGRAM PrintMessage: print(“Please input a message: ”) NewMsg = input() print(NewMsg) # END. # PROGRAM ConvertFromCelsiusToFahrenheit: print(“Please input your temperature in C:”) InputVal = int(input()); print(“That temperature in F is:”) print((InputVal *2) + 30) # END. Convert Description Result int(x) Convert variable into an integer, e.g. x = “10” int(x) 10 Convert variable into a real e.g. x = “10.5” float(x) 10.5 Convert variable into an string, e.g. x = 10 str(x) “10” float(x) str(x) Using Variables • The following words cannot be used as variable names: and del from not while as assert break class elif else except exec global if import in or pass print raise with yield continue def finally for is lambda return try Python: IF statement • In Python the general form of the IF statement is as follows: if CONDITION: STATEMENT(S) else: STATEMENT(S) # PROGRAM SimpleIfStatement: x = 6 y = 7 if x > y: # THEN print(“x is bigger”) else: print(“y is bigger”) # ENDIF; # END. # 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. 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 BiggerOfThree: = int(input(“Please input the first value\n”)) = int(input(“Please second the second value\n”)) = int(input(“Please second the third value\n”)) 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; # ENDIF; # END. “is bigger than”, b, “ and ”, c) “is bigger than”, a, “ and ”, c) “is bigger than”, a, “ 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) elif CONDITION: STATEMENT(S) else: STATEMENT(S) # PROGRAM MultiChoiceQuestion: InputValue = input("Please input your answer:\n") if InputValue == "a": # THEN print("Wrong Answer") elif InputValue == "b": # THEN print("Wrong Answer") elif InputValue == "c": # THEN print("Right Answer") elif InputValue == "d": # THEN print("Wrong Answer") else: print("Bad Option") # ENDIF; # END. Python: WHILE loop • The WHILE loop works as follows: while CONDITION: STATEMENTS # PROGRAM Print1To5: a = 1 while a != 6: # DO print(a) a = a + 1 # ENDWHILE; # END. # PROGRAM Sum1To5: 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 Print1To5For: for a in range(1,6): # DO print(a) # ENDFOR; # END. # 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: # THEN print(a, "is a prime number") else: print(a, "is not a prime number") # ENDIF; # END. # 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. ######################### # 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. ################ # Main Program # ################ # PROGRAM CheckPrime: if IsItPrime() == 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 SampleArrayProg: Age = [44, 23, 42, 33, 16, 54, 34, 18] for a in range(0,8): # DO print(Age[a]) # ENDFOR; # END. # 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. Part 1 of 3 # 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 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: # THEN last = index - 1 else: first = index + 1 # ENDIF; # ENDWHILE; Part 2 of 3 Part 3 of 3 if IsFound == False: # THEN print("Value not in array") # ENDIF; # END. # 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 TempValue = Age[index+1] Age[index+1] = Age[index] Age[index] = TempValue # ENDIF; # ENDFOR; # ENDFOR; print(Age) # END. # 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)): # DO if Age[index] < Age[MinValLocation]: # THEN MinValLocation = index # ENDIF; # ENDFOR; if MinValLocation != outerindex: Age[outerindex], Age[MinValLocation] = Age[MinValLocation], Age[outerindex] # ENDFOR; print(Age) # END. Multi-dimensional Arrays • We declare a multi-dimensional array as follows: Ages = [[0 for x in range(8)] for x in range(8)] Multi-dimensional Arrays • Or like this: Ages = [[2,6,3],[7,5,9]]