python variables : chapter 2

12
PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST

Upload: joey

Post on 23-Feb-2016

49 views

Category:

Documents


0 download

DESCRIPTION

Python Variables : chapter 2. From Think Python How to Think Like a Computer Scientist. Values and Types. A value is one of the basic data objects that a Python program works with. Two of the most used values are numbers and strings of letters. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Python Variables : chapter 2

PYTHONVARIABLES : CHAPTER 2

FROMTHINK PYTHONHOW TO THINK LIKE A COMPUTER SCIENTIST

Page 2: Python Variables : chapter 2

VALUES AND TYPESA value is one of the basic data objects that a Python program works with. Two of the most used values are numbers and strings of letters.These values are categorized into types which include numbers(float, integer, string)Examples:23 is an integer3.14159 is a floating point (float for short)‘Hello World!’ is a string of characters

>>> print 23, 3.14,’This is a cool class’23 3.14 This is a cool class

Page 3: Python Variables : chapter 2

TYPE() FUNCTIONThere is a function called type() that will give you (return) the type of a value.>>>type(23)<class ‘int’>>>>type(3.456)<class ‘float’>>>>type(‘Hello’)<class ‘str’>>>>type(‘45’) Note this one!!!!<class ‘str’)

Page 4: Python Variables : chapter 2

VARIABLESA variable is a name that refers to a value.A assignment statement is the usual way to create variables and assign values to them. Here are a few examplesN = 34PI = 3.1415926535Name = ‘Richard Simpson’

The type of a variable is the type of the value it is assigned to. For example>>>type(PI)<class ‘float’)Note: if you change the value to a different type its type will change also!

Page 5: Python Variables : chapter 2

VARIABLE NAMES• Variables names can be arbitrarily long. • The can contain numbers and letters and underscore. • Upper and lower case is acceptable. • A variable name cannot start with a number• Keywords are not allowed. These include

• And, as, break, if, else, elif, for, from, import, in, not, or, return, while with, as well as some others.

Page 6: Python Variables : chapter 2

STATEMENTSA statement (instruction) is a unit of code that the interpreter can execute. At this point we have only seen two statements, the print statement and the assignment statement. These are executed one at a time in the interactive mode.A script, ie as sequence of instructions, will also execute one at a time but much faster.Here is a scriptprint 34x = 4Print xThere are many other statements that we shall learn

Page 7: Python Variables : chapter 2

OPERATORS AND OPERANDSOperators are used to construction expressionsThe usual mathematical operators such as +, -, * and / should be quite familiar to you. Here are some example expressions2+37+4*8 #what is the result here?3.24 + 5*(4.0-3.2)8 – 2**3 # ** is the power (exponentiation) operator4.0//3 #divide and drop the decimal (floor division)4/3 #also floor division. Result is 14.0/3 # result here is 1.3333333333333333

Page 8: Python Variables : chapter 2

EXPRESSION EVALUATION IN INTERACTIVE MODE>>>2+24>>> 4//31>>> 34 % 3 # divide and return remainder (modulus)1>>>2*2**3 # ** has higher precedence than *16

Page 9: Python Variables : chapter 2

ORDER OF OPERATIONS1. Parentheses has the highest precedence2. Exponentiation3. Multiplication and Division4. Addition and Subtraction5. Operators of equal precedence are L to R

PEMDAS>>> 2+4*3**2-5-10/2 gives what?

Page 10: Python Variables : chapter 2

STRING OPERATORSThere are two operators that work on strings (+ and *)The + operators performs concatenation>>> ‘Soft’+’ware’ Software

The * operators duplicates a string. For example>>> ‘Soft’*4SoftSoftSoftSoftThe others don’t work.

Page 11: Python Variables : chapter 2

COMMENTSComments can be anywhere, but must follow #The interpreter will not read anything past the # symbol#This is a commentX = 4 # so is thisStart every program that you turn in with the following comment collection# Project # : Topic name# Your name# Explain what the program does

Page 12: Python Variables : chapter 2

EXERCISESAssume that the following statements are executedVal = 10Len =15.5Spacer = ‘_’What do the following valuate to?Val/3Val/2.0int(Len)*2Spacer*10Do exercise 2.4 page 16 (all three) and study glossary 2.11