Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.

Slides:



Advertisements
Similar presentations
Python Basics: Statements Expressions Loops Strings Functions.
Advertisements

Computer Programming w/ Eng. Applications
CSC 110 Writing simple programs [Reading: chapter 2] CSC 110 C 1.
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Variables and I/O. Types Strings –Enclosed in quotation marks –“Hello, World!” Integers –4, 3, 5, 65 Floats –4.5, 0.7 What about “56”?
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Introduction to C Programming
Lecture 2 - Variables, program execution, calculations, print() COMPSCI 101 Principles of Programming.
A First Book of ANSI C Fourth Edition
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Introduction to Python
Munster Programming Training
Fundamentals of Python: First Programs
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.
Introduction to Computational Linguistics Programming I.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Variables and Expressions CMSC 201 Chang (rev )
Week 1 Algorithmization and Programming Languages.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Built-in Data Structures in Python An Introduction.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Variables, Expressions and Statements
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Variables, Types, Expressions Intro2CS – week 1b 1.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
C++ First Steps.
Topics Designing a Program Input, Processing, and Output
Python: Experiencing IDLE, writing simple programs
Topic: Python’s building blocks -> Statements
Lecture 24: print revisited, tuples cont.
Variables, Expressions, and IO
Introduction to C++ Programming
CS111 Computer Programming
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
COMPUTER PROGRAMMING SKILLS
CISC101 Reminders Assignment 3 due today.
Introduction to C Programming
Chapter 2 Modular Programs with Calculations and Strings
Introduction to Python
Presentation transcript:

Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University

Values, data types, variables Use type() to learn type of a value Variable: name that refers to a value The assignment statement creates new variables and gives them values Motto in assignment statements: evaluate right hand side, and then assign to the value of left hand side Example values Data types 12int 12.1float 1 + 3jcomplex “hello”str [1, 2, 34.5, ‘a’]list (1,4,’a’)tuple >>> a = 3 >>> a 3 >>> my_str= ‘hello’ >>> my_str ‘hello’ >>> _list1 = [1,2,3] >>> _list1 [1,2,3] >>> a,b,c = 1,2,3 # simultaneous assignment is allowed Check its use in swapping variable values!

Valid identifier names Module names, function names, variable names are called identifiers. Valid identifier names should comply to the following rules: – starts either with a letter or an underscore( ‘_’) – it may contain letters, digits and underscore in them. They are case sensitive. Here are examples of valid identifier names: – a – abc – my_sum – _sum – ___ – sumOf2_digits

Arithmetic operators Addition: + Multiplication: * Subtraction: - (Floating point) Division: / (Integer) Division: // Exponentiation: ** Mod: % What values they can operate on? Numerical values, strings? What is the resulting value and data type given the operand types? Which operation takes place first? (Operator precedence) When equal precedence, do we start evaluating from left or right? (Order of operation)

Type conversions, rounding int() float() implicit type conversion math.floor() round()

Expressions, statements An expression is a combination of values, variables, and operators. Syntactically correct expressions are evaluated by the interpreter to a single value. A statement is an instruction that the Python interpreter can execute. It is a complete command. ExpressionsValue (it evaluates to)Data type of the value 1+2/22int “a”*4 + ’bb’‘aaaabb’str [1, 2, 3] list 1, 4.5, 3/2(1, 4.5, 1.5)tuple

print() print() is a built-in function that displays values. >>> help(print) Help on built-in function print in module builtins: print(...) print(value,..., sep=' ', end='\n', file=sys.stdout) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline.

input() input() is a built-in function that prompts user for input and returns the entered string. >>> help(input) Help on built-in function input in module builtins: input(...) input([prompt]) -> string Read a string from standard input. The trailing newline is stripped. If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError. On Unix, GNU readline is used if enabled. The prompt string, if given, is printed without a trailing newline before reading.

eval() eval() takes an argument of string value, evaluates it and returns the resultant numeric value. >>> eval(“123”) 123 >>> eval(“1 + 2*3”) 7 >>> eval(“’1’*2”) ‘11’ Use eval() with input() to evaluate user input >>> input(“Please enter a number: ”) ‘123’ >>> eval(input(“Please enter a number: ”)) 123

Functions A function is a named sequence of statements that performs a desired operation. This operation is specified in a function definition. In Python, the syntax for a function definition is: def ( ): … The statements inside the function are not executed until the function is called.

Functions II Syntax for a function call is: ( ) Function call itself is evaluated to a value if it returns something, otherwise it evaluates to None. >>> def f(): return 3 >>>f() >>> def f(x, y): return x+y >>>f(3, 4) >>> def f(x, y): print (x+y) >>>f(3, 4) ERROR >>> def f(x, y): return x, y >>>a = f(3,4) >>>a (3,4) >>>a[0] 3

Local vs. Global scope

lists, tuples Create a list by enclosing items in square brackets. Create a tuple by enclosing items in parenthesis. Items are separated by commas. Lists are mutable, tuples are not >>> a = [1, 2**3, 3+4] >>> a [1,8,7] >>> a[0] 1 >>> a[2] 7 >>> len(a) 3 >>> a[2] = 3 >>> a [1,8,3] >>> b = 1, 2**3, 3+4 >>> b (1,8,7) >>> b[0] 1 >>> b[2] 7 >>> len(b) 3 >>> b[2] = 3 ERROR

range() iterator You can think of range() as a function that generates a list of integers. range([start,] stop [, step]) default value of start = 0, default value of step = 1 Use list() to see the list range generates. >>> list( range(1,4)) #Example with start & stop values. [1, 2, 3] >>> list( range(1,10,2)) #Example with start, stop, and step. [1, 3, 5, 7, 9] >>> list(range(5, -6)) #List is empty in this is case. [] >>> list(range(5, -6, -1)) #Can have a negative step. [5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5]

for-loops for-loops allow us to repeat code a specified number of times. Syntax: for in : … >>> for i in [1, 5, 7]: print(i) >>> >>> for i in range(3): print(i) >>> >>> for i in range(3): print(i, end =‘ ‘) print(‘a’,end=‘ ‘) print() print(“hello”) 0 a 1 a 2 a hello >>>

for-loops II def f(x, y): k = 1 t = 2 for i in range(1,4): k = k + x t = t + y x = x + i return x, y, k, t result = f(3, 4) xykti beginning3412- After first iteration After second iteration After third iteration After this code is executed, result has the following value: (9, 4, 14, 14) Tracing a for-loop!

Some algorithmic patterns Recursive application of a function on a starting value to generate a sequence # x0 : starting value of the sequence to generate # num: number of terms to generate # prints the sequence generated by the recursive application # of function f() # f() is assumed to return the next value in the #sequence, # given the previous value def seq(x0, num): k = x0 for i in range (num): print (k, end = ' ') k = f(k)

Some algorithmic patterns II To build up, or accumulate, a final value piece by piece, use an accumulator variable. my_sum = 0 # initialize the accumulator variable for i in range(1,5): my_sum = my_sum + i print(my_sum) # my_sum’s value is the sum of ints in range[1,4] fact = 1 # initialize the accumulator variable for i in range(2, n+1): fact = fact * i print(fact) # fact’s value is n! (read as n factorial)