180 likes | 506 Views
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.
E N D
PythonVariables : 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. • These values are categorized into types which include numbers(float, integer, string) • Examples: • 23 is an integer • 3.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() function • There 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’)
Variables • A 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 examples • N = 34 • PI = 3.1415926535 • Name = ‘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.
Statements • A 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 script • print 34 • x = 4 • Print x • There are many other statements that we shall learn
Operators and operands • Operators are used to construction expressions • The usual mathematical operators such as +, -, * and / should be quite familiar to you. Here are some example expressions • 2+3 • 7+4*8 #what is the result here? • 3.24 + 5*(4.0-3.2) • 8 – 2**3 # ** is the power (exponentiation) operator • 4.0//3 #divide and drop the decimal (floor division) • 4/3 #also floor division. Result is 1 • 4.0/3 # result here is 1.3333333333333333
Expression evaluation in interactive mode • >>>2+2 • 4 • >>> 4//3 • 1 • >>> 34 % 3 # divide and return remainder (modulus) • 1 • >>>2*2**3 # ** has higher precedence than * • 16
Order of Operations • Parentheses has the highest precedence • Exponentiation • Multiplication and Division • Addition and Subtraction • Operators of equal precedence are L to R • PEMDAS • >>> 2+4*3**2-5-10/2 gives what?
String Operators • There are two operators that work on strings (+ and *) • The + operators performs concatenation • >>> ‘Soft’+’ware’ • Software • The * operators duplicates a string. For example • >>> ‘Soft’*4 • SoftSoftSoftSoft • The others don’t work.
Comments • Comments can be anywhere, but must follow # • The interpreter will not read anything past the # symbol • #This is a comment • X = 4 # so is this • Start every program that you turn in with the following comment collection • # Project # : Topic name • # Your name • # Explain what the program does
Exercises • Assume that the following statements are executed • Val = 10 • Len =15.5 • Spacer = ‘_’ • What do the following valuate to? • Val/3 • Val/2.0 • int(Len)*2 • Spacer*10 • Do exercise 2.4 page 16 (all three) and study glossary 2.11