Lecture 02
Lecture 02
Python
2
Numbers
• Numbers are referred to as numeric literals 0, 7 , 8 ,123, -256 , 52.36
• Two Types of numbers: ints and floats
• Whole number written without a decimal point called an int (integers)
• e.g. 0 , 5 , 148 , -14 , -1560
• Number written with a decimal point called a float (due to the floating-point
representation)
• e.g. 0.25 , 5.14563 , 148.32 , -14.005 , -15.60 , 0.0 , 1.00 , -1.00
• 0 , 0.0 , 1 = 1.0 , -1 = -1.0
3
Numbers
• Arithmetic Operators
• Addition (+) , subtraction (-) , multiplication (*), division (/) , and
exponentiation (**)
• exponentiation (**): double asterisk, e.g., 2**3
• Result of a division (/) is always a float
• Result of the other operations is a float if either of the numbers is a
float
• Otherwise, it is an int.
4
Variables
• In mathematics problems, quantities are referred to by names
• Names given to the values are called variables
Example: use speed, time … to calculate distance
5
Assignment Statement
• Assignment statements
7
Variable Names (2)
• Names in Python are case-sensitive
• There are thirty-three words, called reserved words (or keywords)
• Have special meanings in Python
• Cannot be used as variable names
• e.g.
• for, if, while, ……
8
Augmented Assignments
• Remember: expression on right side of assignment statement
evaluated before assignment is made
var = var + value
• Python has special operator to accomplish same thing
var += value
9
Integer Operators
• Integer division operator
• Written //
• Modulus operator
• Written %
10
Arithmetic Operations -Summary
11
Parentheses, Order of Precedence
• Parentheses are used to clarify the meaning of an expression
• Order of precedence
1. Terms inside parentheses (inner to outer)
2. Exponentiation
3. Multiplication, division (ordinary(true) and integer), modulus
4. Addition and subtraction
12
type function
• Each value or variable in Python has a type that indicates the kind of
data the value represents.
• You can view a value’s type, with the type built-in function.
type(7)
type(3.14)
type(var)
13
String Literals
• Sequence of characters that is treated as a single item
• Written as a sequence of characters surrounded by either single quotes
(') or double quotes (").
14
String Variables
• Variables also can be assigned string values
• Created (come into existence) the first time they appear in assignment
statements
• When an argument of a print statement
• Quotation marks not included in display
15
String Concatenation
• Two strings can be combined to form a new string
• Consisting of the strings joined together
• Represented by a plus sign
• e.g.,
fullName = "Ali" + "Ahmad"
fullName = "Ali" + " " + "Ahmad"
16
String Repetition
• Asterisk operator can be used with strings to repeatedly concatenate a
string with itself
17
String Manipulation
• Performed by String Functions and Methods
• Assuming str1 = "Python" :
18
print Function
• Used to display its argument(s) as a line of text :
print('Welcome to Python!')
19
input Function
• Used to requests and obtains user input.
name = input("What's your name? ")
• input Always Returns a String
value1 = input('Enter first number: ')
value2 = input('Enter second number: ‘)
value3 = value1 + value2
• If you need a numeric value (int or float), use the built-in functions
int AND float
value1 = int( input('Enter first number: ‘))
value2 = float(input('Enter second number: ‘))
value3 = value1 + value2
20
Error Types
21
Syntax Errors
• Grammatical and punctuation errors are called syntax errors.
22
Runtime Errors
• Errors discovered while code is running called runtime errors or
exceptions
23
Logic Error
• Occurs when the code does not perform the way it was intended
• Example
average = firstNum + secondNum / 2
• Syntax correct, logic wrong: should be
average = (firstNum + secondNum) / 2
• Logic errors are most difficult type of error to locate
24
Numeric Objects in Memory
• Consider this code:
• This is
what
happens:
25