LET'S CODE WITH - University Of Technology Sydney

Transcription

v2LET’S CODE WITHUTS:INFORMATIONTECHNOLOGYUTS CRICOS PROVIDER CODE: 00099Fit.uts.edu.au

ACTIVITY OVERVIEW What is programming and the Python language? Programming with Python Send/get data to/from users How we store data (data types, arrays) How we use data: Logical operations Decision making loops (if, while, for) Translating data Cryptography – the art of code cracking Decode Caesar’s cipher!it.uts.edu.au

WHAT IS PROGRAMMING AND PYTHON? Programming is a stage of Software Development Code is a list of instructions that tells the computer what to do.(Computers are like really dumb 3-year-olds) Comes in a variety of languages for a range of applications.(java, C, C , C#, XAML, ruby, python, and so many more!) Python is a programming language Designed to be easy to read/write. Does not need to be told when lines end.(Many code languages use ; to mark line ends)“If you knowone language,you can easilylearn another.” Is sensitive to white space and is caps sensitive.(Be careful with “tab” and “caps lock” buttons)it.uts.edu.au

CODING IN PYTHON – GETTING ANDSENDING INFO TO THE USERIn programming, data is stored in variables Set values to variables Eg. X 4This stores the value “4” into a variable named “X” Send info to the user – print() print types the info in the brackets to the screenCODE:OUTPUT:it.uts.edu.au

CODING IN PYTHON – GETTING ANDSENDING INFO TO THE USER Getting info from the user:1. input() ask for a number.2. raw input() treats the input as text.EXAMPLE:EXAMPLE:OUTPUT:OUTPUT:Try this:What happens when you inputcharacters when using input()?it.uts.edu.au

CODING IN PYTHON – DATA TYPES Data types – The type of data that can be stored invariables:1. Characters – a, b, c,.2. Integers (whole numbers) – 1, 2, 3, 0, -1, 100, etc 3. Floats / doubles (decimals) – 1.09, 2.0003, etc 4. Binary logic – TRUE or FALSEBut wait! What about words and sentences?We use “Lists”.it.uts.edu.au

CODING IN PYTHON – DATA TYPES Lists Grid of data (can be of one type or different types, usually 1-D) Words and sentences are a unique type.We call these Strings (List of characters) . Example: the string ‘Hello!’ is stored as:Element012345Value (char)Hello!CODE (convert to List):OUTPUT:it.uts.edu.au

CODING IN PYTHON – PROCESS DATA Now we know how data is stored, we need toprocess it.1. Mathematical and Logical operations - Add, subtract, multiply, divide,and remainder (what’s left over after dividing)2. Decision making/iteration loops – come in three types: if, else, elif (else if) – yes/no decisions while – if something hasn’t happened yet, repeat. for – combination of if and while3. Converting between data types Strings into array of letters ASCII conversion for lettersit.uts.edu.au

CODING IN PYTHON – PROCESS DATA1. Math operationsAllows us to manipulate numerical data.EXAMPLE:OUTPUT:2. Logic operationsAllows us to compare data.EitherTRUE orFALSEComparisonIn code:AddA BSubtractA-BMultiplyA*BDivideA/ BRemainder after divisionA%BComparisonIn code:Equal toA BNot equal toA ! BGreater thanA BLess thanA BGreater than or equal toA BLess than or equal toA Bit.uts.edu.au

CODING IN PYTHON – PROCESS DATADecision Making Loops – “fork in the road”1. if, else and elifIf the conditions are TRUE,do something.Otherwise do somethingelse. (elif means else if)2. whileWhile the conditions are true,keep repeating untilconditions are false.3. forRepeat instructions a specificnumber of times.Loops insides loops are “nested”.Note: stores the value into the variable. checks if one thing equals another.it.uts.edu.au

LET’S TRY CODING:NUMBER GUESSING GAME(Code is in your handout)import randomguesses made 0name raw input('Hello! What is your name?\n')number random.randint(1, 20)print 'Well, {0}, I am thinking of a number between 1 and 20.'.format(name)while guesses made 6:guess int(raw input('Take a guess: '))guesses made 1if guess number:print 'Your guess is too low.'if guess number:print 'Your guess is too high.'if guess number:breakif guess number:print 'Good job, {0}! You guessed my number in {1} guesses!'.format(name, guesses made)else:print 'Nope. The number I was thinking of was {0}'.format(number)it.uts.edu.au

CODING IN PYTHON – PROCESS DATABecause the computer treats Strings and Lists as different data types(even though they are conceptually the same), we need to convertbetween the two types. Convert String into a List of Characterss "foobar"l list(s)print l['f', 'o', 'o', 'b', 'a', 'r'] Convert a Character List into a Stringl ["H", "e", "l", "l", "o"]s "".join(l)print sHelloit.uts.edu.au

CODING IN PYTHON – PROCESS DATAYou can’t do math with letters (“a” “b” ?), but we canconvert them into ASCII.(ASCII –integers representing all the characters on a keyboard) Convert a string into an array of ASCII numberss 'hi'[104, 105]L [ord(c) for c in s]print L Convert an array of ASCII numbers into a stringSTACKl [83, 84, 65, 67, 75]What’s thes "".join([chr(c) for c in l])Output?print sit.uts.edu.au

CRYPTOGRAPHY - CAESAR’S CIPHERCryptography: The science of encoding or decodingsecret messages using Ciphers.(Ciphers – algorithms that encrypt or decrypt data.) Caesar’s Cipher One of the oldest cryptography systems in the world. Is a Substitution Cipher - Works by shifting the alphabet across aspecific number of letters (the number of letters shifted is the unknown):it.uts.edu.au

COMPETITION – CRACKING THE CODEYour task: Design a Cipher to crack the code:CDIWXCV XH IGJT TKTGNIWXCV XH ETGBXIITS Work in teams!(I.T. always works in teams.) First team to win gets a lovely prize. Use any means at your disposal to crack the code. Time limit: 10 minutes.it.uts.edu.au

MY SOLUTION – BRUTE FORCE METHODBrute-force: Finds all possible decryptionsolutions, and pick the one that works.Remember:Z 90 in ASCIIit.uts.edu.au

OTHER RESOURCES TO LEARNPROGRAMMING There are a range of online sources to learn programming: Learnpython.org csedweek.org/learn codeacademy.com groklearning.com/hoc edx.org – Massachusetts Institute of Technology hello.processing.org robomindacademy.com – virtual robot programming with python dreamspark.com – free Microsoft IDE for a range of languages code.org Check the back of your handout for a list! it.uts.edu.au

INFORMATION TECHNOLOGY Information technology and software iseverywhere. We live with it everyday.Mobile devicesComputersArtificial IntelligenceInternet and AppsRoboticsMedical devicesBioinformaticsCloud computingInternet of ThingsActuarial analysisSimulationInvestment & Finance just to name a fewit.uts.edu.au

FIND US AT:FEIT.UTS.EDU.AUUTS:INFORMATIONTECHNOLOGYUTS CRICOS PROVIDER CODE: 00099Fit.uts.edu.au

CODING IN PYTHON - GETTING AND SENDING INFO TO THE USER In programming, data is stored in variables Set values to variables Eg. X 4 This stores the value "4" into a variable named "X" Send info to the user - print() print types the info in the brackets to the screen it.uts.edu.au CODE: OUTPUT: