126
Act like a code monkey Coding Basics

Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Act like a code monkey

Coding Basics

Page 2: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Announcement

� Homework 1 grade is posted� If you believe there is an error in grading (assignments or quizzes),

you may request a regrading within one week of receiving your grade. Requests must be made in writing, explaining clearly to the TA why you think your solution is correct

Page 3: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Base-3 System

Page 4: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Base-30, 1, 2

Page 5: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

1 2 5 100 = 10^2 10 = 10^1 1 = 10^0

1x(10^2) + 2x(10^1) + 5x(10^0) = 125

Decimal

Page 6: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

9 7 6 10^2 10^1 10^0

9x(10^2) + 7x(10^1) + 6x(10^0) = 976

Decimal

Page 7: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

3 4 5 10^2 10^1 10^0

3x(10^2) + 4x(10^1) + 5x(10^0) = 345

Decimal

Page 8: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

2 1 1 10^2 10^1 10^0

2x(10^2) + 1x(10^1) + 1x(10^0) = 211

Decimal

Page 9: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

2 1 1 9 = 3^2 3 = 3^1 1 = 3^0

2x(3^2) + 1x(3^1) + 1x(3^0) = 22 (decimal)

base-3

Page 10: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

2 2 0 9 = 3^2 3 = 3^1 1 = 3^0

2x(3^2) + 2x(3^1) + 0x(3^0) = 24 (decimal)

base-3

Page 11: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

1 0 2 3^2 3^1 3^0

1x(3^2) + 0x(3^1) + 2x(3^0) = 11 (decimal)

base-3

Page 12: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Base-4 System

Page 13: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Base-40, 1, 2,3

Page 14: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

1 0 2 4^2 4^1 4^0

1x(4^2) + 0x(4^1) + 2x(4^0) = 18 (decimal)

base-4

Page 15: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Think beyond Binary

Page 16: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Quantum Computing

� Quantum computers: use quantum-mechanical phenomena to perform operations on data

� Different from digital electronic computers based on transistors.� Uses quantum bits (qubits), which can be in superpositions of states: e.g.

linear combination of basic states of particles� Quantum superposition: any 2+ quantum states can be added together

and the result will be another valid quantum state� Quantum Turing machine� Non-deterministic and probabilistic� Paul Benioff, Yuri Manin 1980; Richard Feynman 1982; David Deutsch 1985.� Further reading: https://en.wikipedia.org/wiki/Quantum_computing

Page 17: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Quantum Computing

� A quantum bit corresponds to a single electron in a particular state. Using the trajectories of an electron through two closely spaced channels for encoding.

� In principle, 2 different states are possible: the electron either moves in the upper channel or in the lower channel – a binary system.

� However, a particle can be in several states simultaneously, that is, it can quasi fly through both channels at the same time.

� These overlapping states can form an extensive alphabet of data processing.

� Quantum computer science� Further reading: http://qist.lanl.gov/qcomp_map.shtml� http://www.webpronews.com/quantum-computing-beyond-binary-

2012-03/

Page 18: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Problem Solving

Page 19: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Problem solving

formulate problem

think creatively

about solutions

express a solution clearly &

accurately

Page 20: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Learn to program = Learn to solve problems

Page 21: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Python

Programming

Language

Credit: lecture notes modeled after http://www.openbookproject.net/thinkcs/python/english2e/index.html

Page 22: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

� high-level language, like C++, JAVA� Different from low-level language like assembly language that has

strong relation to machine code� Easier, more efficient to write� More likely to be correct, portable

Python

Page 23: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

typically considered as an interpreted language

source code interpreter output

read line by line perform computation

Page 24: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Shell Mode in python shell

$ pythonPython 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> print 1+23

Page 25: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

$ python myprogram.py3

Used primarily for this classfor programs more than a few lines

myprogram.pyprint 1+2

Script Mode

Page 26: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

http://www.tutorialspoint.com/execute_python_online.phphttp://www.skulpt.org/http://www.pythontutor.com/index.html

https://repl.it/languages/python3 (note, this is Python 3, some syntax rule is more strict)

Making life easier

We use online Python interpreters

Page 27: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Coding Basics

Page 28: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

What is a program?

Page 29: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

A sequence of instructions that specifies how to perform a

computation

Page 30: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Basic Instructions of a program

Page 31: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Input: get data from the keyboard, a file, or some other device

Page 32: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Output: display data on the screen or send data to a file or other device

Page 33: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Math: perform basic math operations like additions and multiplication

Page 34: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Conditional execution: check for certain conditions and execute the appropriate sequence of statements

Page 35: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Repetition: perform some action repeatedly, usually with some variation

Page 36: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

That’s all need to know about a program!

Page 37: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

programming = problem solving

Page 38: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Breaking a large, complex task to smaller and smaller subtasks, until the

subtasks are simple enough to be performed by some basic instructions...

Page 39: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

What is debugging?

Page 40: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Grace Hopper First actual case of bug (moth)“Amazing Grace”

Page 41: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Credit: Clip art image by Cliparts.co

Page 42: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

bug = programming error debugging = track down the bugs and fix them

Page 43: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Type of bugs� Syntax error: violation of rules and structures. For example, a

sentence has to start with a capital letter...� Runtime error: error does not appear until program is executed

(rare for now)� Semantic error: the program is not doing what you tell it to do.

Tricky to track down.

Page 44: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

programming = experimental debugging, detective work, hypothesis testing, etc.

Page 45: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

“When you have eliminated the

impossible, whatever remains, however improbable, must be the truth.

-- Sherlock Holmes

Page 46: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Debugging can be intellectually challenging and rewarding

Page 47: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Python is a formal language used to express computations

Page 48: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Tokens: words, numbers, etc.Statements: arrangements or structures of tokens

Page 49: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

1st Python Lab

Credit: https://pixabay.com/static/uploads/photo/2014/04/02/14/05/snake-306109_960_720.png

Page 50: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

print “Hello World!!!”

Page 51: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Hello World!!!

Page 52: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

print “What happened to Han Solo?!”

Page 53: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

What happened to Han Solo?!

Page 54: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

print “My name is Harrison Ford.”

your name goes here!

Page 55: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

My name is Harrison Ford.

Page 56: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

print Star Wars

Page 57: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

File "<stdin>", line 1 print Star Wars ^SyntaxError: invalid syntax

� Runtime Error� Need quotation

marks “ ” or ‘ ’

Page 58: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Values and Data Types

Page 59: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

A value is something a program manipulates

� a letter “a”� a number 2� a sentence “hello world!”

Page 60: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

There are different types of values� 2 is an integer � “Hello World” is a string� 3.1416926 is a floating point (real # or

approximation of real #)

Page 61: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

2nd Python Lab

Page 62: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

print 5

Page 63: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

5

Page 64: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

print 100000

Page 65: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

1000000

Page 66: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

print 100,000

Page 67: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

100 0

Treating 100,000 as a list of 2 items

Page 68: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

type(“hello world!”)

Page 69: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

<type 'str'>

Page 70: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

type(2)

Page 71: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

<type 'int'>

Page 72: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

type(“2”)

Page 73: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

<type 'str'>

Page 74: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

type(3.1415926)

Page 75: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

<type 'float'>

Page 76: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

What is a variable?

Page 77: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

A name that refers to a value

Page 78: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Assignment statement: creates new variables and gives them values

Page 79: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

message = “Orange is the new black.”

Page 80: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

n = 28

Page 81: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

pi = 3.1416926

Page 82: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

print messageprint nprint pi

Page 83: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Orange is the new black.283.1415926

Page 84: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

type(message)type(n)type(pi)

Page 85: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

<type 'str'><type 'int'><type 'float'>

Page 86: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

28 = n

Page 87: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

File "<stdin>", line 1SyntaxError: can't assign to literal

Page 88: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Choose meaningful variable names� What = “hello” (not so good)� Begin with letters, contain letters and numbers and “_”� Typically use lowercase letters� Python keywords (describe rules and structures) can’t be variables

Page 89: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

3nd Python Lab

Page 90: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

321name = “Tom”

Page 91: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

File "<stdin>", line 1 321name = “Tom” ^SyntaxError: invalid syntax

Page 92: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

ineedmoney$ = 100

Page 93: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

File "<stdin>", line 1 ineedmoney$ = 100 ^SyntaxError: invalid syntax

Page 94: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

class = 1999

Page 95: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

File "<stdin>", line 1 class = 1999 ^SyntaxError: invalid syntax

Page 96: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

What is a statement?

Page 97: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

An instruction that the Python interpreter can execute

Page 98: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Examples:print statement

assignment statement

Page 99: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Python executes a statement and display the results (if any)

Page 100: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Result of a print statement is a valueAssignment statement produces no

output

Page 101: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

A script/program contains a set of statements, results appear one at a time

Page 102: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

print “a”x = 28print x

Page 103: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

a28

Page 104: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

What is an expression?

Page 105: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

A combination of values, variables and operators

Page 106: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Evaluating an expression by an interpreter produces a value

Page 107: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

>>>1+12>>>2828

Shell Mode (command line)

Page 108: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

>>>message='what is up?'>>>message'what is up?'>>>print messagewhat is up?

Shell Mode (command line)

#print statement print values

Page 109: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

What is an operator?

Page 110: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Operators are special symbols representing computations, such as

addition and multiplication

Page 111: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Operators use operands

Page 112: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

+, -, *, / (integer division)** exponentiation

Page 113: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

11+22hour*60+minute

minute/603**2+3**1

(3**2)+(3**1)

Page 114: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Order of operations( )***, /

Page 115: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

2**2+1 #(2**2)+13*1**3 #3*(1**3)

#comments

Page 116: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

>>> 2**2+15>>> 3*1**33

Shell Mode (command line)

Page 117: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Operations on Strings

Page 118: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

>>> fruit = "apple">>> bakedgood = "pie">>> print fruit+" "+bakedgoodapple pie

Shell Mode (command line)

Page 119: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Input

Page 120: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

>>>n = input("Enter a numerical expression ")Enter a numerical expression 1+3>>> print n4

Shell Mode (command line)

Page 121: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

>>> n = raw_input("Enter a numerical expression ")Enter a numerical expression 1+3>>> print n1+3

Shell Mode (command line)

raw_input() is replaced by input() for Python 3.*

Page 122: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Combination of statements

Page 123: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

>>> print "3+2+1", "is equal to ", 63+2+1 is equal to 6>>>

Shell Mode (command line)

raw_input() is replaced by input() for Python 3.*

Page 124: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Play with Python

labs on your own!

Page 125: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

thanks!

Any questions?

You can find me [email protected]

http://www.sci.utah.edu/~beiwang/teaching/cs1060.html

Page 126: Act like a code monkey Coding Basicsbeiwang/teaching/cs1060/Lecture06-CodingBasics.pdfThere are different types of values 2 is an integer “Hello World” is a string ... Python executes

Credits

Special thanks to all the people who made and released these awesome resources for free:� Presentation template by SlidesCarnival� Photographs by Unsplash