0% found this document useful (0 votes)
13 views25 pages

Lecture 02

Uploaded by

samiabye231
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views25 pages

Lecture 02

Uploaded by

samiabye231
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

CSEN2315 Programming in

Python

Lecture 02- Variables, Strings


and Arithmetic Expressions
1
Outline
• Numbers
• Variables
• Assignment Statement
• Augmented Assignments
• Integer Operators
• Parentheses, Order of Precedence
• Strings
• Error Types

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

Expression on right is evaluated


and the value is assigned to variable
6
Variable Names (1)
• Variable names in Python
• Begin with letter or underscore _
• Can only consist of letters, numbers, underscores
• Recommend using descriptive variable names
• Convention will be
• Begin with lowercase
• Use cap for additional “word” in the name
• Example: rateOfChange

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

Python operation Arithmetic operator Python expression


Addition + f+7
Subtraction – p-c
Multiplication * b*m
Exponentiation ** x ** y
True division / x/y
Integer division // x // y
Modulus (Remainder) % r%s

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 (").

Opening and closing


quotation marks must
be the same type

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!')

– If n is a number, print(n) displays the value of n.


– The print function can display the result of evaluated expressions
– A single print function can display several values

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

• When Python encounters exception


• Terminates execution, displays message

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:

• Numeric objects are called immutable objects.

25

You might also like