Variables, Expressions and Statements

Slides:



Advertisements
Similar presentations
Fundamentals of Python: From First Programs Through Data Structures Chapter 2 Software Development, Data Types, and Expressions.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
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 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.
Introduction to Python
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
JavaScript, Fourth Edition
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Basic Input/Output and Variables Ethan Cerami New York
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Python Programming Fundamentals
Introduction to Python
Fundamentals of Python: First Programs
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
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
PhD, Senior Lecturer, Baimuratov Olimzhon A LGORITHMS & P ROGRAMMING (P YTHON ) Lecture 2 From SDU:
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Week 1 Algorithmization and Programming Languages.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
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.
Variables, Expressions, and Statements
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏ Sec 9-7 Web Design.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Chapter 2 Variables.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
Python Let’s get started!.
PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Data Manipulation Variables, Data Types & Math. Aug Variables A variable is a name (identifier) that points to a value. They are useful to store.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Introduction to Python Lesson 2a Print and Types.
Fundamentals of Programming I Overview of Programming
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
Variables, Expressions, and Statements
Introduction to C++ Programming
Variables, Expressions, and Statements
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Variables, Data Types & Math
Variables, Data Types & Math
Introduction to Programming with Python
Variables, Expressions, and Statements
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Terminal-Based Programs
Variables, Data Types & Math
Text Copyright (c) 2017 by Dr. E. Horvath
Introduction to C Programming
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Variables, Expressions and Statements Python – Part 2

Prepared by Department of Preparatory year Values and Types Values Basic things program works with e.g. letter, number 1, 2, ‘Hello World!’ Types Values belong to different types 2 is an interger ‘Hello World!’ is a string Prepared by Department of Preparatory year

Prepared by Department of Preparatory year Numbers Integers: 12 0 -12987 0123 0X1A2 Type ‘int’ Can’t be larger than 2**31(2 31) Octal literals begin with 0 (0981 illegal!) Hex literals begin with 0X, contain 0-9 and A-F Floating point: 12.03 1E1 -1.54E-21 Type ‘float’ Same precision and magnitude as C double Long integers: 10294L Type ‘long’ Any magnitude Python usually handles conversions from int to long Complex numbers: 1+3J Type ‘complex’ Prepared by Department of Preparatory year

Prepared by Department of Preparatory year String >>> print "Per's lecture“ Per's lecture Single quotes or double quotes can be used for string literals Produces exactly the same value Special characters in string literals: \n newline, \t tab, others Triple quotes useful for large chunks of text in program code >>> print "One line.\nAnother line.“ One line. Another line. >>> print """One line, another line.""“ One line, another line. Prepared by Department of Preparatory year

Prepared by Department of Preparatory year Values and Types Print statement for integers >>>print 4 4 Can check the value type >>> type(‘Hello world!’) <type ‘str’> >>> type (17) <type ‘int’> Prepared by Department of Preparatory year

Prepared by Department of Preparatory year Values and Types Strings belog to the type str Integers belong to the type int Numbers with a decimal point belong to the type float. >>>type (3.2) <type ‘float’> Prepared by Department of Preparatory year

Prepared by Department of Preparatory year Values and Types What about ’17’ and ‘3.2’? >>>type (’17’) <type ‘str’> >>>type (‘3.2’) Prepared by Department of Preparatory year

Prepared by Department of Preparatory year Values and Types >>>print 1,000,000 Output ? 1 0 0 Semantic error Prepared by Department of Preparatory year

Prepared by Department of Preparatory year Variables Variable is a name that refers to a value Assignment statement creates new variables and gives them values. >>>message=‘New message’ >>>n=17 >>>pi=3.1415926535897931 Prepared by Department of Preparatory year

Prepared by Department of Preparatory year Variables Use print statement to display the value of a variable >>> print n 17 >>> print message New message Prepared by Department of Preparatory year

Prepared by Department of Preparatory year Variables Type of variable is the type of value it refers to(The type of the variable is determined by Python) >>>type (pi) <type ‘float’> >>>type (n) <type ‘int’> >>>type (message) <type ‘str’> Prepared by Department of Preparatory year

Prepared by Department of Preparatory year Variable Names Can contain both letters and numbers Begin with a letter Good idea to begin variable names with a lowercase letter Underscore character (_) can appear in a name (often in names with multiple words), e.g. my_name The variable name is case sensitive: ‘val’ is not the same as ‘Val’ Prepared by Department of Preparatory year

Prepared by Department of Preparatory year Variable Names >>> 76trombones = 'big parade' SyntaxError: invalid syntax >>>more@=100000 SytaxError: invalid syntax >>>class =‘CS104’ Class -> one of Python’s keywords Prepared by Department of Preparatory year

Prepared by Department of Preparatory year Python Keywords And del from not while As elif global or with Assert else if pass yield Break except import print Class exec in raise Continue finally is return Def for lambda try Prepared by Department of Preparatory year

Prepared by Department of Preparatory year Statements Unit of code that Python interpreter can execute (print, assignment statement) print 1 x=2 print x Prepared by Department of Preparatory year

Operators and operands Operators – special symbols that represent computations (e.g. addition, division) Values the operator is applied to are called operands + addition - subtraction * multiplication / division ** exponentiation % modulus Prepared by Department of Preparatory year

Operators and operands 20+32 Hour-1 Hour*60+minute 5**2 (5+9)*(15-7) 7%3 Prepared by Department of Preparatory year

Operators and Operands >>>minute=59 >>>minute/60 0 ? Prepared by Department of Preparatory year

Operators and operands If both operands are integers, result is also an integer If either of the operands is a floating-point number Python performs floatin-point division; result is a float >>>minute/60.0 0.98333333333333328 In Python 3.0 or later the result is a float // operator performs integer division Prepared by Department of Preparatory year

Prepared by Department of Preparatory year Expressions Combination of values, variables and operators 17 X X+17 Prepared by Department of Preparatory year

Prepared by Department of Preparatory year Expressions >>>1+1 2 In a script, expression by itself doesn’t do anything. Prepared by Department of Preparatory year

Prepared by Department of Preparatory year Order of operations Order of evaluation depends on rules of precedence. Python follows mathematical convention Parentheses – highest precedence Exponentiation –next highest precedence Multiplication, Division, Modulus (same precedence) Addition and Subtraction (same prec.) Same precedence operators – left to right Prepared by Department of Preparatory year

Prepared by Department of Preparatory year Order of operations 2*(3-1) (1+1)**(5-2) 2**1+1 3*1**3 2*3-1 6+4/2*3 7%3+8/2 Prepared by Department of Preparatory year

Prepared by Department of Preparatory year String operations Concatenation operator + first =‘CS’ second=‘104’ print first+second Repitition operator * ‘spam’*3 ‘spamspamspam’ Prepared by Department of Preparatory year

Prepared by Department of Preparatory year Comments Notes that can be added to program to explain what the program is doing Start with the # symbol #compute the percentage of the hour that has elapsed percentage=(minute*100)/60 percentage=(minute*100)/60 #percentage of an hour Prepared by Department of Preparatory year

Reading Input from the Keyboard Programs commonly need to read input typed by the user on the keyboard. We will use the Python functions to do this. Python uses built-in functions to read input from the keyboard. A function is a piece of prewritten code that performs an operation and then returns a value back to the program. The input function can be used to read numeric data from the keyboard.

Reading Input from the Keyboard Reading Numbers with the input Function Use the input function in an assignment statement: variable = input (prompt) where, variable name of the variable that will reference the data = assignment operator input name of the function prompt string that is displayed on the screen For example: hours = input (‘How many hours did you work?’)

Reading Input from the Keyboard Reading Strings with the raw_input Function The raw_input function retrieves all keyboard input as a string. >>> name = raw_input(‘Enter your name:’) >>> print name Enter your name: Ahmad

Prepared by Department of Preparatory year Write scripts in IDLE Now we need to write proper scripts, saved in files In IDLE: 'File' 'New Window' Do immediately 'Save as…' Browse to directory 'Desktop' Create a directory 'Python course' Go down into it Enter the file name 't1.py' Save Work in the window called 't1.py' Enter the following code: Save the file: Ctrl-S, or menu 'File', 'Save' Run the script: F5, or menu 'Run', 'Run Module' "file Ex1.py" # this is a documentation string print "Hello world!" Prepared by Department of Preparatory year

End Part 2