Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

One Dimensional Arrays
Algorithms CSC1310 Fall What Is Programming? Programming Programming means writing down a series of instructions that tell a computer what to do.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Conditional Statements Introduction to Computing Science and Programming I.
Structured programming
Lecture 1 The Basics (Review of Familiar Topics).
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.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
Geography 465 Assignments, Conditionals, and Loops.
Python Control of Flow.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
The University of Texas – Pan American
4. Python - Basic Operators
Fundamentals of Python: First Programs
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python Lab: Control Statements Conditionals, Loops, Iterations Proteomics Informatics, Spring 2014 Week 4 18 th Feb, 2014
Introduction to Python 2 Dr. Bernard Chen University of Central Arkansas PyArkansas 2011.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
If statements while loop for loop
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
Chapter 14 Advanced Function Topics CSC1310 Fall 2009.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
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.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Chapter 10 Loops: while and for CSC1310 Fall 2009.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Fortran: Control Structures Session Three ICoCSIS.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
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.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
String and Lists Dr. José M. Reyes Álamo.
Selection and Python Syntax
While Loops in Python.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Arrays, For loop While loop Do while loop
Chapter 8 JavaScript: Control Statements, Part 2
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Iteration: Beyond the Basic PERFORM
3 Control Statements:.
String and Lists Dr. José M. Reyes Álamo.
Introduction to Programming Using Python PART 2
Computer Science Core Concepts
Chapter8: Statement-Level Control Structures April 9, 2019
For loops Taken from notes by Dr. Neil Moore
Python Basics with Jupyter Notebook
CHAPTER 5: Control Flow Tools (if statement)
COMPUTER PROGRAMMING SKILLS
While Loops in Python.
Class code for pythonroom.com cchsp2cs
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas

If Statement The main statement used for selecting from alternative actions based on test results It’s the primary selection tool in Python and represents the Logic process

Some Logic Expression Equal: “==” NOT Equal: “!=” Greater: “>”, “>=” Less than: “<”, “<=”

Outline Simple If statement If... else statement If… else if … else statement

Simple If statement It takes the form of an if test if :

Simple If statement if 4>3: print “it is true that 4>3” if 1: print “true” a=10 if a==10: print “a=10”

If... else statement It takes the form of an if test, and ends with an optional else block if : else:

If... else statement if 3>4: print “3 is larger than 4” else: print “3 is NOT larger than 4” if 0: print “true” else: print “false”

If... else statement a=10 if a==10: print “a=10” else: print “a is not equal to 10” a=10 if a!=10: print “a=10” else: print “a is not equal to 10” (also try >, < )

If… else if … else statement It takes the form of an if test, followed by one or more optional elif tests, and ends with an optional else block if : elif : else:

If… else if … else statement a=10 if a>10: print “a > 10” elif a==10: print “a = 10” else: print “a < 10”

If… else if … else statement example number = 23 guess = int(raw_input('Enter an integer : ')) if guess == number: print 'Congratulations, you guessed it.' # New block starts here print "(but you do not win any prizes!)" # New block ends here elif guess < number: print 'No, it is a little higher than that' # Another block # You can do whatever you want in a block... else: print 'No, it is a little lower than that' # you must have guess > number to reach here

Some More Logic Expression and >>> 5>4 and 8>7 True >>> 5>4 and 8>9 False

Some More Logic Expression or >>> 5>4 or 8>7 True >>> 5>4 or 8>9 True

For loop The for loop is a generic sequence iterator in Python It can step through the items in ANY ordered sequence object

For loop The Python for loop begins with a header line that specifies an assignment target along with an object that you want to step through for in : When Python runs a for loop, it assigns item in the sequence object to the target “one by one”, and then executes the loop body

For loop The name used as the assignment target in a for header line is usually a variable in the scope where the for statement is coded After the loop, this variable normally still refers to the last item visited

For loop examples The name x is assigned to each of the three items in the list in turn, from left to right >>> aa=[“spam”, “eggs”, “ham”] >>> for i in aa: print i

For loop examples >>> aa=[1,2,3,4] >>> sum=0 >>> for i in aa: sum = sum + i >>> product=1 >>> for i in aa: product = product * i >>> sum, product

For loop examples The name x is assigned to each of the three items in the list in turn, from left to right >>> s=“string in python” >>> for i in s: print i

Loop Variations There are also situations where you will need to iterate in a more specialized way in the loop operation. For example, what if you need to visit every second or third item in a list? We have “range” function to help

Counter loops: range The range function is really independent of for loops; although it’s used most often to generate indexes in a for loop There are three formats of range: >>> range(5) >>> range(2,5) >>> range(0,10,2)

Counter loops: range With one argument, range generates a list with integers from zero up to but NOT including the argument’s value If you pass in two arguments, the first is taken as the lower bound An optional third argument can give a step; if used, Python adds the step to each successive integer in the result

Counter loops: range >>> range(5) [0, 1, 2, 3, 4] >>> range(2,5) [2, 3, 4] >>> range(0,10,2) [0, 2, 4, 6, 8]

Counter loops: range Ranges can also be non-positive, and non- ascending, if you want them to be: >>> range(-5,5) [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4] >>> range(5,-5,-1) [5, 4, 3, 2, 1, 0, -1, -2, -3, -4]

range in for loop >>> sum=0 >>> for i in range(100): sum = sum + i >>> product=1 >>> for i in range(5,10) product = product * 10

range in for loop >>> aa=[1,2,3,4] >>> sum=0 >>> for i in aa: sum = sum + i >>> sum=0 >>> for i in range(len(aa)): >>> sum = sum + aa[i]

range in for loop aa= [90,80,75,60,80,77,65,30,50,100] If we only want to compute the sum of first 5 scores: >>> sum=0 >>> for i in range(5): >>> sum=sum + aa[i] If we only need the even number student’s score: >>> sum=0 >>> for i in range(0,len(aa),2): >>> sum=sum + aa[i]

For loop examples Please Draw the following figure by using for loop * ** *** **** *****

Python Official Sites Python Tutorial Python Official Site Download Python Latest Version