Introduction to Programming

Slides:



Advertisements
Similar presentations
Repetition There are three different ways that a set of instructions can be repeated, and each way is determined by where the decision to repeat is.
Advertisements

Repetition Control Structures
Adapted from John Zelle’s Book Slides
Python Programming: An Introduction to Computer Science
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
Loops – While, Do, For Repetition Statements Introduction to Arrays
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Chapter 2 Writing Simple Programs
Introduction to C Programming
Adapted from slides by Marie desJardins
Computer Science 101 Introduction to Programming.
A First Book of ANSI C Fourth Edition
Python Programming Fundamentals
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
The University of Texas – Pan American
Introduction to Python
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 12: How Long Can This Go On?
Input, Output, and Processing
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
1 CS161 Introduction to Computer Science Topic #8.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Python: Experiencing IDLE, writing simple programs
Chapter 2 Introduction to C++ Programming
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
A Simple Syntax-Directed Translator
Chapter 2 - Introduction to C Programming
Chapter 5 - Control Structures: Part 2
ALGORITHMS AND FLOWCHARTS
Introduction to Programming
The Selection Structure
Variables, Expressions, and IO
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Lecture 2 Python Programming & Data Types
Chapter 2 - Introduction to C Programming
Algorithm Discovery and Design
Chapter 2 - Introduction to C Programming
Introduction to Programming Using Python PART 2
Fundamentals of visual basic
Lecture 2 Python Programming & Data Types
Flowcharts and Pseudo Code
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Presentation transcript:

Introduction to Programming A program is a sequence of precise instructions Common programming languages: Python, Perl, Java, C++ Elements of a programming language: keywords – words that have special meaning (the vocabulary) syntax – rules that describe the valid constructs (the grammar) Some of the keywords in Python if else def elif for return in

Python Environment Python Shell: for executing and testing the programs Python Editor: for typing/creating the programs

Introduction to Python Using Python as a fancy calculator >>> 2 + 3 5 >>> 2 + 3 * 4 14 >>> (2 + 3) * 4 20 >>> exams = 100 >>> labs = 70 >>> total = exams + labs >>> print total 170

Functions Adding your own constructs describe how to convert from Fahrenheit to Celsius To CONVERT to Celsius a given temperature: 1. subtract 32.0 from the given temperature 2. multiply the value from step 1 by 5.0 3. divide the value from step 2 by 9.0 4. report the value from step 3 def convert(degrees): value1 = degrees - 32.0 value2 = value1 * 5.0 result = value2 / 9.0 return result

Functions Adding your own constructs describe how to convert from Fahrenheit to Celsius def convert(degrees): value1 = degrees - 32.0 value2 = value1 * 5.0 result = value2 / 9.0 return result Testing the new construct (in the Python Shell): >>> convert(32) >>> convert(-40) 170

Functions Adding your own constructs describe how to convert from Fahrenheit to Celsius def convert(degrees): value1 = degrees - 32.0 value2 = value1 * 5.0 result = value2 / 9.0 return result The first two keywords – def and return: def – define a new construct return – computation is done, send the "result" back

Variables Adding your own constructs A new concept – variables: describe how to convert from Fahrenheit to Celsius def convert(degrees): value1 = degrees - 32.0 value2 = value1 * 5.0 result = value2 / 9.0 return result A new concept – variables: places to store values keep track of intermediate quantities Variable names can be anything (other than keywords) but: must contain only letters, digits, and _ symbol cannot start with a digit

Functions Adding another construct describe how to compute the average of two numbers To compute the AVERAGE of two numbers x and y: 1. Add the numbers x and y 2. Divide the value from step 1 by 2.0 3. report the value from step 2 def average(x, y): total = x + y result = total / 2.0 return result >>> average(20, 70) 45 >>> exams = 90 >>> labs = 80 >>> average(exams, labs) 85

Functions Multiple steps can be carried out at once def average(x, y): total = x + y result = total / 2.0 return result def average(x, y): result = (x + y) / 2.0 return result def convert(degrees): result = (degrees - 32.0) * 5.0 / 9.0 return result def convert(degrees): value1 = degrees - 32.0 value2 = value1 * 5.0 result = value2 / 9.0 return result

Functions Summary Functions describe useful computations Functions take input, process the input, and return a result convert average

Functions Summary Choose good names for the function, inputs, and variables def average(x, y): result = (x + y) / 2.0 return result def compute(tic, tac): toe = (tic + tac) / 2.0 return toe

Expressing Decisions Write a function that computes absolute value of a number To compute the Absolute Value of a given number x: 1. if x is greater than 0: the result is x itself otherwise: the result is negative x 2. report the result def absValue(x): if x > 0: result = x else: result = -x return result >>> absValue(5) 5 >>> absValue(-3) 3

Chaining Decisions Write a function that converts a student’s exam score to a grade in the range [0 .. 4] To convert a given exam score to [0..4]: 1. if the score is at least 90: the result is 4.0 otherwise if the score is at least 80: the result is 3.0 otherwise if the score is at least 70: the result is 2.0 otherwise if the score is at least 60: the result is 1.0 otherwise: the result is 0.0 2. report the result def computeGrade(score): if score >= 90: grade = "A" elif score >= 80: grade = "B" elif score >= 70: grade = "C" elif score >= 60: grade = "D" else: grade = "F" return grade def computeGrade(score): if score >= 90: grade = 4.0 elif score >= 80: grade = 3.0 elif score >= 70: grade = 2.0 elif score >= 60: grade = 1.0 else: grade = 0.0 return grade >>> computeGrade(93) 4.0 >>> computeGrade(68) 1.0

Decisions Summary The new keywords: if, elif, else Expressing comparisons if value1 == value2: if value1 <> value2: ... do something ... ... do something ... if value1 < value2: if value1 > value2: if value1 <= value2: if value1 >= value2:

Strings Variables can contain strings (sequence of characters) course = "Bioinformatics" fragment = "GATTACA" Representation course ---> Accessing individual elements print course[0] displays B print course[1] displays i print course[13] displays s Finding the length of a string len(course) B i o n f r m a t c s 1 2 3 4 5 6 7 8 9 10 11 12 13

Expressing Repetition General form for variable in range(start, stop): ... DO SOMETHING ... Example >>> for i in range(1, 5): print i 1 2 3 4 >>> 5 (stop value) is not included

Expressing Repetition Write a function that takes two inputs (low, high) representing Fahrenheit temperatures and displays the Celsius equivalents of all temperatures in between To print all Celsius temps. between the values low, high: 1. for each temperature in the range (low, high): 1.1. compute Celsius for current temperature 1.2. print the value from step 1.1. def printCelsius(low, high): for f in range(low, high): c = convert(f) print c >>> printCelsius(32, 40) 0.0 0.555 1.111 1.666 2.222 2.777 3.333 3.888 4.444

Repetition and Strings Write a function that takes one input (a DNA sequence) and displays each individual character from the sequence To print the individual characters in a given DNA string: 1. for each index in the range of valid DNA indices: 1.2 print the DNA character at the current index def printBases(sequence): for index in range(0, len(sequence)): print sequence[index] >>> printBases("GATTACA") G A T C

Repetition and Accumulation Write a function that takes one integer, n, as input and returns the sum of all integers from 1 to n. To add the integers from 1 to the given number n: 1. for each number in the range from 1 to n: update the total sum by adding the current number 2. report the total sum 1. set up a blank variable for the total sum 2. for each number in the range from 1 to n: 3. report the total sum >>> addIntegers(5) 15 >>> addIntegers(6) 21

Repetition and Accumulation Write a function that takes one integer, n, as input and returns the sum of all integers from 1 to n. To add the integers from 1 to the given number n: 1. set up a blank variable for the total sum 2. for each number in the range from 1 to n: update the total sum by adding the current number 3. report the total sum def addIntegers(n): total = 0 for number in range(1, n): total = total + number return total

Executing a Program Line by Line

Computing GC Content Version I with two counters – one for G’s and one for C’s def computeGCcontent(strand): countC = 0 countG = 0 for index in range(0, len(strand)): if strand[index] == "G": countG = countG + 1 elif strand[index] == "C": countC = countC + 1 content = 100.0 * (countG + countC) / len(strand) return content

Computing GC Content Version II with one counter for the total of G’s and C’s def computeGCcontent(strand): countGC = 0 for index in range(0, len(strand)): if strand[index] == "G": countGC = countGC + 1 elif strand[index] == "C": content = 100.0 * countGC / len(strand) return content

Computing GC Content Version III with one counter using the or keyword def computeGCcontent(strand): countGC = 0 for index in range(0, len(strand)): if strand[index] == "G" or strand[index] == "C": countGC = countGC + 1 content = 100.0 * countGC / len(strand) return content

return vs print return sends a value back that can be stored and used print only displays a value on the screen def average(x, y): result = (x + y) / 2.0 return result >>> labs = average(2.0, 4.0) OK >>> grade = labs + average(3.3, 3.6) OK def average(x, y): result = (x + y) / 2.0 print result >>> labs = average(2.0, 4.0) Nothing to store!! >>> grade = labs + average(3.3, 3.6) Nothing to add!!

return vs print return sends a value back that can be stored and used print only displays a value on the screen In functions typically should use return Remember that return terminates the function immediately; put return at the point where the result is ready to report

Decisions Revisited The else portion is not mandatory: if base == "A": count = count + 1 else: count = count This is fine – if the condition fails, we simply do nothing: no need

What is the result from computeGrade(80)? Decisions Revistied Difference between if-elif-elif (chained if) vs. if-if-if What is the result from computeGrade(80)? def computeGrade(score): if score >= 90: grade = "A" elif score >= 80: grade = "B" elif score >= 70: grade = "C" elif score >= 60: grade = "D" else: grade = "F" return grade def computeGrade(score): if score >= 90: grade = "A" if score >= 80: grade = "B" if score >= 70: grade = "C" if score >= 60: grade = "D" else: grade = "F" return grade

Repetition Revisited General form Example for variable in range(start, stop): ... DO SOMETHING ... Example >>> for i in range(1, 5): print i 1 2 3 4 >>> 5 (stop value) is not included

Repetition with Skips Adding a step to skip at regular intervals(optional) : for variable in range(start, stop, step): ... DO SOMETHING ... Example >>> for i in range(1, 9, 2): print i 1 3 5 7 >>> Same outcome for stop value of 8: >>> for i in range(1, 8, 2): print i 1 3 5 7 >>>

Slicing Strings Extracting portions from a given string (slices) course = "Bioinformatics“ course ---> print course[0 : 3] displays Bio print course[3 : 7] displays info print course[5 : 11] displays format print course[10 : 14] displays tics In general course[start : stop] will extract all characters from index start up to (but not including) index stop B i o n f r m a t c s 1 2 3 4 5 6 7 8 9 10 11 12 13

Skipping and Slicing An example that uses skipping and slicing write a function that prints the 3-letter codons in a given RNA >>> printCodons("GAUUACAUGAACGUU") GAU UAC AUG AAC GUU G A U C 1 2 3 4 5 6 7 8 9 10 11 12 13 14 To print the 3-letter codons: 1. for every 3-rd index in the given RNA: - extract the codon at current index - print the codon from previos step

Accumulation (count = count + 1) To keep a running total use the construct: count = count + 1 This just means: evaluate the expression on the right side store the result in the variable on the left side For example >>> total = 0 >>> total = total + 1 # total is now 1 >>> total = total + 1 # total is now 2 >>> total = total + 1 # total is now 3

Accumulation (count = count + 1) Also works with strings, but it means “append” >>> name = "" >>> name = name + "B" # name is now "B" >>> name = name + "i" # name is now "Bi" >>> name = name + "o" # name is now "Bio" However, it could mean “prepend” if the order is flipped >>> name = "B" + name # name is now "B" >>> name = "i" + name # name is now "iB" >>> name = "o" + name # name is now "oiB"

Commenting the Code Comments are programmer’s notes ignored by Python descriptions of the purpose of functions annotations of subtle points in the code annotations of meaning of variables Comments aid in clarifying your programs to other programmers (and to you after a long break) Comments can be added via # – anything after # until the end of the line is ignored by Python # computes the average of the given numbers x and y # e.g. average(20, 40) returns 30 def average(x, y): result = (x + y) / 2.0 return result