Introduction to Python

Slides:



Advertisements
Similar presentations
Control Flow Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Advertisements

Tutorial #6 PseudoCode (reprise)
Flow Charting, Structured English and PseudoCode
Introduction to Python
Chapter 2 Writing Simple Programs
Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.
Python.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
Searching Damian Gordon. Google PageRank Damian Gordon.
Xin Liu Feb 11, * Part 1 1. What value does s have, after this code is run? s = '*' s = s + s s = s + s + s (A) '**' (B) '***' (C) '****' (D) '*****'
Python: Print Damian Gordon. Your first Python program When learning a new computer programming language, the first thing typically taught is how to write.
Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Data Structures: Arrays Damian Gordon. Arrays Imagine we had to record the age of everyone in the class, we could do it declaring a variable for each.
Python: Arrays Damian Gordon. Arrays In Python arrays are sometimes called “lists” or “tuple” but we’ll stick to the more commonly used term “array”.
Modularisation Damian Gordon. Modularisation Let’s imagine we had code as follows:
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Python Conditionals chapter 5
Python: Sorting - Bubblesort Damian Gordon. Sorting: Bubblesort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows for.
Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways.
Python Let’s get started!.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
Python: Selection Damian Gordon. Python: Selection We’ll consider two ways to do selection: The IF statement The CASE statement.
Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version of the prime number checking program:
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Python: Iteration Damian Gordon. Python: Iteration We’ll consider four ways to do iteration: – The WHILE loop – The FOR loop – The DO loop – The LOOP.
Introduction to Python Damian Gordon e:
Iteration: FOR, DO, LOOP Loop Damian Gordon. FOR Loop The FOR loop does the same thing as a WHILE loop but is easier if you are using the loop to do a.
CSc 127a/110, Autumn 2016 Lecture 1: Introduction; Basic Python Programs.
Python Basics.
Chapter 2 Writing Simple Programs
Scientific Programming in Python -- Cheat Sheet
Lecture 1: Introduction; Basic Python Programs
CMSC201 Computer Science I for Majors Lecture 02 – Intro to Python
Python unit_4 review Tue/Wed, Dec 1-2
CS1022 Computer Programming & Principles
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Python Let’s get started!.
Pseudocode (Revision)
Python: Advanced Sorting Algorithms
Introduction to Programming
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Programming – syllabus knowledge
While Loops (Iteration 2)
IDLE Hints To re-load a module after making changes:
Introduction to Programming
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Introduction to Programming
Python: Algorithms Damian Gordon.
3. Decision Structures Rocky K. C. Chang 19 September 2018
Suggested Layout ** Designed to be printed on white A3 paper.
Introduction to Programming Using Python PART 2
Some Common Issues: Iteration
Conditional and iterative statements
Sorting Damian Gordon.
CISC101 Reminders All assignments are now posted.
Module 2 - Part 1 Variables, Assignment, and Data Types
Structured Programming
Some Common Issues: Print, Maths, Variables
CHAPTER 5: Control Flow Tools (if statement)
Python: Sorting – Selection Sort
Some Common Issues: Common Algorithms
Introduction to Programming
CSE 231 Lab 2.
COMPUTING.
Data Types and Expressions
Python Reserved Words Poster
Presentation transcript:

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

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

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

# 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 DivisionRemainder: print(“10 % 7 = “, 10 % 7) # END. which is what is left over when we divide 7 into 10 This should give us: 3

# 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” 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”

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

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

# 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.

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

# 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.

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

# 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.

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

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

# 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.

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: print(a, "is a prime number") else: print(a, "is not a prime number") # 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.

# 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

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

if IsFound == False: # THEN print("Value not in array") # ENDIF; # END. Part 3 of 3

# 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.

# 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.

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]]