Act like a code monkey Coding Basics

Similar documents
Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Variables, expressions and statements

Algorithms and Programming I. Lecture#12 Spring 2015

An Introduction to Python (TEJ3M & TEJ4M)

Getting Started with Python

Fundamentals of Programming (Python) Getting Started with Programming

How to Think Like a Computer Scientist: Learning with Python 3»

CS 1301 Exam 1 Answers Fall 2009

CS 1301 Exam 1 Fall 2009

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Expressions, Statements, Variables, Assignments, Types

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Python Programming Exercises 1

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

PROGRAMMING FUNDAMENTALS

CS1110 Lab 1 (Jan 27-28, 2015)

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

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

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

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS First Name: Last Name: NetID:

Lecture 01 & 02 Computer Programming

Practical Programming, 2nd Edition

CMSC 201 Computer Science I for Majors

Full file at

Python for Informatics

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Computer Components. Software{ User Programs. Operating System. Hardware

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Computer Components. Software{ User Programs. Operating System. Hardware

How to Think Like a Computer Scientist: Learning with Python 3 Documentation

Chapter 2 Basic Elements of C++

Full file at

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

CSC326 Python Imperative Core (Lec 2)

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

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

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Programming Fundamentals and Python

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

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

Python for Bioinformatics Fall 2014

Python Input, output and variables

More Social Networks Limitations and Potential of computation

Introduction to Python

PROGRAMMING FUNDAMENTALS

1/11/2010 Topic 2: Introduction to Programming 1 1

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

More Coding Basics with Some Turtle

T H E I N T E R A C T I V E S H E L L

CS1 Lecture 3 Jan. 22, 2018

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

Topic 2: Introduction to Programming

Chapter 2 Working with Data Types and Operators

Invent Your Own Computer Games with Python

ENGR 101 Engineering Design Workshop

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

CSC 015: FUNDAMENTALS OF COMPUTER SCIENCE I

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

6.096 Introduction to C++ January (IAP) 2009

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

Introduction to Programming in Turing. Input, Output, and Variables

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

CS-201 Introduction to Programming with Java

Lecture 1. Course Overview, Python Basics

CSE 115. Introduction to Computer Science I

CS1 Lecture 3 Jan. 18, 2019

Variables, Expressions, and Statements

Getting Started Values, Expressions, and Statements CS GMU

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Introduction to computers and Python. Matthieu Choplin

Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

a name refers to an object side effect of assigning composite objects

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

More on Lists.

Lecture 2: Python Arithmetic

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

CSC108: Introduction to Computer Programming. Lecture 1

Lab 1: Input, Processing, and Output This lab accompanies Chapter 2 of Starting Out with Programming Logic & Design.

MEIN 50010: Python Introduction

Lecture 1. Types, Expressions, & Variables

Curriculum Map Grade(s): Subject: AP Computer Science

DaMPL. Language Reference Manual. Henrique Grando

Computing with Numbers

Variables and Constants

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

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

CS1004: Intro to CS in Java, Spring 2005

ENGR 102 Engineering Lab I - Computation

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Practical Programming, Third Edition

Transcription:

Act like a code monkey Coding Basics

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

Base-3 System

Base-3 0, 1, 2

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

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

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

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

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

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

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

Base-4 System

Base-4 0, 1, 2,3

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

Think beyond Binary

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

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/

Problem Solving

Problem solving formulate problem think creatively about solutions express a solution clearly & accurately

Learn to program = Learn to solve problems

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

Python 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

typically considered as an interpreted language read line by line perform computation source code interpreter output

Shell Mode in python shell $ python Python 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 darwin Type "help", "copyright", "credits" or "license" for more information. >>> print 1+2 3

Script Mode $ python myprogram.py 3 Used primarily for this class for programs more than a few lines myprogram.py print 1+2

We use online Python interpreters Making life easier http://www.tutorialspoint.com/execute_python_online.php http://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)

Coding Basics

What is a program?

A sequence of instructions that specifies how to perform a computation

Basic Instructions of a program

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

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

Math: perform basic math operations like additions and multiplication

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

Repetition: perform some action repeatedly, usually with some variation

That s all need to know about a program!

programming = problem solving

Breaking a large, complex task to smaller and smaller subtasks, until the subtasks are simple enough to be performed by some basic instructions...

What is debugging?

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

Credit: Clip art image by Cliparts.co

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

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.

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

When you have eliminated the impossible, whatever remains, however improbable, must be the truth. -- Sherlock Holmes

Debugging can be intellectually challenging and rewarding

Python is a formal language used to express computations

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

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

print Hello World!!!

Hello World!!!

print What happened to Han Solo?!

What happened to Han Solo?!

print My name is Harrison Ford. your name goes here!

My name is Harrison Ford.

print Star Wars

File "<stdin>", line 1 print Star Wars ^ SyntaxError: invalid syntax Runtime Error Need quotation marks or

Values and Data Types

A value is something a program manipulates a letter a a number 2 a sentence hello world!

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

2nd Python Lab

print 5

5

print 100000

1000000

print 100,000

Treating 100,000 as a list of 2 items 100 0

type( hello world! )

<type 'str'>

type(2)

<type 'int'>

type( 2 )

<type 'str'>

type(3.1415926)

<type 'float'>

What is a variable?

A name that refers to a value

Assignment statement: creates new variables and gives them values

message = Orange is the new black.

n = 28

pi = 3.1416926

print message print n print pi

Orange is the new black. 28 3.1415926

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

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

28 = n

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

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

3nd Python Lab

321name = Tom

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

ineedmoney$ = 100

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

class = 1999

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

What is a statement?

An instruction that the Python interpreter can execute

Examples: print statement assignment statement

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

Result of a print statement is a value Assignment statement produces no output

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

print a x = 28 print x

a 28

What is an expression?

A combination of values, variables and operators

Evaluating an expression by an interpreter produces a value

Shell Mode (command line) >>>1+1 2 >>>28 28

Shell Mode (command line) >>>message='what is up?' >>>message 'what is up?' >>>print message what is up? #print statement print values

What is an operator?

Operators are special symbols representing computations, such as addition and multiplication

Operators use operands

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

11+22 hour*60+minute minute/60 3**2+3**1 (3**2)+(3**1)

Order of operations ( ) ** *, /

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

Shell Mode (command line) >>> 2**2+1 5 >>> 3*1**3 3

Operations on Strings

Shell Mode (command line) >>> fruit = "apple" >>> bakedgood = "pie" >>> print fruit+" "+bakedgood apple pie

Input

Shell Mode (command line) >>>n = input("enter a numerical expression ") Enter a numerical expression 1+3 >>> print n 4

Shell Mode (command line) >>> n = raw_input("enter a numerical expression ") Enter a numerical expression 1+3 >>> print n 1+3 raw_input() is replaced by input() for Python 3.*

Combination of statements

Shell Mode (command line) >>> print "3+2+1", "is equal to ", 6 3+2+1 is equal to 6 >>> raw_input() is replaced by input() for Python 3.*

Play with Python labs on your own!

thanks! Any questions? You can find me at beiwang@sci.utah.edu http://www.sci.utah.edu/~beiwang/teaching/cs1060.html

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