let’s code with - home | university of technology sydney · let’s code with it.uts.edu.au v2 ....

19
UTS: INFORMATION TECHNOLOGY UTS CRICOS PROVIDER CODE: 00099F LET’S CODE WITH it.uts.edu.au v2

Upload: vandiep

Post on 28-May-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

UTS: INFORMATION TECHNOLOGY

UTS CRICOS PROVIDER CODE: 00099F

LET’S CODE WITH

it.uts.edu.au

v2

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)

• Is sensitive to white space and is caps sensitive. (Be careful with “tab” and “caps lock” buttons)

it.uts.edu.au

“If you know one language, you can easily learn another.”

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:

CODING IN PYTHON – GETTING AND SENDING INFO TO THE USER Getting info from the user:

it.uts.edu.au

2. raw_input() treats the input as text.

EXAMPLE: OUTPUT:

1. input() ask for a number. EXAMPLE: OUTPUT:

Try this: What happens when you input characters when using input()?

CODING IN PYTHON – DATA TYPES

• Data types – The type of data that can be stored in variables:

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 FALSE

it.uts.edu.au

But wait! What about words and sentences?

We use “Lists”.

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:

it.uts.edu.au

Element 0 1 2 3 4 5 Value (char) H e l l o !

CODE (convert to List): OUTPUT:

CODING IN PYTHON – PROCESS DATA

• Now we know how data is stored, we need to process 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 while

3. Converting between data types

• Strings into array of letters

• ASCII conversion for letters

it.uts.edu.au

CODING IN PYTHON – PROCESS DATA

1. Math operations Allows us to manipulate numerical data.

EXAMPLE: OUTPUT:

2. Logic operations Allows us to compare data.

it.uts.edu.au

Comparison In code:

Add A + B

Subtract A - B

Multiply A * B

Divide A / B

Remainder after division A % B

Comparison In code:

Equal to A == B

Not equal to A != B

Greater than A < B

Less than A > B

Greater than or equal to A >= B

Less than or equal to A <= B

Either TRUE or FALSE

CODING IN PYTHON – PROCESS DATA

Decision Making Loops – “fork in the road”

it.uts.edu.au

Note: = stores the value into the variable. == checks if one thing equals another.

1. if, else and elif If the conditions are TRUE, do something. Otherwise do something else. (elif means else if)

2. while

While the conditions are true, keep repeating until conditions are false.

3. for Repeat instructions a specific number of times.

Loops insides loops are “nested”.

LET’S TRY CODING: NUMBER GUESSING GAME

it.uts.edu.au

import random guesses_made = 0 name = 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 += 1 if guess < number: print 'Your guess is too low.' if guess > number: print 'Your guess is too high.' if guess == number: break if 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)

(Code is in your handout)

Because the computer treats Strings and Lists as different data types (even though they are conceptually the same), we need to convert between the two types.

CODING IN PYTHON – PROCESS DATA

• Convert String into a List of Characters

• Convert a Character List into a String

it.uts.edu.au

l = ["H", "e", "l", "l", "o"] s = "".join(l) print s

Hello

s = "foobar" l = list(s) print l

['f', 'o', 'o', 'b', 'a', 'r']

What’s the Output?

CODING IN PYTHON – PROCESS DATA

You can’t do math with letters (“a”+“b”=??), but we can convert them into ASCII.

(ASCII –integers representing all the characters on a keyboard)

• Convert a string into an array of ASCII numbers

• Convert an array of ASCII numbers into a string

it.uts.edu.au

l = [83, 84, 65, 67, 75] s = "".join([chr(c) for c in l]) print s

s = 'hi' L = [ord(c) for c in s] print L

[104, 105]

STACK

Cryptography: The science of encoding or decoding secret messages using Ciphers.

(Ciphers – algorithms that encrypt or decrypt data.)

CRYPTOGRAPHY - CAESAR’S CIPHER

• Caesar’s Cipher • One of the oldest cryptography systems in the world.

• Is a Substitution Cipher - Works by shifting the alphabet across a specific number of letters (the number of letters shifted is the unknown):

it.uts.edu.au

COMPETITION – CRACKING THE CODE

Your task: Design a Cipher to crack the code:

• 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

CDIWXCV XH IGJT TKTGNIWXCV XH ETGBXIITS

MY SOLUTION – BRUTE FORCE METHOD

it.uts.edu.au

Brute-force: Finds all possible decryption solutions, and pick the one that works.

Remember: Z = 90 in ASCII

OTHER RESOURCES TO LEARN PROGRAMMING • 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 is everywhere.

• We live with it everyday.

it.uts.edu.au

Mobile devices Computers Artificial Intelligence Internet and Apps Robotics Medical devices Bioinformatics Cloud computing Internet of Things Actuarial analysis Simulation Investment & Finance …just to name a few

UTS: INFORMATION TECHNOLOGY

UTS CRICOS PROVIDER CODE: 00099F

FIND US AT: FEIT.UTS.EDU.AU

it.uts.edu.au