Introduction to Computer Programming for Non-Majors

Size: px
Start display at page:

Download "Introduction to Computer Programming for Non-Majors"

Transcription

1 Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2016 Mid-term Review Instructor: Long Ma The Department of Computer Science

2 Basic Model of a Computer Input CPU Control Unit Arithmetic /Logic Unit (ALU) Output Memory

3 Programming Languages Natural language has ambiguity and imprecision problems when used to describe complex algorithms. Programs expressed in an unambiguous, precise way using programming languages. Every structure in programming language has a precise form, called its syntax. Every structure in programming language has a precise meaning, called its semantics.

4 Inside a Python Program # File: chaos.py # A simple program illustrating chaotic behaviour Lines that start with # are called comments Python skips text from # to end of line Frequently used in programming Organized Easy read Maintenance

5 The Software Development Process The process of creating a program is often broken down into stages according to the information that is produced in each phase. Analyze the Problem Determine Specifications Create a Design Implement the Design Test/Debug the Program Maintain the Program

6 Example Program: Temperature Converter Before we start coding, let s write a rough draft of the program in pseudocode Pseudocode is precise English that describes what a program does, step by step.

7 Elements of Programs Names Names are given to variables (celsius, fahrenheit), modules (main, convert), etc. These names are called identifiers Every identifier must begin with a letter or underscore ( _ ), followed by any sequence of letters, digits, or underscores. Identifiers are case sensitive.

8 Elements of Programs Complete list of Python Keywords

9 Elements of Programs Output Statements A print statement can print any number of expressions. print( 3+4 ) 3+4 print(3, 4, 3+4) Successive print statements will display on separate lines (\n), or separate by other symbol, like space print(3, 4, end= ), print(3 + 4) A bare print will print a blank line. print()

10 Assignment Statements Simple Assignment <variable> = <expr> variable is an identifier, expr is an expression x = 3.9 * x * (1-x) >>> myvar = 0 The expression on the RHS is evaluated to produce a value which is then associated with the variable named on the LHS.

11 Assigning Input The purpose of an input statement is to get input from the user and store it into a variable. Input string: <variable> = input(<prompt>) First the prompt is printed The input part waits for the user to enter a value and press <enter>

12 Assigning Input Input number: <variable> = eval(input(<prompt>)) The expression that was entered is evaluated to turn it from a string of characters into a Python value (a number). The value is assigned to the variable.

13 Data Type Numeric Data Types Int (while numbers) Float ( with fractions) How can we tell which is which? A numeric literal without a decimal point produces an int value A literal that has a decimal point is represented by a float (even if the fractional part is 0)

14 Math Library To access the sqrt library routine, we need to access it as math.sqrt(x). Using this dot notation tells Python to use the sqrt function found in the math library module. To calculate the root, you can do: x=16 y = math.sqrt(x)

15 Accumulating Results: Factorial What does range(n) return? 0, 1, 2, 3,, n-1 range has another optional parameter! range(start, n) returns start, start + 1,, n-1 But wait! There s more! range(start, n, step) start, start+step,, n-1 list(<sequence>) to make a list

16 Accumulating Results: Factorial Some examples! >>> list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(range(5,10)) [5, 6, 7, 8, 9] >>> list(range(5,10,2)) [5, 7, 9]

17 The Object of Objects An object is a sort of active data type that combines data and operations. Objects know stuff (contain data) and they can do stuff (have operations). Basic idea view a complex system as the interaction of simpler objects. Objects interact by sending each other messages.

18 Using Graphical Objects Each object is an instance of some class, and the class describes the properties of the instance. Classes = Blueprints Example: Class of Car Object s of Car

19 GUI Most applications you re familiar with have Graphical User Interfaces (GUI) that provide windows, icons, buttons and menus. Python GUI framework: Tkinter There s a graphics library (graphics.py). It s based on Tkinter.

20 Simple Graphics Programming Since this is a library, we need to import the graphics commands import graphics A graphics window is a place on the screen where the graphics will appear. win = graphics.graphwin() Notice the use of dot notation to invoke the GraphWin function that lives in the graphics library. We can manipulate the window object through the variable win. Windows can be closed/destroyed by issuing the command win.close()

21 Simple Graphics Programming Doing the import this way eliminates the need to preface graphics commands with graphics. >>> from graphics import * >>> win = GraphWin()

22 Simple Graphics Programming >>> p = Point(50, 60) >>> p.getx() 50 >>> p.gety() 60 >>> win = GraphWin() >>> p.draw(win) >>> p2 = Point(140, 100) >>> p2.draw(win)

23 Simple Graphics Programming >>> ### Open a graphics window >>> win = GraphWin('Shapes') >>> ### Draw a red circle centered at point (100, 100) with radius 30 >>> center = Point(100, 100) >>> circ = Circle(center, 30) >>> circ.setfill('red') >>> circ.draw(win) >>> ### Put a textual label in the center of the circle >>> label = Text(center, "Red Circle") >>> label.draw(win) >>> ### Draw a square using a Rectangle object >>> rect = Rectangle(Point(30, 30), Point(70, 70)) >>> rect.draw(win) >>> ### Draw a line segment using a Line object >>> line = Line(Point(20, 30), Point(180, 165)) >>> line.draw(win) >>> ### Draw an oval using the Oval object >>> oval = Oval(Point(20, 150), Point(180, 199)) >>> oval.draw(win)

24 Using Graphical Objects The assignment righteye = lefteye makes righteye and lefteye refer to the same circle! The situation where two variables refer to the same object is called aliasing.

25 Interactive Graphics The graphics module hides the underlying, low-level window management Two simple way to get users input win.getmouse() Entry()

26 Interactive Graphics -win.getmouse() An event is generated whenever a user moves the mouse, clicks the mouse, or types a key on the keyboard. An event is an object that encapsulates information about what just happened! The event object is sent to the appropriate part of the program to be processed, for example, a button event.

27 Getting Mouse Clicks The following code reports the coordinates of a mouse click: from graphics import * win = GraphWin("Click Me!") p = win.getmouse() print("you clicked", p.getx(), p.gety()) We can use the accessors like getx and gety or other methods on the point returned.

28 Getting Mouse Clicks # triangle.pyw # Interactive graphics program to draw a triangle from graphics import * def main(): win = GraphWin("Draw a Triangle") win.setcoords(0.0, 0.0, 10.0, 10.0) message = Text(Point(5, 0.5), "Click on three points") message.draw(win) # Get and draw three vertices of triangle p1 = win.getmouse() p1.draw(win) p2 = win.getmouse() p2.draw(win) p3 = win.getmouse() p3.draw(win)

29 Getting Mouse Clicks # Use Polygon object to draw the triangle triangle = Polygon(p1,p2,p3) triangle.setfill("peachpuff") triangle.setoutline("cyan") triangle.draw(win) # Wait for another click to exit message.settext("click anywhere to quit.") win.getmouse() main()

30 Handling Textual Input -Entry() The triangle program s input was done completely through mouse clicks. There s also an Entry object that can get keyboard input. The Entry object draws a box on the screen that can contain text. It understands settext and gettext, with one difference that the input can be edited.

31 The String Data Type Text is represented in programs by the string data type. A string is a sequence of characters enclosed within quotation marks( or ) >>> str1="hello" >>> str2='spam' >>> print(str1, str2) Hello spam >>> type(str1) Getting a string as input >>> firstname = input("please enter your name: ") Please enter your name: John >>> print("hello", firstname) Hello John 10/3/16-31-

32 The String operations We can access the individual characters in a string through indexing. The positions in a string are numbered from the left, starting with 0. The general form is <string>[<expr>], where the value of expr determines which character is selected from the string. 10/3/16-32-

33 The String Data Type H e l l o B o b >>> greet = "Hello Bob" >>> greet[0] 'H' >>> print(greet[0], greet[2], greet[4]) H l o >>> x = 8 >>> print(greet[x - 2]) B 10/3/16-33-

34 The String Data Type H e l l o B o b In a string of n characters, the last character is at position n-1 since we start counting with 0. We can index from the right side using negative indexes. >>> greet[-1] 'b' >>> greet[::-1] 'bob olleh >>> greet[:-1] Hello Bo >>> str[1:-2] 'ello b' 10/3/16-34-

35 The String Data Type Can we put two strings together into a longer string? Two ways Concatenation glues two strings together (+) >>> "spam" + "eggs" 'spameggs' >>> "Spam" + "And" + "Eggs" 'SpamAndEggs Repetition: 5 * spam 10/3/16-35-

36 The String Data Type String variable is kinds of object! Operator Meaning + Concatenation * Repetition <string>[] <string>[:] len(<string>) for <var> in <string> Indexing Slicing Length Iteration through characters 10/3/16-36-

37 Split to List Read the input as a single string, then split it apart into substrings, each of which represents one words? Strings are objects and have useful methods associated with them One of these methods is split. This will split a string into substrings based on spaces. >>> "Hello string methods!".split() ['Hello', 'string', 'methods!'] 10/3/16-37-

38 Strings, Lists, and Sequences It turns out that strings are really a special kind of sequence, so these operations also apply to sequences! >>> [1,2] + [3,4] [1, 2, 3, 4] >>> [1,2]*3 [1, 2, 1, 2, 1, 2] >>> grades = ['A', 'B', 'C', 'D', 'F'] >>> grades[0] 'A' >>> grades[2:4] ['C', 'D'] >>> len(grades) 5 10/3/16-38-

39 Strings, Lists, and Sequences Lists are mutable, meaning they can be changed. Strings can not be changed. >>> mylist = [34, 26, 15, 10] >>> mylist[2] 15 >>> mylist[2] = 0 >>> mylist [34, 26, 0, 10] >>> mystring = "Hello World" >>> mystring[2] 'l' >>> mystring[2] = "p" Traceback (most recent call last): File "<pyshell#16>", line 1, in -toplevelmystring[2] = "p" TypeError: object doesn't support item assignment 10/3/16-39-

40 Append vs Extend Append: appends object at end Extend: extends list by appending elements from the iterable. >>> x = [1, 2, 3] >>> x.append([4, 5]) >>> x.extend([4, 5]) 10/3/16-40-

41 String Representation and Message Encoding The ord function returns the numeric (ordinal) code of a single character. The chr function converts a numeric code to the corresponding character. >>> ord("a") 65 >>> ord("a") 97 >>> chr(97) 'a' >>> chr(65) 'A' 10/3/16-41-

42 Input/Output as String Manipulation We now have a complete set of type conversion operations: Function float(<expr>) int(<expr>) str(<expr>) eval(<string>) Meaning Convert expr to a floating point value Convert expr to an integer value Return a string representation of expr Evaluate string as an expression 10/3/16-42-

43 String Formatting We can format our output by modifying the print statement as follows: print("the total value of your change is ${0:0.2f}".format(total)) Now we get something like: The total value of your change is $1.50 Key is the string format method. 10/3/16-43-

44 File Processing Working with text files in Python Associate a disk file with a file object using the open function <filevar> = open(<name>, <mode>) Name is a string with the actual file name on the disk. Sometimes you need to specify the path of this file. The mode is either r or w depending on whether we are reading or writing the file. Infile = open( input.txt, r ) Outfile = open( output, w ) 10/3/16-44-

45 File Methods <file>.read() reads the entire file at once, (possibly large, multi-line) string Eg. Infile.read() <file>.readline() reads in just only single line of the file at a time. This is all text up to and including the next newline character <file>.readlines() automatically parse the read contents into a list of the lines in the file. Each list item is a single line including the newline characters. 10/3/16-45-

46 File Processing Opening a file for writing prepares the file to receive data If you open an existing file for writing, you wipe out the file s contents. If the named file does not exist, a new one is created. Outfile = open("mydata.out", "w") print(<expressions>, file=outfile) 10/3/16-46-

47 The Function of Functions 47 So far, we ve seen four different types of functions: Our programs comprise a single function called main(). Built-in Python functions (abs,print,eval ) Functions from the standard libraries (math.sqrt ) Functions from the graphics module (p.getx() ) 10/3/16-47-

48 The reasons of defining functions 48 Having similar or identical code in more than one place has some drawbacks. Issue one: writing the same code twice or more. Issue two: This same code must be maintained in two separate places. Functions Can be used to reduce code duplication Make programs more easily understood Make programs more easily to be maintained. 10/3/16-48-

49 Defining Functions 49 The part of the program that creates a function is called a function definition. A function definition looks like this: def <name>(<formal-parameters>): <body> When the function is used in a program, we say the definition is called or invoked. A function is called by using its name followed by a list of actual parameters or arguments <name>(<actual-parameters>) 10/3/16-49-

50 Parameters Formal parameter -- The parameter defined as part of the function definition, e.g., x in: def cube(x): return x*x*x Actual Parameter -- The actual data sent to a function. It's found in the function call, e.g., 7 in result = cube(7) or y in: y=5 res = cube(y) 10/3/16-50-

51 Function Call 51 When Python comes to a function call, it initiates a four-step process. The calling program suspends execution at the point of the call. The formal parameters of the function get assigned the values supplied by the actual parameters in the call. The body of the function is executed. Control returns to the point just after where the function was called. 10/3/16-51-

52 Functions and Parameters The scope of a variable refers to the places in a program a given variable can be referenced. The variables used inside of a function are local to that function, even if they happen to have the same name as variables that appear inside of another function. Formal parameters, like all variables used in the function, are only accessible in the body of the function. Multiple parameters: In this case the formal and actual parameters are matched up based on position 10/3/16-52-

53 Function: Return Values Sometimes a function needs to return more than one value. To do this, simply list more than one expression in the return statement. When calling this function, use simultaneous assignment. As before, the values are assigned based on position 10/3/16-53-

CITS 4406 Problem Solving & Programming. Lecture 03 Numeric Data Processing

CITS 4406 Problem Solving & Programming. Lecture 03 Numeric Data Processing CITS 4406 Problem Solving & Programming Tim French Lecture 03 Numeric Data Processing (These slides are based on John Zelle s powerpoint slides for lectures accompanied with the text book) Python Programming,

More information

Macroanalysis. futureoftech/data-mining-classics-makesbeautiful-science

Macroanalysis.  futureoftech/data-mining-classics-makesbeautiful-science Macroanalysis His system processed thousands of digital books from the 19th- and late 18th centuries a period chosen because the works are free, plentiful and subject to a wealth of theory already. The

More information

2/4/11. Python Programming: An Introduction to Computer Science. Scientific Workflow Systems. Announcements

2/4/11. Python Programming: An Introduction to Computer Science. Scientific Workflow Systems. Announcements 2/4/11 Announcements Scientific Workflow Systems Today: Star/ng on simple graphics (in Python) Next up: Data integra/on, analysis, scien/fic workflows, etc 1 Reading ECS 166 Reading ECS 166 ECS 166 Python

More information

Objects and Graphics Part II

Objects and Graphics Part II Objects and Graphics Part II Quizzes coming next week Graphing the interest calculator/ Choosing Coordinates Python Programming, 2/e 4 Graphing Future Value/ Choosing Coordinates Python Programming, 2/e

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2015 Chapter 4 Part 2 The Department of Computer Science Python file extension name Save your file as.pyw can also be recognized as python

More information

This is a medical robot, guided by a skilled surgeon and designed to get to places doctors are unable to reach without opening a pacent up.

This is a medical robot, guided by a skilled surgeon and designed to get to places doctors are unable to reach without opening a pacent up. BBC Headline: Slashdot Headline: Robots join the fight against cancer Robot Snakes To Fight Cancer Via Natural Orifice Surgery This is a medical robot, guided by a skilled surgeon and designed to get to

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2016 Chapter 5 Part 1 Instructor: Long Ma The Department of Computer Science Objectives Chapter 5: Sequences: Strings, Lists, and Files

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2015 Chapter 5 Part 1 The Department of Computer Science Objectives To understand the string data type and how strings are represented

More information

String and list processing

String and list processing String and list processing Michael Mandel Lecture 3 Methods in Computational Linguistics I The City University of New York, Graduate Center https://github.com/ling78100/lectureexamples/blob/master/lecture03final.ipynb

More information

Introduction to Python. Data Structures

Introduction to Python. Data Structures Introduction to Python Data Structures Data Structures Encapsulation & Notion of an Object Data + a set of methods (functions) that operate on the data A.foo() Linear Data Structure: List, Strings, sequences

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2016 Chapter 2 Instructor: Long Ma The Department of Computer Science Review Install and test python Basic concept of computers Test simple

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2016 Chapter 3 Instructor: Long Ma The Department of Computer Science Review of Chapter 2 Stages of creating a program: Analyze the problem

More information

Lecture. Sequences: Strings, Lists, and Files. Richard E Sarkis CSC 161: The Art of Programming

Lecture. Sequences: Strings, Lists, and Files. Richard E Sarkis CSC 161: The Art of Programming Lecture Sequences: Strings, Lists, and Files Richard E Sarkis CSC 161: The Art of Programming Class Administrivia Agenda To understand the string data type and how strings are represented in the computer

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2015 Chapter 3 The Department of Computer Science Review of Chapter 2 Stages of creating a program: Analyze the problem Determine specifications

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2014 Chapter 3 The Department of Computer Science Review of Chapter 2 Stages of creating a program: Analyze the problem Determine specifications

More information

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science Python Programming: An Introduction to Computer Science Chapter 2 Writing Simple Programs Python Programming, 3/e 1 Objectives n To know the steps in an orderly software development process. n To understand

More information

Lecture Writing Programs. Richard E Sarkis CSC 161: The Art of Programming

Lecture Writing Programs. Richard E Sarkis CSC 161: The Art of Programming Lecture Writing Programs Richard E Sarkis CSC 161: The Art of Programming Class Administrivia Agenda To be able to understand and write Python statements to output information to the screen To assign values

More information

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science Python Programming: An Introduction to Computer Science Chapter 5 Sequences: Strings, Lists, and Files Python Programming, 2/e 1 Objectives n To understand the string data type and how strings are represented

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2014 Chapter 2 The Department of Computer Science Chapter 2 Elements of Programs-Objectives To be able to understand and write Python

More information

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science Python Programming: An Introduction to Computer Science Chapter 2 Python Programming, 2/e 1 Objectives n To be able to understand and write Python statements to output information to the screen, assign

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2016 Chapter 6 (Part 2) Instructor: Long Ma The Department of Computer Science Objectives--Defining Functions 2 To understand why programmers

More information

The winning bots both achieved a humanness rating of 52 percent. Human players received an average humanness rating of only 40 percent.

The winning bots both achieved a humanness rating of 52 percent. Human players received an average humanness rating of only 40 percent. The bots face off in a tournament against one another and about an equal number of humans, with each player trying to score points by eliminating its opponents. Each player also has a "judging gun" in

More information

CITS 4406 Problem Solving & Programming

CITS 4406 Problem Solving & Programming CITS 4406 Problem Solving & Programming Tim French Lecture 02 The Software Development Process (These slides are based on John Zelle s powerpoint slides for lectures accompanied with the text book) Objectives

More information

CS177 Recitation. Graphics. Python Programming, 2/e 1

CS177 Recitation. Graphics. Python Programming, 2/e 1 CS177 Recitation Graphics Python Programming, 2/e 1 Objectives To be familiar with the various graphic objects available in the graphics library. To understand the fundamental concepts of computer graphics,

More information

Sequences: Strings, Lists, and Files

Sequences: Strings, Lists, and Files Sequences: Strings, Lists, and Files Read: Chapter 5, Sections 11.1-11.3 from Chapter 11 in the textbook Strings: So far we have examined in depth two numerical types of data: integers (int) and floating

More information

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming Lecture Numbers Richard E Sarkis CSC 161: The Art of Programming Class Administrivia Agenda To understand the concept of data types To be familiar with the basic numeric data types in Python To be able

More information

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers Python Programming, 2/e 1 Objectives n To understand the concept of data types. n To be familiar with the basic

More information

CITS 1401 Problem Solving & Programming

CITS 1401 Problem Solving & Programming CITS 1401 Problem Solving & Programming Tim French Lecture 03 Numeric Data Processing (These slides are based on John Zelle s powerpoint slides for lectures accompanied with the text book) Objectives of

More information

Comp 151. Using Objects (and the beginning of graphics)

Comp 151. Using Objects (and the beginning of graphics) Comp 151 Using Objects (and the beginning of graphics) Admin New project coming Assignment Read chapter 4 in the Zelle book The Object of Objects Basic idea view a complex system as the interaction of

More information

Writing Python Programs, Numerical Data Types

Writing Python Programs, Numerical Data Types Writing Python Programs, Numerical Data Types Michael Mandel Lecture 2 Methods in Computational Linguistics I The City University of New York, Graduate Center https://github.com/ling78100/lectureexamples/blob/master/lecture02final.ipyn

More information

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

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems CSSE 120 Introduction to Software Development Exam 1 Format, Concepts, What you should be able to do, and Sample Problems Page 1 of 6 Format: The exam will have two sections: Part 1: Paper-and-Pencil o

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2016 Chapter 1 Instructor: Long Ma The Department of Computer Science Objectives To understand the respective roles of hardware and software

More information

Python Seminar. This is part 2 of 3 Python seminars. Needed Applications Chrome (website: c9.io)

Python Seminar. This is part 2 of 3 Python seminars. Needed Applications Chrome (website: c9.io) Python Seminar Needed Applications Chrome (website: c9.io) GradQuant Resources http://gradquant.ucr.edu/workshop-resources/ Audience No programing experience. Beginning Python. This is part 2 of 3 Python

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2015 Chapter 6 Part 1 The Department of Computer Science Objectives 2 To understand why programmers divide programs up into sets of cooperating

More information

Computing with Numbers

Computing with Numbers Computing with Numbers Example output: Numeric Data Types Numeric Data Types Whole numbers are represented using the integer data type (int for short).values of type int can be positive or negative whole

More information

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions Python Programming, 3/e 1 Objectives n To understand why programmers divide programs up into sets of cooperating functions.

More information

CHAPTER 6 DEFINING FUNCTIONS Python Programming: An Introduction to Computer Science

CHAPTER 6 DEFINING FUNCTIONS Python Programming: An Introduction to Computer Science CHAPTER 6 DEFINING FUNCTIONS Python Programming: An Introduction to Computer Science Objectives æ To understand why programmers divide programs up into sets of cooperating functions. æ To be able to define

More information

Introduction to Computer Programming for Non-Majors CSC 2301, Fall The Department of Computer Science

Introduction to Computer Programming for Non-Majors CSC 2301, Fall The Department of Computer Science Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2015 The Department of Computer Science CSC 2301 Welcome to Class CSC 2301! Introduction to Computer Programming for Non-Majors Who are

More information

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers Python Programming, 3/e 1 Objectives n To understand the concept of data types. n To be familiar with the basic

More information

6. Defining Functions

6. Defining Functions 6. Defining Functions Original Slides by John Zelle, 2010. (Minor modifications by the instructor) Intelligent Data Systems Lab. Seoul National University Objectives To understand why programmers divide

More information

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

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines. Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions Python Programming, 2/e 1 Objectives To understand why programmers divide programs up into sets of cooperating functions.

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Strings, Lists, and Sequences

Strings, Lists, and Sequences Strings, Lists, and Sequences It turns out that strings are really a special kind of sequence, so these operations also apply to sequences! >>> [1,2] + [3,4] [1, 2, 3, 4] >>> [1,2]*3 [1, 2, 1, 2, 1, 2]

More information

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

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions. COMP1730/COMP6730 Programming for Scientists Data: Values, types and expressions. Lecture outline * Data and data types. * Expressions: computing values. * Variables: remembering values. What is data?

More information

Lecture. Defining Functions. Richard E Sarkis CSC 161: The Art of Programming

Lecture. Defining Functions. Richard E Sarkis CSC 161: The Art of Programming Lecture Defining Functions Richard E Sarkis CSC 161: The Art of Programming Class Administrivia Agenda To understand why programmers divide program code up into sets of cooperating functions To be able

More information

Python Intro GIS Week 1. Jake K. Carr

Python Intro GIS Week 1. Jake K. Carr GIS 5222 Week 1 Why Python It s simple and easy to learn It s free - open source! It s cross platform IT S expandable!! Why Python: Example Consider having to convert 1,000 shapefiles into feature classes

More information

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors CMSC201 Computer Science I for Majors Lecture 09 Strings Last Class We Covered Lists and what they are used for Getting the length of a list Operations like append() and remove() Iterating over a list

More information

Control structure: Repetition - Part 1

Control structure: Repetition - Part 1 Control structure: Repetition - Part 1 01204111 Computers and Programming Chalermsak Chatdokmaiprai Department of Computer Engineering Kasetsart University Cliparts are taken from http://openclipart.org

More information

File processing and decision structures

File processing and decision structures File processing and decision structures Michael Mandel Lecture 4 Methods in Computational Linguistics I The City University of New York, Graduate Center https://github.com/ling78100/lectureexamples/blob/master/lecture04final.ipynb

More information

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

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2014 Chapter 5 Part 3 & Review The Department of Computer Science Multi-Line Strings -2-1 Files: Multi-line Strings A file is a sequence

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2016 Chapter 11 Part 1 Instructor: Long Ma The Department of Computer Science Chapter 11 Data Collections Objectives: To understand the

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2015 Chapter 5 Part 3 & Review The Department of Computer Science Multi-Line Strings Files: Multi-line Strings A file is a sequence of

More information

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors CMSC201 Computer Science I for Majors Lecture 09 Strings Last Class We Covered Lists and what they are used for Getting the length of a list Operations like append() and remove() Iterating over a list

More information

CS177 Python Programming. Recitation 2 - Computing with Numbers

CS177 Python Programming. Recitation 2 - Computing with Numbers CS177 Python Programming Recitation 2 - Computing with Numbers Outline Data types. Variables Math library. Range Function What is data (in the context of programming)? Values that are stored and manipulated

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2015 Chapter 11 Part 1 The Department of Computer Science Objectives Chapter 11 Data Collections To understand the use of lists (arrays)

More information

CPTS 111, Fall 2011, Sections 6&7 Exam 3 Review

CPTS 111, Fall 2011, Sections 6&7 Exam 3 Review CPTS 111, Fall 2011, Sections 6&7 Exam 3 Review File processing Files are opened with the open() command. We can open files for reading or writing. The open() command takes two arguments, the file name

More information

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

Introduction to Programming in Turing. Input, Output, and Variables Introduction to Programming in Turing Input, Output, and Variables The IPO Model The most basic model for a computer system is the Input-Processing-Output (IPO) Model. In order to interact with the computer

More information

Comp 151. More on Arithmetic and intro to Objects

Comp 151. More on Arithmetic and intro to Objects Comp 151 More on Arithmetic and intro to Objects Admin Any questions 2 The Project Lets talk about the project. What do you need A 'accumulator' variable. Start outside of the loop Lets look at your book's

More information

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

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT Python The way of a program Srinidhi H Asst Professor Dept of CSE, MSRIT 1 Problem Solving Problem solving means the ability to formulate problems, think creatively about solutions, and express a solution

More information

CSCE 120: Learning To Code

CSCE 120: Learning To Code CSCE 120: Learning To Code Manipulating Data I Introduction This module is designed to get you started working with data by understanding and using variables and data types in JavaScript. It will also

More information

CSCA20 Worksheet Strings

CSCA20 Worksheet Strings 1 Introduction to strings CSCA20 Worksheet Strings A string is just a sequence of characters. Why do you think it is called string? List some real life applications that use strings: 2 Basics We define

More information

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

Lesson 4: Type Conversion, Mutability, Sequence Indexing. Fundamentals of Text Processing for Linguists Na-Rae Han Lesson 4: Type Conversion, Mutability, Sequence Indexing Fundamentals of Text Processing for Linguists Na-Rae Han Objectives Python data types Mutable vs. immutable object types How variable assignment

More information

Python. Objects. Geog 271 Geographic Data Analysis Fall 2010

Python. Objects. Geog 271 Geographic Data Analysis Fall 2010 Python This handout covers a very small subset of the Python language, nearly sufficient for exercises in this course. The rest of the language and its libraries are covered in many fine books and in free

More information

Computing with Strings. Learning Outcomes. Python s String Type 9/23/2012

Computing with Strings. Learning Outcomes. Python s String Type 9/23/2012 Computing with Strings CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17 1 Learning Outcomes To understand the string data type and how strings are represented

More information

Lecture 4: Basic I/O

Lecture 4: Basic I/O Lecture 4: Basic I/O CS1068+ Introductory Programming in Python Dr Kieran T. Herley Department of Computer Science University College Cork 2017-2018 KH (21/09/17) Lecture 4: Basic I/O 2017-2018 1 / 20

More information

Topic 7: Lists, Dictionaries and Strings

Topic 7: Lists, Dictionaries and Strings Topic 7: Lists, Dictionaries and Strings The human animal differs from the lesser primates in his passion for lists of Ten Best H. Allen Smith 1 Textbook Strongly Recommended Exercises The Python Workbook:

More information

Part III Appendices 165

Part III Appendices 165 Part III Appendices 165 Appendix A Technical Instructions Learning Outcomes This material will help you learn how to use the software you need to do your work in this course. You won t be tested on it.

More information

File Input/Output. Learning Outcomes 10/8/2012. CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01. Discussion Sections 02-08, 16, 17

File Input/Output. Learning Outcomes 10/8/2012. CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01. Discussion Sections 02-08, 16, 17 CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 1 Discussion Sections 02-08, 16, 17 Adapted from slides by Sue Evans et al. 2 Learning Outcomes Become familiar with input and output (I/O) from

More information

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

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program

More information

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions Grace Murray Hopper Expressions Eric Roberts CSCI 121 January 30, 2018 Grace Hopper was one of the pioneers of modern computing, working with

More information

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an  . Michigan State University CSE 231, Fall 2013 CSE 231, Rich Enbody Office Hours After class By appointment send an email 2 1 Project 1 Python arithmetic Do with pencil, paper and calculator first Idle Handin Help room 3 What is a Computer Program?

More information

CS110 Introduction to Computing Fall 2006 Midterm Exam

CS110 Introduction to Computing Fall 2006 Midterm Exam CS110 Introduction to Computing Fall 2006 Midterm Exam Name: This is an open book/notes exam. Sharing of notes and books is not permitted. Answer all questions in the space provided. Continue on back of

More information

Fundamentals of Programming (Python) Getting Started with Programming

Fundamentals of Programming (Python) Getting Started with Programming Fundamentals of Programming (Python) Getting Started with Programming Ali Taheri Sharif University of Technology Some slides have been adapted from Python Programming: An Introduction to Computer Science

More information

Comp 151. More on Arithmetic and number types

Comp 151. More on Arithmetic and number types Comp 151 More on Arithmetic and number types Admin Any questions 2 Strings Remember Strings from last time Strings represent text. Discussion of data representation in a computer 1000001 = 65 (A) 110000

More information

Programming to Python

Programming to Python Programming to Python Sept., 5 th Slides by M. Stepp, M. Goldstein, M. DiRamio, and S. Shah Compiling and interpreting Many languages require you to compile (translate) your program into a form that the

More information

The >>> is a Python prompt indicating that Python is ready for us to give it a command. These commands are called statements.

The >>> is a Python prompt indicating that Python is ready for us to give it a command. These commands are called statements. When you start Python, you will see something like: Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information.

More information

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program? Intro to Programming & C++ Unit 1 Sections 1.1-4 and 2.1-10, 2.12-13, 2.15-17 CS 1428 Spring 2019 Jill Seaman 1.1 Why Program? Computer programmable machine designed to follow instructions Program a set

More information

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords Worksheet 1: Introductory Exercises Turtle Programming Calculations The Print Function Comments Syntax Semantics Strings Concatenation Quotation Marks Types Variables Restrictions on Variable Names Long

More information

CSEN 102 Introduction to Computer Science

CSEN 102 Introduction to Computer Science CSEN 102 Introduction to Computer Science Lecture 2: Python and Sequential Algorithms Prof. Dr. Slim Abdennadher Dr. Aysha Alsafty, slim.abdennadher@guc.edu.eg, aysha.alsafty@guc.edu.eg German University

More information

>>> * *(25**0.16) *10*(25**0.16)

>>> * *(25**0.16) *10*(25**0.16) #An Interactive Session in the Python Shell. #When you type a statement in the Python Shell, #the statement is executed immediately. If the #the statement is an expression, its value is #displayed. #Lines

More information

mith College Computer Science Lecture Notes CSC111 Week 7 Spring 2018 Dominique Thiébaut

mith College Computer Science Lecture Notes CSC111 Week 7 Spring 2018 Dominique Thiébaut mith College Computer Science Lecture Notes Week 7 Spring 2018 CSC111 Dominique Thiébaut dthiebaut@smith.edu Midterm Grades available later today (3/19/18) Outline A Second Look at Files Reading Files

More information

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science Python Programming: An Introduction to Computer Science Chapter 7 Decision Structures Python Programming, 2/e 1 Objectives æ To understand the programming pattern simple decision and its implementation

More information

Introduction to Python

Introduction to Python Introduction to Python Reading assignment: Perkovic text, Ch. 1 and 2.1-2.5 Python Python is an interactive language. Java or C++: compile, run Also, a main function or method Python: type expressions

More information

Expressions and Variables

Expressions and Variables Expressions and Variables Expressions print(expression) An expression is evaluated to give a value. For example: 2 + 9-6 Evaluates to: 5 Data Types Integers 1, 2, 3, 42, 100, -5 Floating points 2.5, 7.0,

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG 1 Notice Assignments Reading Assignment: Chapter 3: Introduction to Parameters and Objects The Class 10 Exercise

More information

Variables and Constants

Variables and Constants 87 Chapter 5 Variables and Constants 5.1 Storing Information in the Computer 5.2 Declaring Variables 5.3 Inputting Character Strings 5.4 Mistakes in Programs 5.5 Inputting Numbers 5.6 Inputting Real Numbers

More information

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

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python Python Sequence Types Sequence types str and bytes are sequence types Sequence types have several operations defined for them Indexing Python Sequence Types Each element in a sequence can be extracted

More information

A Brief Introduction to Python

A Brief Introduction to Python A Brief Introduction to Python Python is an interpreted language, meaning that a program, i.e., the interpreter, is used to execute your commands one after the other. The commands can be entered interactively

More information

Introduction to Python Code Quality

Introduction to Python Code Quality Introduction to Python Code Quality Clarity and readability are important (easter egg: type import this at the Python prompt), as well as extensibility, meaning code that can be easily enhanced and extended.

More information

The SPL Programming Language Reference Manual

The SPL Programming Language Reference Manual The SPL Programming Language Reference Manual Leonidas Fegaras University of Texas at Arlington Arlington, TX 76019 fegaras@cse.uta.edu February 27, 2018 1 Introduction The SPL language is a Small Programming

More information

Graphics Reference (graphics.py v5)

Graphics Reference (graphics.py v5) Graphics Reference (graphics.py v5) 1 Overview The package graphics.py is a simple object oriented graphics library designed to make it very easy for novice programmers to experiment with computer graphics

More information

CMSC 201 Computer Science I for Majors

CMSC 201 Computer Science I for Majors CMSC 201 Computer Science I for Majors Lecture 02 Intro to Python Syllabus Last Class We Covered Grading scheme Academic Integrity Policy (Collaboration Policy) Getting Help Office hours Programming Mindset

More information

Variable and Data Type I

Variable and Data Type I Islamic University Of Gaza Faculty of Engineering Computer Engineering Department Lab 2 Variable and Data Type I Eng. Ibraheem Lubbad September 24, 2016 Variable is reserved a location in memory to store

More information

Downloaded from Chapter 2. Functions

Downloaded from   Chapter 2. Functions Chapter 2 Functions After studying this lesson, students will be able to: Understand and apply the concept of module programming Write functions Identify and invoke appropriate predefined functions Create

More information

At full speed with Python

At full speed with Python At full speed with Python João Ventura v0.1 Contents 1 Introduction 2 2 Installation 3 2.1 Installing on Windows............................ 3 2.2 Installing on macos............................. 5 2.3

More information

Comp 151. More on working with Data

Comp 151. More on working with Data Comp 151 More on working with Data 1 Admin Questions on project? Did you see the project yet? Quiz coming next week 2 working with lots of data sometimes working with one piece of data at a time is fine

More information

Starting. Read: Chapter 1, Appendix B from textbook.

Starting. Read: Chapter 1, Appendix B from textbook. Read: Chapter 1, Appendix B from textbook. Starting There are two ways to run your Python program using the interpreter 1 : from the command line or by using IDLE (which also comes with a text editor;

More information

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

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals

More information