Math 15 Introduction to Scientific Data Analysis Lecture 8 Python Programming – Part 2 University of California, Merced.

Slides:



Advertisements
Similar presentations
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Advertisements

CMPS 1371 Introduction to Computing for Engineers
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Lists Introduction to Computing Science and Programming I.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Chapter 2 Writing Simple Programs
The University of Texas – Pan American
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
An Introduction to Textual Programming
Introduction to Python
Programming for Linguists An Introduction to Python 24/11/2011.
Python Programming Chapter 8: Lists Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Math 15 Lecture 10 University of California, Merced Scilab Programming – No. 1.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Programming in Python Part III Dr. Fatma Cemile Serçe Atılım University
Tutorial 51 Programming Structures Sequence - program instructions are processed, one after another, in the order in which they appear in the program Selection.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Visual Basic Programming
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Math 15 Introduction to Scientific Data Analysis Lecture 9 Python Programming – #3 University of California, Merced.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
Magic 8 ball. "Design and write a python program that emulates a Magic Eight Ball. Your program should continually prompt the user to enter a question.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
String and Lists Dr. José M. Reyes Álamo.
G. Pullaiah College of Engineering and Technology
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Presented By S.Yamuna AP/IT
ITM 352 Flow-Control: Loops
3rd prep. – 2nd Term MOE Book Questions.
PH2150 Scientific Computing Skills
CPS120: Introduction to Computer Science
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Chapter (3) - Looping Questions.
T. Jumana Abu Shmais – AOU - Riyadh
Loops CIS 40 – Introduction to Programming in Python
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Introduction to TouchDevelop
Lists A list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements. Lists are.
String and Lists Dr. José M. Reyes Álamo.
Computer Science Core Concepts
Case & Repetitive Statements
A LESSON IN LOOPING What is a loop?
Introduction to Computer Science
Introduction to Strings
Welcome back! October 11, 2018.
Introduction to Strings
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Presentation transcript:

Math 15 Introduction to Scientific Data Analysis Lecture 8 Python Programming – Part 2 University of California, Merced

UC Merced2 Schedule December pm

UC Merced3 Last Week!  First time of Python Programming What did you think?

UC Merced4 October, 2007 Bis 180

UC Merced5  Any Questions?

UC Merced6 Assignment #5 due November 2 nd, 2007! Assignments & their materials!

UC Merced7 Click This!

UC Merced8 First, Retrieve this file. For submission, first click this to attach your python codes.

UC Merced9 Click this to browse your file and attach your python file

UC Merced10 Then hit this to continue!

UC Merced11 To submit

UC Merced12  Any Questions?

UC Merced13 Reviews from 1 st Programming Experience  Case sensitive Name1 = raw_input( “….. name1 = raw_input( “…..  If computer is asking questions, you need to respond! name1 = raw_input("What is your name?") print "Hello, " + name1 Python Program IDLE ==== No Subprocess ==== >>> What is your name? Masa Hello, Masa >>>

UC Merced14 What is a computer programming?  A computer programming is a sequence of instructions that specifies how to perform a computation. Basically, all computers need someone to tell them what to do!

UC Merced15 Example – Conversion of US$ to Can$ # # This program converts from US $ to Canadian $ us_money = input ("Money value in US $ ") can_money = us_money /0.6 print "US$", us_money, " = Canadian $", can_money IDLE 1.2 >>> Money value in US $ 2.5 US$ 2.5 = Canadian $ >>> Execute (F5 of Editing window) us_money = 2.5 Variables

UC Merced16 Python Type "copyright", "credits" or "license()" for more information. >>> x = 2 >>> x 2 >>> y = 1 >>> y 1 >>> x + y 3 >>> y = x >>> x + y 4 >>>

UC Merced17 3 mistakes in this program. # if statement answer = raw_input("Do you like Mathematics? ") if answer == "yes": print "That is great!" else: print "That is disappointing!”

UC Merced18  Any Questions?

UC Merced19 Next Week  More Programming while statement  General loop for loop statement  A sequence iteration List  Mutable (changeable) arrays of object reference:  i.e. [0,1,2,3] [ ‘ Lions ’, ” Tigers ’, ’ Bears ’, ’ Oh my ’ ]

UC Merced20 Remember!  The process of learning to program is an excellent opportunity to practice problem- solving skills.

UC Merced21 Lists  A list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements. Examples: var1 = [10, 20, 30, 40] var2 = ["spam", "bungee", "swallow"] var3 = ["hello", 2.0, 5, “ UC Merced ” ] a list of four integers. a list of three strings. a list of mixing value types

UC Merced22 List operations >>> a = [1, 2, 3] >>> b = [4, 5, 6] >>> c = a + b >>> print c [1, 2, 3, 4, 5, 6]  The + operator concatenates lists:

UC Merced23 List operations – cont. >>> list = ['a', 'b', 'c', 'd', 'e', 'f'] >>> list[:4] [‘a’, 'b', 'c‘, ‘d’] >>> list[3:] ['d', 'e', 'f'] >>> list[1:3] ['b', 'c']  The slice operations: Up to 4 th element From 3 rd element on 0 th element1 st element

UC Merced24 List operations – cont. >>> fruit = ["banana", "apple", "quince"] >>> fruit[0] = "pear" >>> fruit[-1] = "orange" >>> print fruit ['pear', 'apple', 'orange']  lists are mutable, which means we can change their elements. 0 th Element Last Element

UC Merced25 List operations – cont.  List deletion del removes an element from a list: >>> a = ['one', 'two', 'three'] >>> del a[1] >>> a ['one', 'three'] You can use a slice as an index for del: >>> list = ['a', 'b', 'c', 'd', 'e', 'f'] >>> del list[1:5] >>> print list ['a', 'f']

UC Merced26 List operations – cont.  Append & Remove Syntax- list_name.append() & list_name.remove()  Example: ucm = [“shrimp", “hare", "eagle", “bobcat"] print ucm ucm.append("dog") print "The zoo has the following animals:", ucm ucm.remove("eagle") print "The zoo has the following animals:", ucm >>> [‘shrimp', ‘hare', 'eagle', ‘bobcat'] The zoo has the following animals: [‘shrimp', ‘hare', 'eagle', ‘bobcat', 'dog'] The zoo has the following animals: [‘shrimp', ‘hare', ‘bobcat', 'dog']

UC Merced27  Any Questions?

UC Merced28 while statement  Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that computers do well and people do poorly.  The first feature we are going to look at is the while statement. The while statement continuously performs an action while a condition is true. while : Syntax The while command first checks if the condition is true or false and then execute the command(s) if true.

UC Merced29 while statement – cont.  Example N = 10# Set the initial # value of N while N > 0: print N N = N-1 print "Blastoff!" >>> Blastoff! This program reads: "While N is greater than 0, continue displaying the value of N and then reducing the value of N by 1. When you get to 0, display the word Blastoff!"

UC Merced30 while statement – cont.  Here is the flow of execution for a while statement: 1. Evaluate the condition, yielding false or true. 2. If the condition is true, execute each of the commands in the body and then go back to If the condition is false, exit the while statement and continue execution at the next statement. while : Syntax

UC Merced31  Any Questions?

UC Merced32 for loop statement  Another operation for repetitive tasks is for loop statement.  Each time through the loop, the next character in the string or next value in the value list is assigned to the variable, jj. The loop continues until no characters or values are left. for jj in (list): Syntax

UC Merced33 for loop statement – cont. fruit = ["banana","apple","orange"] for masa in fruit: print masa  Examples >>> banana apple orange N = 10 for var0 in range(N): print var0 >>>

UC Merced34 for loop statement – cont. N = 10 for var in range(1,N,2): print var  Examples >>> N = 15 icount = 0 # Intializing a variable for var in range(1,N,2): icount = icount + 1 print var, icount >>> Starting Value End Value Increment Counter

UC Merced35  Any Questions?

UC Merced36  Homework #5 due November 2 nd Already available in UCMCROP Here comes Quiz #4!