UNIVERSITY OF TECHNOLOGY SYDNEY FACULTY OF ENGINEERING AND IT. Let's code with! DOCUMENTATION, MATERIAL, RESOURCES. (version 2)

Similar documents
Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Python Games. Session 1 By Declan Fox

Variable and Data Type I

Introduction to: Computers & Programming: Review prior to 1 st Midterm

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Introduction to Python for Plone developers

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Part III Appendices 165

Basic types and definitions. Chapter 3 of Thompson

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

18.1. CS 102 Unit 18. Python. Mark Redekopp

Variable and Data Type I

Beyond Blocks: Python Session #1

Jython. secondary. memory

Advanced Python. Executive Summary, Session 1

Python: common syntax

Introduction to Python (All the Basic Stuff)

Strings, Lists, and Sequences

Chapter 2 Input, Processing and Output. Hong Sun COSC 1436 Spring 2017 Jan 30, 2017

Semester 2, 2018: Lab 5

CSCE 110 Programming I

CS1114 Spring 2015 Test ONE ANSWER KEY. page 1 of 8 pages (counting the cover sheet)

PYTHON MOCK TEST PYTHON MOCK TEST III

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

Introduction to Python

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Scheme as implemented by Racket

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Python 1: Intro! Max Dougherty Andrew Schmitt

Python Unit

Built-in Types of Data

Accelerating Information Technology Innovation

Midterm 1 Review. Important control structures. Important things to review. Functions Loops Conditionals

Introduction to: Computers & Programming: Strings and Other Sequences

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Python Intro GIS Week 1. Jake K. Carr

Python. Executive Summary

Fundamentals of Programming (Python) Getting Started with Programming

Sequences: Strings, Lists, and Files

Introduction to Python

Programming to Python

Script language: Python Data structures

Scripting Languages. Python basics

Pace University. Fundamental Concepts of CS121 1

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

ENGR 101 Engineering Design Workshop

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

Cambridge TECHNICALS LEVEL 3

CSCA20 Worksheet Strings

Lecture #13: More Sequences and Strings. Last modified: Tue Mar 18 16:17: CS61A: Lecture #13 1

ENGR 102 Engineering Lab I - Computation

Cryptography Lesson Plan

MULTIPLE CHOICE. Chapter Seven

Key Differences Between Python and Java

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

Downloaded from Chapter 2. Functions

Lecture 12. Lists (& Sequences)

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Lesson 4: Type Conversion, Mutability, Sequence Indexing. Fundamentals of Text Processing for Linguists Na-Rae Han

UNIT 5. String Functions and Random Numbers

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Accelerating Information Technology Innovation

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

The Big Python Guide

Worksheet - Reading Guide for Keys and Passwords

Fundamentals of Programming. Strings. Anil Ada

Strings. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Python Programming Exercises 3

Flow Control: Branches and loops

CSc 110, Autumn Lecture 14: Strings. Adapted from slides by Marty Stepp and Stuart Reges

C1M0 Introduction to Maple Assignment Format C1M1 C1M1 Midn John Doe Section 1234 Beginning Maple Syntax any

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Python Tutorial. CSE 3461: Computer Networking

Lecture 3. Strings, Functions, & Modules

cs1114 REVIEW of details test closed laptop period

egrapher Language Reference Manual

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

How to Design Programs Languages

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

High Level Scripting. Gino Tosti University & INFN Perugia. 06/09/2010 SciNeGhe Data Analysis Tutorial

URLs and web servers. Server side basics. URLs and web servers (cont.) URLs and web servers (cont.) Usually when you type a URL in your browser:

Strings are actually 'objects' Strings

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

Working with Strings. Husni. "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.

Chapter 2. Python Programming for Physicists. Soon-Hyung Yook. March 31, Soon-Hyung Yook Chapter 2 March 31, / 52

Computer Science 121. Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans

An introduction to Python

for loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

CSc 110, Spring Lecture 14: Booleans and Strings. Adapted from slides by Marty Stepp and Stuart Reges

Sequence of Characters. Non-printing Characters. And Then There Is """ """ Subset of UTF-8. String Representation 6/5/2018.

DSC 201: Data Analysis & Visualization

CS116 - Module 3 - Strings and Input/Output

Strings and Testing string methods, formatting testing approaches CS GMU

The inverse of a matrix

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Transcription:

UNIVERSITY OF TECHNOLOGY SYDNEY FACULTY OF ENGINEERING AND IT Let's code with! DOCUMENTATION, MATERIAL, RESOURCES (version 2) For UTS FEIT Outreach and UTS Women in Engineering and IT Written by Albert Ong

Purpose FEIT Outreach and Women in Engineering have prioritized running engineering and IT events in high schools. Although Engineering activities have been largely successful in both student satisfaction and promoting collaboration between students, eisting IT activities have been poor in the latter. Activity Summary Let s code with Python teaches students about programming with the python language and introduces fundamental concepts in cryptography. It is targeted at high school students with little eperience in programming. Activity Layout This activity is epected to run for roughly 1 hour, and takes around 30 minutes to set up. It can be run in one of two ways, depending on the student skill level with computers: Option 1: Presentation + Eercise + Competition (Good for students with some programming background.) Duration (min) Task Description 25 Introduction to coding What is programming, and why it s important What is python? Datatypes, structures, operations. 5 Try Code Number guessing game Build eperience with the IDE 15 Cryptography Competition Problem solving race with programming using Caesar s cipher. First to decipher the code wins a prize. 3 Closure The power of IT, projects and future at UTS. Other resources to learn programming. TOTAL = 53 Minutes Option 2: Walkthrough + Competition (Good for students with little programming background, but good typing speed. This does rely on students being closer to the projector.) Duration (min) Task Description 40 Walkthrough to coding Guide through programming concepts, and do eamples in presentation slides 10 Cryptography Competition Problem solving race with programming using Caesar s cipher. First to decipher the code wins a prize. 3 Closure The power of IT, projects and future at UTS. Other resources to learn programming. TOTAL = 53 Minutes

Let's code with! Python is a general purpose programming language that uses coding synta which makes it easy to read. Today, we re going to show you two very important areas in IT, programming, and cryptography (the science of writing secret codes). 1. Get an IDE (coding environment) Either: Open http://repl.it/languages/python, or Open IEP v3.4 You should see two sections on the screen: Write code on left Click play to run code Program output on right 2. Let s try coding (It may take a minute to load the random library.) import random guesses_made = 0 number = random.randint(1, 20) name = raw_input('hello! What is your name?\n') 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)

3. Cracking Caesar s Cipher! (Work in pairs) The Caesar Cipher was one of the earliest cryptography ciphers ever invented. It s a substitution cipher that works by shifting the alphabet a specific number of letters and replaces each character. Crack this code: CDIWXCV XH IGJT TKTGNIWXCV XH ETGBXIITS Decoding steps (a brute-force cipher): 1. *Optional* Get encoded string (encoded message) from user, 2. Convert the string into an array of ASCII integers, 3. Add a number (between 0 to 25) to each ASCII number. (In order to keep the characters in the capital letter range, if the ASCII number eceeds 90, subtract 26 (so Z becomes A ), 4. Reconvert ASCII numbers back into a string. Print the string. Repeat with a different integer. ASCII Table (ASCII is shaded grey): 32 (space) 42 * 52 4 62 > 72 H 82 R 92 \ 102 f 112 p 122 z 33! 43 + 53 5 63? 73 I 83 S 93 ] 103 g 113 q 123 { 34 " 44, 54 6 64 @ 74 J 84 T 94 ^ 104 h 114 r 124 35 # 45-55 7 65 A 75 K 85 U 95 _ 105 i 115 s 125 } 36 $ 46. 56 8 66 B 76 L 86 V 96 ` 106 j 116 t 126 ~ 37 % 47 / 57 9 67 C 77 M 87 W 97 a 107 k 117 u 38 & 48 0 58 : 68 D 78 N 88 X 98 b 108 l 118 v 39 ' 49 1 59 ; 69 E 79 O 89 Y 99 c 109 m 119 w 40 ( 50 2 60 < 70 F 80 P 90 Z 100 d 110 n 120 41 ) 51 3 61 = 71 G 81 Q 91 [ 101 e 111 o 121 y

Python Reference (from http://cscircles.cemc.uwaterloo.ca/cheatsheet/): 3 * 4, 3 + 4, 3-4, 3 / 4 #==> 12, 7, -1, 0.75 3 ** 4, 3 // 4, 3 % 4 #==> 81, 0, 3 4 > 3, 4 >= 3, 3 == 3.0, 3!= 4, 3 <= 4 #==> True, True, True, True, True # order of operations: brackets, **, {* / // %}, {+ -}, {==!= <= < > >=} min(3, 4), ma(3, 4), abs(-10) #==> 3, 4, 10 sum([1, 2, 3]) # [1, 2, 3] is a list #==> 6 type(3), type(3.0), type("myvariable") int("4"+"0"), float(3), str(1 / 2) "double quotes: ', escaped \" \\ \'" 'it\'s "similar" in single quotes ' #==> class 'int', class 'float', # class 'str' #==> 40, 3.0, '0.5' #==> double quotes: ', escaped " \ ' #==> it's "similar" in single quotes ord("a"), chr(66) #==> 65, 'B' string = "hello" # the following statements work for lists too len(string) #==> 5 string[0], string[4] # get characters #==> "h", "o" string[1:3] # get a substring #==> "el" string[:2], string[2:] # l/r substrings #==> "he", "llo" string[-1], string[-2:] # negative indices#==> "o", "lo" "con" + "cat" + "enation " + str(123) #==> "concatenation 123" "boo" * 2 #==> "booboo" getlineofinputasstring = input() #==> read input (or EOF error) print("takes", 0, "or more arguments") #==> takes 0 or more arguments print("using", "custom", "sep", sep=".") #==> using.custom.sep print("no", "newline", end="bye") #==> no newlinebye not True, False or True, False and True #==> False, True, False # order of operations: brackets, {==!=}, not, and, or if booleancondition: elif anothercondition: else: while booleancondition: break continue # indent the body block # every line by the same amount # can do zero, one, or several elif blocks # multi-line block # optional # multi-line block # the body block # jump out (optional) # restart from top of net iteration (optional) for indevariable in range(low, highplus): print(indevariable) #==> low, low+1,..., highplus-1 # "for item in listorstring:" forall/foreach loops # break, continue can be used in for loops def nameofnewfunction(argument1, argument2): # the body block return y # (optional; if you don't return, it returns None) def remember(bar): # writing to global variables global savebar # after calling foo(3), savebar = 3 savebar = bar # even outside of the function's scope (CONTINUED ON NEXT PAGE)

# these 'slice' commands have analogues for lists and range() "0123456789"[::2] # slices #==> "02468" "0123456789"[::-1] # descending #==> "9876543210" "0123456789"[6:3:-1] #==> "654" += 1 # also -=, /=, *=, %=, **=, //=. Python has no C++-style "++", y = y, # multiple assignment 3 < < 5 # same as "(3 < ) and ( < 5)". can chain {< <= > >= ==!= is} import math # import, to get everything with period print(math.sqrt(2)) from math import sqrt # import, to get one thing without period print(sqrt(2)) # also in math module: pi, ep, log, sin, cos, tan, ceil, floor, and more list = ['zero', 'one', 'two'] list.inde('one') #==> 1 list.inde('three') #==> causes an error 'three' in list, 'zero' in list #==> False, True list.count('two') #==> 1 del list[1] # list becomes ['zero', 'two'] "string" in "superstring" #==> True "superstring".inde("string") #==> 5 # more list methods: append(item), insert(item, inde), etend(list), # remove(value), pop(), pop(inde), reverse(), sort(), and more # some string methods: capitalize(), lower/upper(), islower/isupper(), # isalpha/isdigit(), center/ljust/rjust(width, fillchar), strip(), split(), # splitlines(), endswith/startswith(string), find(string), replace(old, new), # and more mylist = [11, 99] actuallythesamelist = mylist # not a true copy! just copies the reference mylist is actuallythesamelist #==> True realcopy = mylist[:] # or list(mylist), copy.copy(mylist), deepcopy realcopy is mylist #==> False There are a bunch of resources available for learning software development and IT: codeavengers.com Learnpython.org csedweek.org/learn codeacademy.com groklearning.com/hoc ed.org hello.processing.org robomindacademy.com dreamspark.com code.org