Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.

Slides:



Advertisements
Similar presentations
Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements.
Advertisements

Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Python Programming Language
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.
Introduction to C Programming
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
16-May-15 Sudden Python Drinking from the Fire Hose.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Jay Summet CS 1 with Robots IPRE Python Review 1.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Variables and I/O. Types Strings –Enclosed in quotation marks –“Hello, World!” Integers –4, 3, 5, 65 Floats –4.5, 0.7 What about “56”?
JavaScript, Third Edition
CSCI/CMPE 4341 Topic: Programming in Python Chapter 3: Control Structures (Part 1) – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
Geography 465 Assignments, Conditionals, and Loops.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
4. Python - Basic Operators
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Python Lesson Week 01. What we will accomplish today Install Python on your computer Using IDLE interactively to explore String Variables if/else while.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Input, Output, and Processing
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
A Sample Program public class Hello { public static void main(String[] args) { System.out.println( " Hello, world! " ); }
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
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.
String and Lists Dr. José M. Reyes Álamo.
Python Review 1.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Python Variable Types.
Tuples and Lists.
Chapter 4: Making Decisions.
2.5 Another Java Application: Adding Integers
Basic operators - strings
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Chapter 4: Making Decisions.
Arithmetic operations, decisions and looping
And now for something completely different . . .
Introduction to C++ Programming
Lists in Python.
String and Lists Dr. José M. Reyes Álamo.
Selection Statements.
Chapter 4: Control Structures I (Selection)
Python Primer 1: Types and Operators
CHAPTER 4: Lists, Tuples and Dictionaries
Introduction to Computer Science
Python Review
Class code for pythonroom.com cchsp2cs
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Presentation transcript:

Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from left hand operand *MultiplicationMultiplies values on either side of the operator /DivisionDivides left hand operand by right hand operand %ModulusDivides left hand operand by right hand operand and returns remainder **ExponentPerforms exponential (power) calculation on operands

Python Variables What’s Variable? Variables are like labels. It describes a place to store information. Python uses the equal sign (=) to assign values to variables. The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable. Python has five standard data types Numbers (integer, floating point number) String List Tuple Dictionary

Naming Convention ● It can be only one word ● It can use only letters, numbers, and the underscore (_) character ● It can't begin with a number Valid variable namesInvalid variable names maxAmountmax-Amount max_amountmax amount SPAM42 _spamtotal_$um, 'hello' account44account

Compound Data Types Strings – Enclosed in Quotes, holds characters, (immutable): “This is a String” Tuples – Values separated by commas, (usually enclosed by parenthesis) and can hold any data type (immutable): (4, True, “Test”, 34.8)‏ Lists – Enclosed in square brackets, separated by commas, holds any data type (mutable): [4, True, “Test”, 34.8] Dictionaries – Enclosed in curly brackets, elements separated by commas, key : value pairs separated by colons, keys can be any immutable data type, values can be any data type: { 1 : “I”, 2 : ”II”, 3 : “III”, 4 : “IV”, 5 : “V” }

String myString = “This is a test.” You can access a specific element using an integer index which counts from the front of the sequence (starting at ZERO!)‏ myString[0] produces 'T' myString[1] produces 'h' myString[2] produces 'i' myString[3] produces 's' The len() function can be used to find the length of a sequence. Remember, the last element is the length minus 1, because counting starts at zero! myString[ len(myString) – 1] produces '.'

List Lists are a mutable data type that you can create by enclosing a series of elements in square brackets separated by commas. The elements do not have to be of the same data type: myList = [ 23, True, 'Cheese”, ] Unlike Strings and tuples, individual elements in a list can be modified using the assignment operator. After the following commands: myList[0] = True myList[1] = 24 myList[3] = “Boo” myList contains: [ True, 24, 'Cheese', 'Boo' ]

Tuple A tuple is like a list that uses parenthese. Numbers=(0,1,2,3,4,5) The difference is you can’t change. Wrong: Numbers[0]=6

OperatorMeaning ==Equal to !=Not equal to <Less than >Greater than <=Less than or equal to >=Greater than or equal to Comparison Operators Logical Operators OperatorMeaning andTrue if both the operands are true orTrue if any of the two operands are non-zero notReverse the logical state of the operand

Exercises: >>>a=6 >>>(a>4) or (a <8) >>>4<a<8 >>>1<a<3 >>> a!=66 >>>a='hello' >>>a == 'hello' >>>not a>3 >>>not 0 >>>b='hello' >>>b=="hello"

If you have two mutually exclusive choices, and want to guarantee that only one of them is executed, you can use an IF/ELSE statement. The ELSE statement adds a second block of code that is executed if the boolean expression is false. if boolean_expression : STATEMENT else: STATEMENT Flow Control

IF/ELSE Example: numberOfWheels = 3 if ( numberOfWheels < 3): print(“You are a motorcycle!”) else: print(“You are a Car!”) print(“You have”, numberOfWheels, “wheels”) The last print statement is executed no matter what. If numberOfWheels is less than 3, it's called a motorcycle, otherwise it's called a car!

IF/ELIF/ELSE If you have several mutually exclusive choices, and want to guarantee that only one of them is executed, you can use an IF/ELIF/ELSE statements. The ELIF statement adds another boolean expression test and another block of code that is executed if the boolean expression is true. if boolean_expression : STATEMENT elif 2 nd_ boolean_expression ): STATEMENT else: STATEMENT

IF/ELIF/ELSE Example: numberOfWheels = 3 if ( numberOfWheels == 1): print(“You are a Unicycle!”) elif (numberOfWheels == 2): print(“You are a Motorcycle!”) elif (numberOfWheels == 3): print(“You are a Tricycle!”) elif (numberOfWheels == 4): print(“You are a Car!”) else: print(“That's a LOT of wheels!”) Only the print statement from the first true boolean expression is executed.

Input Function userName = input(“What is your name?”)‏ userAge = int( input(“How old are you?”)‏ ) birthYear = userAge print(“Nice to meet you, “ + userName) print(“You were born in: “, birthYear) input() is guaranteed to give us a string, no matter WHAT the user enters. But what happens if the user enters “ten” for their age instead of 10?