CSCE 110 Programming I

Size: px
Start display at page:

Download "CSCE 110 Programming I"

Transcription

1 CSCE 110 Programming I Simulations and Plotting Data Dr. Tiffani L. Williams Department of Computer Science and Engineering Texas A&M University Spring 2014 Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

2 We are now done with Part 1 of the course. We have covered the basics of Python. Now, it s time to look at different kinds of problems we can solve using our new found knowledge of Python. In this module, we will look at simulation and plotting data. Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

3 Flipping a coin On average, how often will heads appear during a coin flip? How would you prove it? Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

4 Fair coin experiments Listing 1: fair-coin.py 1 # A program that simulates the number of heads and tails that appear 2 # using a fair coin. 3 4 import random 5 6 def flip_coin (): 7 return random. choice ([ heads, tails ]) 8 9 def main ( coin_tosses ): 10 heads = 0 11 tails = 0 12 for i in range (0, coin_tosses ): 13 result = flip_coin () 14 if result == heads : 15 heads += 1 16 else : 17 tails += # Print results 20 print Total number of coin tosses : %d % ( coin_tosses ) 21 print Number of heads : %d (%.2 f percent ) \ 22 %( heads, float ( heads )/ coin_tosses * 100) 23 print Number of tails : %d (%.2 f percent ) \ 24 %( tails, float ( tails )/ coin_tosses * 100) main (10000) Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

5 Unfair coin experiments Listing 2: unfair-coin.py 1 # A program that simulates the number of heads and tails that appear 2 # using an unfair coin. 3 4 import random 5 6 def flip_coin (): 7 return random. choice ([ heads, tails, tails, tails ]) 8 9 def main ( coin_tosses ): 10 heads = 0 11 tails = 0 12 for i in range (0, coin_tosses ): 13 result = flip_coin () 14 if result == heads : 15 heads += 1 16 else : 17 tails += # Print results 20 print Total number of coin tosses : %d % ( coin_tosses ) 21 print Number of heads : %d (%.2 f percent ) \ 22 %( heads, float ( heads )/ coin_tosses * 100) 23 print Number of tails : %d (%.2 f percent ) \ 24 %( tails, float ( tails )/ coin_tosses * 100) main (10000) Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

6 Consider the following problem. On average, how many times do you need to roll a die before all six different numbers have turned up? Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

7 We can use a computer to simulate the problem for us. Listing 3: rolling-all-numbers.py 1 import random 2 3 def roll_die (): 4 return random. randint (1,6) 5 6 def roll_all_numbers (): 7 rolled_value = [0,0,0,0,0,0,0] 8 number_rolls = while sum ( rolled_value )!= 6: 11 number = roll_die () 12 rolled_value [ number ] = 1 13 number_rolls += return number_rolls def conduct_simulation ( repetitions ): 18 total_rolls = 0 19 for i in range (0, repetitions ): 20 total_rolls += roll_all_numbers () 21 print Repetitions : % -8d \t Average rolls is %.1 f % ( 22 repetitions, float ( total_rolls ) / repetitions ) def main (): 25 for experiment in (1, 10, 100, 1000, 10000, ): 26 conduct_simulation ( experiment ) main () Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

8 Sets A set is used to contain an unordered collection of objects. The elements of a set are never duplicated. To create a set, use the set() function and supply a sequence of items such as follows: Sets are unordered and cannot be indexed by numbers. Sets support a standard collection of operations, including union, intersection, difference, and symmetric difference. New items can be added to a set using add() or update(): Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

9 Set Examples I 1 >>> a = [1, 2, 3, 4, 1, 3, " apple "] 2 >>> b = set (a) # convert the list a to a set 3 >>> b 4 set ([1, 2, 3, 4, apple ]) 5 >>> c = set ([" apple ", " banana ", 3]) 6 >>> c 7 set ([3, apple, banana ]) 8 >>> c. add (4) # add the integer 4 to the set c 9 >>> c 10 set ([3, 4, apple, banana ]) 11 >>> c.add (" banana ") # add " banana " to the set c 12 >>> c 13 set ([3, 4, apple, banana ]) 14 >>> b 15 set ([1, 2, 3, 4, apple ]) 16 >>> b. union (c) # union 17 set ([1, 2, 3, 4, apple, banana ]) 18 >>> b. intersection (c) # intersection 19 set ([3, 4, apple ]) 20 >>> b. difference (c) # set difference 21 set ([1, 2]) 22 >>> b. issubset (c) # subset 23 False 24 >>> b. issuperset (c) # superset 25 False 26 >>> d = set ([1,2]) 27 >>> d. issubset (b) # subset 28 True 29 >>> b Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

10 Set Examples II 30 set ([1, 2, 3, 4, apple ]) 31 >>> b. pop () # arbitrarily select an item from the set b >>> b 34 set ([2, 3, 4, apple ]) 35 >>> b. remove (" apple ") # remove " apple " from the set b 36 >>> b 37 set ([2, 3, 4]) 38 >>> len (b) >>> b [1] 41 Traceback ( most recent call last ): 42 File "<string >", line 1, in <fragment > 43 TypeError : set object does not support indexing Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

11 Consider the following problem. Let s assume you roll six dice at the same time. On average, how many times do you need to roll six dice before each of them has a different number? Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

12 Computer programming comes to the rescue again. I Listing 4: six-dice-with-all-numbers.py 1 # Simulates the number of times we have to roll six dice so that each of them 2 # have different values. 3 import random 4 5 def roll_six_dice (): 6 roll = [] 7 for die in range (6): 8 roll += [ random. randint (1,6)] 9 return roll def roll_all_numbers (): 12 number_rolls = 0 13 roll = [] 14 while len ( roll )!= 6: 15 roll = set ( roll_six_dice ()) 16 number_rolls += 1 17 return number_rolls def conduct_simulation ( repetitions ): 20 total_rolls = 0 21 for i in range (0, repetitions ): 22 total_rolls += roll_all_numbers () 23 print Repetitions : % -8d \t Average rolls is %f % ( 24 repetitions, float ( total_rolls ) / repetitions ) def main (): Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

13 Computer programming comes to the rescue again. II 27 for experiment in (1, 10, 100, 1000, 10000): 28 conduct_simulation ( experiment ) main () Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

14 Simple Plotting Example (1) Listing 5: matplot-lib-example.py 1 import matplotlib. pyplot as plot 2 3 # plot values 4 plot.plot ( range (0,10), [9,4,5,2,3,5,7,12,2,3] ) 5 plot.plot ( range (0,10), [12,5,33,2,4,5,3,3,22,10] ) 6 7 # annotate our plot 8 plot. xlabel ( x axis ) 9 plot. ylabel ( y axis ) 10 plot. title ( a simple plot ) 11 plot. legend () 12 plot. grid ( True ) ## view our plot 15 plot. savefig ("sample -plot.pdf ") # saves file in PDF 16 #plot. savefig (" sample -plot.png ") # saves file in PNG format 17 #plot. savefig (" sample -plot.jpg ") # saves file in JPG 18 plot. show () # allows for interactive exploration of the plot Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

15 Simple Plotting Example (2) 35 a simple plot y axis x axis Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

16 Plotting Fair Coin Experiments (1) I Listing 6: fair-coin-plot-results.py 1 # Simulates the number of heads and tails that appear using a fair coin. 2 3 import random 4 import matplotlib. pyplot as plot 5 6 def plot_results ( heads_list, tails_list, x_axis ): 7 plot. plot ( x_axis, heads_list, label= heads, marker= o ) 8 plot. plot ( x_axis, tails_list, label= tails, marker= ^ ) 9 plot. xlabel ( Coin Tosses ) 10 plot. ylabel ( Percentage (%) ) 11 plot. legend () 12 plot. grid () 13 plot. ylim (0,80) 14 plot. xscale ( log ) 15 plot. savefig ("fair -coin -plot.pdf ") # saves file in PDF 16 plot. show () def flip_coin (): 19 return random. choice ([ heads, tails ]) def simulate ( coin_tosses ): 22 heads = 0 23 tails = 0 24 for i in range (0, coin_tosses ): 25 result = flip_coin () 26 if result == heads : Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

17 Plotting Fair Coin Experiments (1) II 27 heads += 1 28 else : 29 tails += # Print results 32 print Total number of coin tosses : %d % ( coin_tosses ) 33 print Number of heads : %d (%.2 f percent ) %( 34 heads, float ( heads )/ coin_tosses * 100) 35 print Number of tails : %d (%.2 f percent ) %( 36 tails, float ( tails )/ coin_tosses * 100) return heads, tails def main (): 41 heads_y_axis = [] 42 tails_y_axis = [] 43 x_axis = [10, 100, 1000, 10000, , ] 44 for tosses in x_axis : 45 heads, tails = simulate ( tosses ) 46 # heads_y_axis += list ( heads ) 47 heads_y_axis += [ float ( heads )/ tosses * 100] 48 tails_y_axis += [ float ( tails )/ tosses * 100] plot_results ( heads_y_axis, tails_y_axis, x_axis ) main () Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

18 Plotting Fair Coin Experiments (2) heads tails Percentage(%) Coin Tosses Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

19 File Processing (1) Now, we are going to learn how to obtain input from a file. Consider the following file of planet names. The name of this file is planets.txt. 1 Mercury 2 Venus 3 Earth 4 Mars 5 Jupiter 6 Saturn 7 Uranus 8 Neptune Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

20 File Processing (2) Let s read the contents of the planets.txt file and output the results to the screen. Listing 7: read-file-planets.py 1 # open file for reading 2 input_file = open ( planets. txt, r ) 3 4 # place contents of file into the variable lines 5 # the function readlines () returns the file contents as a list 6 lines = input_file. readlines () 7 8 # now iterate through the list as you normally would 9 for line in lines : 10 print "%s: %d" %( line, len (line )) # close the file 13 input_file. close () Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

21 File Processing (3) Another version of our Python program for reading the planets.txt file. 1 input_file = open ( planets. txt, r ) 2 for line in input_file. readlines (): 3 strip_line = line. strip () # removes the new line character 4 print "%s: %d" %( strip_line, len ( strip_line )) 5 input_file. close () Tiffani L. Williams (Texas A&M) CSCE 110) Spring / 21

CS101 - Text Processing Lecture 8

CS101 - Text Processing Lecture 8 CS101 - Text Processing Lecture 8 School of Computing KAIST 1 / 16 Roadmap Last week we learned Data structures String Set Dictionary Image processing 2 / 16 Roadmap Last week we learned Data structures

More information

Symbols. Anscombe s quartet, antiderivative, 200. bar charts for exercise, for expenses, Barnsley fern, drawing,

Symbols. Anscombe s quartet, antiderivative, 200. bar charts for exercise, for expenses, Barnsley fern, drawing, Index Symbols + (addition operator), 2 {} (curly brackets), to define a set, 122 δ (delta), 184 / (division operator), 2 ε (epsilon), 192, 197 199 == (equality operator), 124 e (Euler s number), 179 **

More information

CSCE 110 Programming I Basics of Python: Lists, Tuples, and Functions

CSCE 110 Programming I Basics of Python: Lists, Tuples, and Functions CSCE 110 Programming I Basics of Python: Lists, Tuples, and Functions Dr. Tiffani L. Williams Department of Computer Science and Engineering Texas A&M University Fall 2011 Lists Ordered collection of data.

More information

CSCE 110 Programming I

CSCE 110 Programming I CSCE 110 Programming I Basics of Python (Part 3): Functions, Lists, For Loops, and Tuples Dr. Tiffani L. Williams Department of Computer Science and Engineering Texas A&M University Spring 2014 Tiffani

More information

Introduction to Scientific Computing Lecture 8

Introduction to Scientific Computing Lecture 8 Introduction to Scientific Computing Lecture 8 Professor Hanno Rein Last updated: November 7, 2017 1 N-body integrations 1.1 Newton s law We ll discuss today an important real world application of numerical

More information

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

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output Dr. Tiffani L. Williams Department of Computer Science and Engineering Texas A&M University Spring 2011 Python Python was developed

More information

CIS192 Python Programming

CIS192 Python Programming CIS192 Python Programming Probability, Simulations & Course Wrap-Up Raymond Yin University of Pennsylvania December 8, 2016 Raymond Yin (University of Pennsylvania) CIS 192 December 8, 2016 1 / 21 Outline

More information

Introduction to Scientific Computing Lecture 8

Introduction to Scientific Computing Lecture 8 Introduction to Scientific Computing Lecture 8 Professor Hanno Rein Last updated: October 30, 06 7. Runge-Kutta Methods As we indicated before, we might be able to cancel out higher order terms in the

More information

CSCE 110 Programming I

CSCE 110 Programming I CSCE 110 Programming I Basics of Python (Part 1): Variables, Expressions, and Input/Output Dr. Tiffani L. Williams Department of Computer Science and Engineering Texas A&M University Spring 2013 Tiffani

More information

PyPlot. The plotting library must be imported, and we will assume in these examples an import statement similar to those for numpy and math as

PyPlot. The plotting library must be imported, and we will assume in these examples an import statement similar to those for numpy and math as Geog 271 Geographic Data Analysis Fall 2015 PyPlot Graphicscanbeproducedin Pythonviaavarietyofpackages. We willuseapythonplotting package that is part of MatPlotLib, for which documentation can be found

More information

CS 2530 INTERMEDIATE COMPUTING

CS 2530 INTERMEDIATE COMPUTING CS 2530 INTERMEDIATE COMPUTING Spring 2016 1-8-2018 Michael J. Holmes University of Northern Iowa Object Oriented Concepts In some cases we ll learn the concepts prior to learning the terminology. Other

More information

Announcement. Submit assignment 3 on CourSys Do not hand in hard copy Due Friday, 15:20:00. Caution: Assignment 4 will be due next Wednesday

Announcement. Submit assignment 3 on CourSys Do not hand in hard copy Due Friday, 15:20:00. Caution: Assignment 4 will be due next Wednesday Announcement Submit assignment 3 on CourSys Do not hand in hard copy Due Friday, 15:20:00 Caution: Assignment 4 will be due next Wednesday Recursion Examples and Simple Searching CMPT 125 Jan. 28 Recursion

More information

Question 1 [13] The multiple choice answers are as follows: 1.1 A 1.2 D 1.3 B 1.4 B 1.5 C

Question 1 [13] The multiple choice answers are as follows: 1.1 A 1.2 D 1.3 B 1.4 B 1.5 C Question 1 [13] The multiple choice answers are as follows: 1.1 A 1.2 D 1.3 B 1.4 B 1.5 C Please see the LibreOffice document for the detailed memo for each multiple choice question. Bladsy 1 van 11 /

More information

Basics of Programming with Python

Basics of Programming with Python Basics of Programming with Python A gentle guide to writing simple programs Robert Montante 1 Topics Part 3 Obtaining Python Interactive use Variables Programs in files Data types Decision-making Functions

More information

Solution: It may be helpful to list out exactly what is in each of these events:

Solution: It may be helpful to list out exactly what is in each of these events: MATH 5010(002) Fall 2017 Homework 1 Solutions Please inform your instructor if you find any errors in the solutions. 1. You ask a friend to choose an integer N between 0 and 9. Let A = {N 5}, B = {3 N

More information

CS 115 Exam 3, Spring 2011

CS 115 Exam 3, Spring 2011 CS 115 Exam 3, Spring 2011 Your name: Rules You may use one handwritten 8.5 x 11 cheat sheet (front and back). This is the only resource you may consult during this exam. Explain/show work if you want

More information

PyPlot. The plotting library must be imported, and we will assume in these examples an import statement similar to those for numpy and math as

PyPlot. The plotting library must be imported, and we will assume in these examples an import statement similar to those for numpy and math as Geog 271 Geographic Data Analysis Fall 2017 PyPlot Graphicscanbeproducedin Pythonviaavarietyofpackages. We willuseapythonplotting package that is part of MatPlotLib, for which documentation can be found

More information

Scientific Python: matplotlib

Scientific Python: matplotlib Scientific Python: matplotlib 17 July 2014 Introduction and Aims This exercise introduces the matplotlib module of Python. Matplotlib is a versatile plotting library that can be used to produce both quick

More information

4.2 Function definitions the basics

4.2 Function definitions the basics 4.2. FUNCTION DEFINITIONS THE BASICS 89 4.2 Function definitions the basics There are three questions you must answer before you can write a function definition: What will the function do? What inputs

More information

Loop-Body Returns. Another way to terminate a loop.

Loop-Body Returns. Another way to terminate a loop. 12. Loops and Logic Topics: Loop-Body Return The Idea of a Random Walk Showcase: Open-Ended Iteration Showcase: Computing Averages Showcase: Top-Down Development Loop-Body Returns Loop-Body Returns Another

More information

Eng591 - Virtual Reality Programming First Mid-Term Exam

Eng591 - Virtual Reality Programming First Mid-Term Exam Eng591 - Virtual Reality Programming First Mid-erm Exam Fall 1999 You are allowed to use your coursepack only, ( including any notes written therein. ) No other assistance or references are permitted.

More information

LSP 121. LSP 121 Math and Tech Literacy II. Topics. More VBA. More VBA. Variables

LSP 121. LSP 121 Math and Tech Literacy II. Topics. More VBA. More VBA. Variables Greg Brewster, DePaul University Page 1 Math and Tech Literacy II Greg Brewster DePaul University Topics More Visual Basic Variables Naming Rules Implicit vs. Explicit Types Variable Lifetime Static variables

More information

Lecture 7. SchemeList, finish up; Universal Hashing introduction

Lecture 7. SchemeList, finish up; Universal Hashing introduction Lecture 7. SchemeList, finish up; Universal Hashing introduction CS 16 February 24, 2010 1 / 15 foldleft #!/usr/bin/python def foldleft(func, slist, init): foldleft: ( * -> ) * ( SchemeList)

More information

Bi 1x Spring 2014: Plotting and linear regression

Bi 1x Spring 2014: Plotting and linear regression Bi 1x Spring 2014: Plotting and linear regression In this tutorial, we will learn some basics of how to plot experimental data. We will also learn how to perform linear regressions to get parameter estimates.

More information

Section 1.1. Inductive Reasoning. Copyright 2013, 2010, 2007, Pearson, Education, Inc.

Section 1.1. Inductive Reasoning. Copyright 2013, 2010, 2007, Pearson, Education, Inc. Section 1.1 Inductive Reasoning What You Will Learn Inductive and deductive reasoning processes 1.1-2 Natural Numbers The set of natural numbers is also called the set of counting numbers. N = {1, 2, 3,

More information

Test 1 - Python Edition

Test 1 - Python Edition 'XNH8QLYHUVLW\ (GPXQG73UDWW-U6FKRRORI(QJLQHHULQJ EGR 103L Spring 2018 Test 1 - Python Edition Shaundra B. Daily & Michael R. Gustafson II NetID (please print): In keeping with the Community Standard, I

More information

11 Sets II Operations

11 Sets II Operations 11 Sets II Operations Tom Lewis Fall Term 2010 Tom Lewis () 11 Sets II Operations Fall Term 2010 1 / 12 Outline 1 Union and intersection 2 Set operations 3 The size of a union 4 Difference and symmetric

More information

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points) EECS 183 Fall 2013 Exam 1 Part 1 (80 points) Closed Book Closed Notes Closed Electronic Devices Closed Neighbor Turn off Your Cell Phones We will confiscate all electronic devices that we see including

More information

Section 1.1. Inductive Reasoning. Copyright 2013, 2010, 2007, Pearson, Education, Inc.

Section 1.1. Inductive Reasoning. Copyright 2013, 2010, 2007, Pearson, Education, Inc. Section 1.1 Inductive Reasoning What You Will Learn Inductive and deductive reasoning processes 1.1-2 Natural Numbers The set of natural numbers is also called the set of counting numbers. N = {1, 2, 3,

More information

Data 8 Final Review #1

Data 8 Final Review #1 Data 8 Final Review #1 Topics we ll cover: Visualizations Arrays and Table Manipulations Programming constructs (functions, for loops, conditional statements) Chance, Simulation, Sampling and Distributions

More information

The Hong Kong Polytechnic University

The Hong Kong Polytechnic University COMP1001-Problem Solving in IT Top-Down Design Objective: Upon completion of this lab, you will be able to: How to use top down design to design an Automatic teller machine (ATM) Structure of Top-Down

More information

Chapter 8 SETS AND DICTIONARIES

Chapter 8 SETS AND DICTIONARIES Chapter 8 SETS AND DICTIONARIES Chapter Goals To build and use a set container To learn common set operations for processing data To build and use a dictionary container To work with a dictionary for table

More information

Chapter 8 SETS AND DICTIONARIES

Chapter 8 SETS AND DICTIONARIES Chapter 8 SETS AND DICTIONARIES Chapter Contents Sets Dictionaries Complex Data Structures 10/19/16 Page 2 Chapter Goals To build and use a set container To learn common set opera2ons for processing data

More information

Sets. Mukulika Ghosh. Fall Based on slides by Dr. Hyunyoung Lee

Sets. Mukulika Ghosh. Fall Based on slides by Dr. Hyunyoung Lee Sets Mukulika Ghosh Fall 2018 Based on slides by Dr. Hyunyoung Lee Sets Sets A set is an unordered collection of objects, called elements, without duplication. We write a A to denote that a is an element

More information

Flow Control: Branches and loops

Flow Control: Branches and loops Flow Control: Branches and loops In this context flow control refers to controlling the flow of the execution of your program that is, which instructions will get carried out and in what order. In the

More information

CS 251 Intermediate Programming More on classes

CS 251 Intermediate Programming More on classes CS 251 Intermediate Programming More on classes Brooke Chenoweth University of New Mexico Spring 2018 Empty Class public class EmptyClass { Has inherited methods and fields from parent (in this case, Object)

More information

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Python review. 1 Python basics. References. CS 234 Naomi Nishimura Python review CS 234 Naomi Nishimura The sections below indicate Python material, the degree to which it will be used in the course, and various resources you can use to review the material. You are not

More information

15-110: Principles of Computing, Spring 2018

15-110: Principles of Computing, Spring 2018 5-: Principles of Computing, Spring 28 Problem Set 8 (PS8) Due: Friday, March 3 by 2:3PM via Gradescope Hand-in HANDIN INSTRUCTIONS Download a copy of this PDF file. You have two ways to fill in your answers:.

More information

Chapter 5 Functions. Dr. Zhang COSC 1436 Summer, 2018 June 19, 2018

Chapter 5 Functions. Dr. Zhang COSC 1436 Summer, 2018 June 19, 2018 Chapter 5 Functions Dr. Zhang COSC 1436 Summer, 2018 June 19, 2018 while Loop Repetition Review A Condition-Controlled Loop Causes a statement or set of statements to repeat as long as a condition is true.

More information

Discrete Mathematics 2 Exam File Spring 2012

Discrete Mathematics 2 Exam File Spring 2012 Discrete Mathematics 2 Exam File Spring 2012 Exam #1 1.) Suppose f : X Y and A X. a.) Prove or disprove: f -1 (f(a)) A. Prove or disprove: A f -1 (f(a)). 2.) A die is rolled four times. What is the probability

More information

Installation and Basic Usage Constructing Graphs Analyzing Graphs Plotting (Matplotlib) NetworkX Tutorial

Installation and Basic Usage Constructing Graphs Analyzing Graphs Plotting (Matplotlib) NetworkX Tutorial September 28, 2012 1 Installation and Basic Usage 2 Constructing Graphs 3 Analyzing Graphs 4 Plotting (Matplotlib) Local Installation install manually from http://pypi.python.org/pypi/networkx or use built-in

More information

Analysis of Large-Scale Networks: NetworkX

Analysis of Large-Scale Networks: NetworkX Analysis of Large-Scale Networks NetworkX JP Onnela Department of Biostatistics Harvard School of Public Health July 17, 2014 Outline 1 Overview of NetworkX 2 Nodes and Edges 3 Node Degree and Neighbors

More information

X & Y COORDINATES. Teachers Guide

X & Y COORDINATES. Teachers Guide Teachers Guide Background: One piece of information that is essential to us on a daily basis is our location. We may not realize how often we use this type of information, but a little reflection on your

More information

User-Defined Function

User-Defined Function ENGR 102-213 (Socolofsky) Week 11 Python scripts In the lecture this week, we are continuing to learn powerful things that can be done with userdefined functions. In several of the examples, we consider

More information

CSC Discrete Math I, Spring Sets

CSC Discrete Math I, Spring Sets CSC 125 - Discrete Math I, Spring 2017 Sets Sets A set is well-defined, unordered collection of objects The objects in a set are called the elements, or members, of the set A set is said to contain its

More information

1.1 - Introduction to Sets

1.1 - Introduction to Sets 1.1 - Introduction to Sets Math 166-502 Blake Boudreaux Department of Mathematics Texas A&M University January 18, 2018 Blake Boudreaux (Texas A&M University) 1.1 - Introduction to Sets January 18, 2018

More information

CS1110 Lab 6 (Mar 17-18, 2015)

CS1110 Lab 6 (Mar 17-18, 2015) CS1110 Lab 6 (Mar 17-18, 2015) First Name: Last Name: NetID: The lab assignments are very important and you must have a CS 1110 course consultant tell CMS that you did the work. (Correctness does not matter.)

More information

UNC Charlotte 2010 Comprehensive

UNC Charlotte 2010 Comprehensive 00 Comprehensive March 8, 00. A cubic equation x 4x x + a = 0 has three roots, x, x, x. If x = x + x, what is a? (A) 4 (B) 8 (C) 0 (D) (E) 6. For which value of a is the polynomial P (x) = x 000 +ax+9

More information

LSP 121. LSP 121 Math and Tech Literacy II. Topics. Loops. Loops. Loops

LSP 121. LSP 121 Math and Tech Literacy II. Topics. Loops. Loops. Loops LSP 121 Math and Tech Literacy II Greg Brewster DePaul University Topics The FOR loop Repeats a set of statements a fixed number of times The WHILE loop Repeats a set of statements until a condition is

More information

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters. CS 61A Spring 2019 Guerrilla Section 5: April 20, 2019 1 Interpreters 1.1 Determine the number of calls to scheme eval and the number of calls to scheme apply for the following expressions. > (+ 1 2) 3

More information

CARDSTOCK MODELING Math Manipulative Kit. Student Activity Book

CARDSTOCK MODELING Math Manipulative Kit. Student Activity Book CARDSTOCK MODELING Math Manipulative Kit Student Activity Book TABLE OF CONTENTS Activity Sheet for L.E. #1 - Getting Started...3-4 Activity Sheet for L.E. #2 - Squares and Cubes (Hexahedrons)...5-8 Activity

More information

Introduction to Python

Introduction to Python Introduction to Python Jon Kerr Nilsen, Dmytro Karpenko Research Infrastructure Services Group, Department for Research Computing, USIT, UiO Why Python Clean and easy-to-understand syntax alldata = cpickle.load(open(filename1,

More information

Basic Shell Scripting Practice. HPC User Services LSU HPC & LON March 2018

Basic Shell Scripting Practice. HPC User Services LSU HPC & LON March 2018 Basic Shell Scripting Practice HPC User Services LSU HPC & LON sys-help@loni.org March 2018 Quotation Exercise 1. Print out your $LOGNAME 2. Print date 3. Print `who am i` 4. Print your current directory

More information

List comprehensions (and other shortcuts) UW CSE 160 Spring 2015

List comprehensions (and other shortcuts) UW CSE 160 Spring 2015 List comprehensions (and other shortcuts) UW CSE 160 Spring 2015 Three Ways to Define a List Explicitly write out the whole thing: squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100] Write a loop to create

More information

Figure 1: From Left to Right, General Venn Diagrams for One, Two, and Three Sets

Figure 1: From Left to Right, General Venn Diagrams for One, Two, and Three Sets 2.3. VENN DIAGRAMS & SET OPERATIONS In this section we introduce Venn diagrams and define four basic operations on sets. We also present some important properties related to these operations. Venn Diagrams

More information

3. Probability 51. probability A numerical value between 0 and 1 assigned to an event to indicate how often the event occurs (in the long run).

3. Probability 51. probability A numerical value between 0 and 1 assigned to an event to indicate how often the event occurs (in the long run). 3. Probability 51 3 Probability 3.1 Key Definitions and Ideas random process A repeatable process that has multiple unpredictable potential outcomes. Although we sometimes use language that suggests that

More information

Part IV. More on Python. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26,

Part IV. More on Python. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26, Part IV More on Python Compact Course @ Max-Planck, February 16-26, 2015 36 More on Strings Special string methods (excerpt) s = " Frodo and Sam and Bilbo " s. islower () s. isupper () s. startswith ("

More information

Practice Question. Omar Khan / Atif Alvi. October 10, 2016

Practice Question. Omar Khan / Atif Alvi. October 10, 2016 Practice Question Omar Khan / Atif Alvi October 10, 2016 1 Chapter 1 Loops 1.1 While Loop Iterating through a string 1. Print the first 10 integers 1 i = 0 2 n = 10 3 while i

More information

Compliments and Disjoint Events

Compliments and Disjoint Events Compliments and Disjoint Events p. 1/ Compliments and Disjoint Events Gene Quinn Compliments and Disjoint Events p. 2/ Suppose E is an event. Then "E does not occur" is also an event. Compliments and Disjoint

More information

CS 139 Practice Midterm Questions #2

CS 139 Practice Midterm Questions #2 CS 139 Practice Midterm Questions #2 Spring 2016 Name: 1. Write Java statements to accomplish each of the following. (a) Declares numbers to be an array of int s. (b) Initializes numbers to contain a reference

More information

COT 3100 Spring 2010 Midterm 2

COT 3100 Spring 2010 Midterm 2 COT 3100 Spring 2010 Midterm 2 For the first two questions #1 and #2, do ONLY ONE of them. If you do both, only question #1 will be graded. 1. (20 pts) Give iterative and recursive algorithms for finding

More information

COMP519 Web Programming Lecture 12: JavaScript (Part 3) Handouts

COMP519 Web Programming Lecture 12: JavaScript (Part 3) Handouts COMP519 Web Programming Lecture 12: JavaScript (Part 3) Handouts Ullrich Hustadt Department of Computer Science School of Electrical Engineering, Electronics, and Computer Science University of Liverpool

More information

Sets and Maps. Set Commands Set ADT Set ADT implementation Map ADT Map ADT implementation

Sets and Maps. Set Commands Set ADT Set ADT implementation Map ADT Map ADT implementation Sets and Maps Set Commands Set ADT Set ADT implementation Map ADT Map ADT implementation Set The Set is a common container used in programming. Represents the mathematical set structure: Stores unordered

More information

4. BASIC PLOTTING. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

4. BASIC PLOTTING. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman 4. BASIC PLOTTING JHU Physics & Astronomy Python Workshop 2016 Lecturer: Mubdi Rahman INTRODUCING MATPLOTLIB! Very powerful plotting package. The Docs: http://matplotlib.org/api/pyplot_api.html GETTING

More information

UNIVERSITETET I OSLO

UNIVERSITETET I OSLO (Continued on page 2.) UNIVERSITETET I OSLO Det matematisk-naturvitenskapelige fakultet Examination in: INF1100 Introduction to programming with scientific applications Day of examination: Thursday, October

More information

UNIT 9A Randomness in Computation: Random Number Generators

UNIT 9A Randomness in Computation: Random Number Generators UNIT 9A Randomness in Computation: Random Number Generators 1 Last Unit Computer organization: what s under the hood 3 This Unit Random number generation Using pseudorandom numbers 4 Overview The concept

More information

Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103. Chapter 2. Sets

Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103. Chapter 2. Sets Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 2 Sets Slides are adopted from Discrete Mathematics and It's Applications Kenneth H.

More information

Goals for This Lecture:

Goals for This Lecture: Goals for This Lecture: Learn how to use gnuplot to make simple plots of data from a file. Introduce some basic I/O concepts Using Gnuplot to Plot data from a file Gnuplot (pronounced Ga-new-plot ) is

More information

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau List of Slides 1 Title 2 Chapter 13: Graphical user interfaces 3 Chapter aims 4 Section 2: Example:Hello world with a GUI 5 Aim 6 Hello world with a GUI 7 Hello world with a GUI 8 Package: java.awt and

More information

Python. Chapter 4. Sets

Python. Chapter 4. Sets Python Chapter 4 Sets 53 Chapter 4 Sets 54 Python Chapter 4 Sets In This Chapter: 1. Introduction to Sets 2. Symmetric Difference 3. No Idea! 4. Set.add() 5. Set.discard(),.remove() &.pop() 6. Set.union()

More information

1. Find the type and value of each of the following expressions. Warning: several of these expressions will result in an error.

1. Find the type and value of each of the following expressions. Warning: several of these expressions will result in an error. This was the final CS1 exam given in May, 2014. I eliminated one question concerning classes and objects, and clarified a few points that were unclear in the original version. I also translated some things

More information

Introductory Scientific Computing with Python

Introductory Scientific Computing with Python Introductory Scientific Computing with Python Introduction, IPython and Plotting FOSSEE Department of Aerospace Engineering IIT Bombay SciPy India, 2015 December, 2015 FOSSEE group (IIT Bombay) Interactive

More information

ios 8 SDK Development

ios 8 SDK Development Extracted from: ios 8 SDK Development Creating iphone and ipad Apps with Swift This PDF file contains pages extracted from ios 8 SDK Development, published by the Pragmatic Bookshelf. For more information

More information

CS150 - Final Cheat Sheet

CS150 - Final Cheat Sheet CS150 - Final Cheat Sheet 1 Input/Output Reading input from the user raw input(message): Displays message to the user and return what the user typed as a string Reading from a file file = open(filename,

More information

CSC 1351: Quiz 6: Sort and Search

CSC 1351: Quiz 6: Sort and Search CSC 1351: Quiz 6: Sort and Search Name: 0.1 You want to implement combat within a role playing game on a computer. Specifically, the game rules for damage inflicted by a hit are: In order to figure out

More information

Last Time: Rolling a Weighted Die

Last Time: Rolling a Weighted Die Last Time: Rolling a Weighted Die import math/rand func DieRoll() int { return rand.intn(6) + 1 Multiple Rolls When we run this program 100 times, we get the same outcome! func main() int { fmt.println(dieroll())

More information

COMP284 Scripting Languages Lecture 3: Perl (Part 2) Handouts

COMP284 Scripting Languages Lecture 3: Perl (Part 2) Handouts COMP284 Scripting Languages Lecture 3: Perl (Part 2) Handouts Ullrich Hustadt Department of Computer Science School of Electrical Engineering, Electronics, and Computer Science University of Liverpool

More information

TI-84 GRAPHING CALCULATOR

TI-84 GRAPHING CALCULATOR TI-84 GRAPHING CALCULATOR Table of Contents Set Up & Troubleshooting... 3 TI-84: Resetting the Calculator... 4 TI-84: Mode Settings... 5 Entering Data... 7 TI-84: Entering & Editing Lists of Data... 8

More information

CMPSCI 187 / Spring 2015 Implementing Sets Using Linked Lists

CMPSCI 187 / Spring 2015 Implementing Sets Using Linked Lists CMPSCI 187 / Spring 2015 Implementing Sets Using Linked Lists Due on Tuesday February 24, 2015, 8:30 a.m. Marc Liberatore and John Ridgway Morrill I N375 Section 01 @ 10:00 Section 02 @ 08:30 1 CMPSCI

More information

COMP284 Scripting Languages Lecture 15: JavaScript (Part 2) Handouts

COMP284 Scripting Languages Lecture 15: JavaScript (Part 2) Handouts COMP284 Scripting Languages Lecture 15: JavaScript (Part 2) Handouts Ullrich Hustadt Department of Computer Science School of Electrical Engineering, Electronics, and Computer Science University of Liverpool

More information

MODIFIED VERSION OF: An introduction to Matlab for dynamic modeling ***PART 3 ***

MODIFIED VERSION OF: An introduction to Matlab for dynamic modeling ***PART 3 *** MODIFIED VERSION OF: An introduction to Matlab for dynamic modeling ***PART 3 *** Stephen P. Ellner 1 and John Guckenheimer 2 1 Department of Ecology and Evolutionary Biology, and 2 Department of Mathematics

More information

Chapter 5 Functions (Part 2) Hong Sun COSC 1436 Fall, 2017 Oct 18, 2017

Chapter 5 Functions (Part 2) Hong Sun COSC 1436 Fall, 2017 Oct 18, 2017 Chapter 5 Functions (Part 2) Hong Sun COSC 1436 Fall, 2017 Oct 18, 2017 Defining and Calling a Void Function Function Name Roles: python requires that you follow the same rules that you follow when naming

More information

ENGR 102 Engineering Lab I - Computation

ENGR 102 Engineering Lab I - Computation ENGR 102 Engineering Lab I - Computation Learning Objectives by Week 1 ENGR 102 Engineering Lab I Computation 2 Credits 2. Introduction to the design and development of computer applications for engineers;

More information

D&B Market Insight Release Notes. November, 2015

D&B Market Insight Release Notes. November, 2015 D&B Market Insight Release Notes November, 2015 Table of Contents Table of Contents... 2 Charting Tool: Add multiple measures to charts... 3 Charting Tool: Additional enhancements to charts... 6 Data Grids:

More information

Assignment 02 (Due: Monday, February 1, 2016)

Assignment 02 (Due: Monday, February 1, 2016) Assignment 02 (Due: Monday, February 1, 2016) CSCE 155N 1 Lab Objectives Improve your understanding of arrays and array operations Differentiate array operators and matrix operators Create, access, modify,

More information

CIS192 Python Programming

CIS192 Python Programming CIS192 Python Programming Probability and Simulations (With Other Modules) Harry Smith University of Pennsylvania October 11, 2017 Harry Smith (University of Pennsylvania) CIS 192 Lecture 7 October 11,

More information

COMP1730/COMP6730 Programming for Scientists. Dictionaries and sets

COMP1730/COMP6730 Programming for Scientists. Dictionaries and sets COMP1730/COMP6730 Programming for Scientists Dictionaries and sets Lecture outline * Mappings: the dict type. * Sets: the set type. Mappings * A mapping (a.k.a. dictionary) stores key value pairs; each

More information

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python CONDITION CONTROLLED LOOPS Introduction to Programming - Python Generating Random Numbers Generating a random integer Sometimes you need your program to generate information that isn t available when you

More information

Random Walks and Defining Functions FEB 9 AND 11, 2015

Random Walks and Defining Functions FEB 9 AND 11, 2015 Random Walks and Defining Functions FEB 9 AND 11, 2015 If we take a random walk, will we go places? Problem: Simulate a random walk in which a person starts of at point 0 and at each step randomly picks

More information

GRAPHING CALCULATOR REFERENCE BOOK

GRAPHING CALCULATOR REFERENCE BOOK John T. Baker Middle School GRAPHING CALCULATOR REFERENCE BOOK Name: Teacher: - 1 - To Graph an Equation: Graphing Linear Equations 1.) Press Y= and enter the equation into Y 1. 2.) To see the graph in

More information

(f) d={ alchemist :( a, t ), shaman : ( s, n ), wizard : ( w, z )} d[ shaman ][1]

(f) d={ alchemist :( a, t ), shaman : ( s, n ), wizard : ( w, z )} d[ shaman ][1] CSCI1101 Final Exam December 18, 2018 Solutions 1. Determine the value and type of each of the expressions below. If the question has two lines, assume that the statement in the first line is executed,

More information

Condition Controlled Loops. Introduction to Programming - Python

Condition Controlled Loops. Introduction to Programming - Python + Condition Controlled Loops Introduction to Programming - Python + Repetition Structures n Programmers commonly find that they need to write code that performs the same task over and over again + Example:

More information

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples Abstract Data Types CS 234, Fall 2017 Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples Data Types Data is stored in a computer as a sequence of binary digits:

More information

Lecture 11: while loops CS1068+ Introductory Programming in Python. for loop revisited. while loop. Summary. Dr Kieran T. Herley

Lecture 11: while loops CS1068+ Introductory Programming in Python. for loop revisited. while loop. Summary. Dr Kieran T. Herley Lecture 11: while loops CS1068+ Introductory Programming in Python Dr Kieran T. Herley Python s while loop. Summary Department of Computer Science University College Cork 2017-2018 KH (24/10/17) Lecture

More information

CSE 215: Foundations of Computer Science Recitation Exercises Set #9 Stony Brook University. Name: ID#: Section #: Score: / 4

CSE 215: Foundations of Computer Science Recitation Exercises Set #9 Stony Brook University. Name: ID#: Section #: Score: / 4 CSE 215: Foundations of Computer Science Recitation Exercises Set #9 Stony Brook University Name: ID#: Section #: Score: / 4 Unit 14: Set Theory: Definitions and Properties 1. Let C = {n Z n = 6r 5 for

More information

Introduction to Matplotlib: 3D Plotting and Animations

Introduction to Matplotlib: 3D Plotting and Animations 1 Introduction to Matplotlib: 3D Plotting and Animations Lab Objective: 3D plots and animations are useful in visualizing solutions to ODEs and PDEs found in many dynamics and control problems. In this

More information

Fifth Annual Juilfs Contest Spring 2013

Fifth Annual Juilfs Contest Spring 2013 Fifth Annual Juilfs Contest Spring 2013 A. Transmat (5 points) The Fourth Great and Bountiful Human Empire is built on transmat technology. Two terminals are created using quantum entanglement. Then,

More information

Introduction to Python

Introduction to Python Introduction to Python Development Environments what IDE to use? 1. PyDev with Eclipse 2. Sublime Text Editor 3. Emacs 4. Vim 5. Atom 6. Gedit 7. Idle 8. PIDA (Linux)(VIM Based) 9. NotePad++ (Windows)

More information

Assignment 4: Due Friday Mar 11 at 6pm

Assignment 4: Due Friday Mar 11 at 6pm CS1110 Spring 2016 Assignment 4: Due Friday Mar 11 at 6pm You must work either on your own or with one partner. If you work with a partner, you and your partner must first register as a group in CMS (this

More information

MMAE 540 Homework 9. Stefan Stevanovic. Nov. 20, 2014

MMAE 540 Homework 9. Stefan Stevanovic. Nov. 20, 2014 MMAE Homework Stefan Stevanovic Nov., Problem : Generate a PRM (Probabilistic Road Map) path planner that maps the path from the goal location (X, Y ) = (, ) to a final location (X, Y ) = (, ), with the

More information