python variables : chapter 2
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 PresentationTRANSCRIPT
PYTHONVARIABLES : CHAPTER 2
FROMTHINK PYTHONHOW TO THINK LIKE A COMPUTER SCIENTIST
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
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’)
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!
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.
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
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
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
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?
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.
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
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