Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Types and Arithmetic Operators
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”?
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Python November 14, Unit 7. Python Hello world, in class.
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”?
Python Programming Chapter 2: Variables, expressions, and statements Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
The If/Else Statement, Boolean Flags, and Menus Page 180
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Chapter 4 Making Decisions
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
Programming in Python Part I Dr. Fatma Cemile Serçe Atılım University
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Input, Output, and Processing
INLS 560 – C ONDITIONALS Instructor: Jason Carter.
Flow of Control Part 1: Selection
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Introduction to Python and programming Ruth Anderson UW CSE 140 Winter
Variables, Expressions and Statements
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Python Conditionals chapter 5
Chapter Making Decisions 4. Relational Operators 4.1.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Python Let’s get started!.
Variables, Types, Expressions Intro2CS – week 1b 1.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Decision Making CMSC 201 Chang (rev ).
PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Topics Designing a Program Input, Processing, and Output
CprE 185: Intro to Problem Solving (using C)
Introduction to Python
INC 161 , CPE 100 Computer Programming
Line Continuation, Output Formatting, and Decision Structures
Making Choices with if Statements
Chapter 4: Making Decisions.
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
Topics The if Statement The if-else Statement Comparing Strings
Line Continuation, Output Formatting, and Decision Structures
Selection CIS 40 – Introduction to Programming in Python
And now for something completely different . . .
Chapter 7 Conditional Statements
Adding Intelligence Check for the presence or absence of a condition in the environment Take the appropriate actions Involves making a choice (to do or.
SELECTIONS STATEMENTS
Boolean Expressions to Make Comparisons
DATA TYPES AND OPERATIONS
Introduction to Python
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures

Values, Types and Variables  1, 2, 3.14 and ‘Hello, World!’ are values  Values belong to types: int, float, str, bool …  If you aren’t sure what type something is you can ask >>> type (3.2)  >>> type (True)   An assignment statement creates new variables at run-time >>> n = 17 >>> type (n)   A variable name starts with letter, followed by 0 or more letters, digits or _ ◦Choose an incorrect variable name, get error >>> = 1000  Syntax error: invalid syntax

31 Reserved Words

Statements  Unit of code the interpreter executes and if there is a result, shows it  Seen the print and the assignment  Several statements are a script >>> print 1 >>> x =2 >>> print x  Produces the output 1 2

Operators  Operators are special symbols representing operations (such as, addition, multiplication, etc.) + - * / ** (exponentiation)  Values they operate on are called operands  If both operands are integers, performs integer operation in Python 2.7 >>> minutes = 59 >>>print minutes / 60 0  In Python 3.0 // is integer division, otherwise does floating point division  Modulus operator 7 % 3  1

Expressions  An expression is a combination of values, operands and operators  If you type an expression in interactive mode, it will show you the result >>>  Order of operations PEMDAS

String Operations  + is concatenation >>> print ‘100’ + ‘150’  Python has a "string format" operator %. This functions analogous to printf format strings in C "foo=%s bar=%d" % ("blah", 2)  "foo=blah bar=2"  In Python 3 and 2.6+, this was supplemented by the format() method of the str class "foo={0} bar={1}".format("blah", 2).

Input  raw_input gets input from keyboard  >>> name = raw_input (‘What is your name?\n’)  Chuck  >>> print name  Chuck  >>> speed = raw_input(‘How fast were you going?\n)  23  >>> int (speed)  >>> print speed + 5  28  If user types in a non-integer  ValueError: invalid literal for int( )  In Python 3.0 this function is input

Comments #  #compute the percentage of the hour that has elapsed  percentage = int ( (float(minutes) / 60) * 100)  Or  percentage = int ((float (minutes)/60) * 100 ) #percentage of hour  Don’t do this!  v = 5 #assign 5 to v  Better – use a good variable name  velocity = 5

Boolean Expressions  Expression that is either true or false  Comparison operator == != = is and is not  Three logical operators and or and not >>>not (True)  False  0 is interpreted as false  Any nonzero number is interpreted as True >>> 17 and True  True  Comparisons may be chained, for example a <= b <= c.

Conditional statements if x > 0: print ‘x is positive’j #NOTE indentation!!! if x % 2 = 0: print ‘x is even’ else: print ‘x is odd’ if x > 0: print ‘x is positive’ elif x < 0: #NOTE elif print ‘x is negative’ else print ‘x is zero’

Nested Conditionals  Indentation is IMPORTANT if x == y: print ‘x is same as y’ else: if x < y: print ‘x is less than y’ else: print ‘x is greater than y’

Nested Conditionals  Indentation is IMPORTANT if x == y: print ‘x is same as y’ else: if x < y: print ‘x is less than y’ else: print ‘x is greater than y’  Gives an error IndentationError: unindent does not match any outer indentation level

Error Conditionals- try/catch strtemp = input (‘Enter Fahrenheit temperature: ‘) try: fahr = float (strtemp) cel = (fahr ) * 5.0 /9.0 print cel except: print (‘Please enter a number’)

Short Circuiting  Expressions are evaluated left to right  If nothing to be gained from evaluating further (because value already known), it short circuits if num != 0 and sum / num > 0 : print (sum / num) else: print (‘Cannot do division’)

switch  Python doesn’t have one  Can fake with an array or dictionary or lambdas  More on that later