Lesson 2
Lesson 2
PROGRAMMING
INTENDED LEARNING
OUTCOMES
• Python Variables
• Variables
o Variables are containers for storing data values.
REVIEW
• Creating Variables
• A variable is created the moment you first assign a value to it.
• Example
• x=5
y = "John"
print(x)
print(y)
• Casting
o If you want to specify the data type of a variable, this can be done with casting.
• Example
• x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
REVIEW
• DATA TYPES
- STR (TEXT)
- INT (WHOLE NUMBER)
- FLOAT (DECIMAL NUMBERS)
- BOOLEAN (TRUE OR FALSE)
Python Operators
• Is a graphic visual representations of information, data, or
knowledge intended to present information quickly and clearly
• Operators are used to perform operations on
variables and values.
o In the example below, we use the + operator to add together two
values:
o Example
o print(10 + 5)
• Python divides the operators in the following
groups:
o Arithmetic operators
o Assignment operators
o Comparison operators
o Logical operators
o Identity operators
o Membership operators
o Bitwise operators
Python Arithmetic Operators
• a = 21
• b = 10
• c=0
• c=a+b
• print "Line 1 - Value of c is ", c
• c=a-b
• print "Line 2 - Value of c is ", c
• c=a*b
• print "Line 3 - Value of c is ", c
• c=a/b
• print "Line 4 - Value of c is ", c
• c=a%b
• print "Line 5 - Value of c is ", c
• a=2b=3
• c = a**b
• print "Line 6 - Value of c is ", c
Python Assignment Operators
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
Python Assignment Operators
• a = 21
• b = 10
• c=0
• c=a+b
• print "Line 1 - Value of c is ", c
• c += a
• print "Line 2 - Value of c is ", c
• c *= a
• print "Line 3 - Value of c is ", c
• c /= a
• print "Line 4 - Value of c is ", c
• c =2
• c %= a
• print "Line 5 - Value of c is ", c
• c **= a
• print "Line 6 - Value of c is ", c
Python Relational Operators
== Equal x == y
!= Not equal x != y
E1 E2 E1 AND E2 E1 OR E2 NOT
T T T T F
T F F T F
F T F T T
F F F F T
PRACTICE EXERCISE :)
1. Create a program that uses 5 Arithmetic Operators in the given value from
the user.
.
PRACTICE EXERCISE :)
2. Create a program that can determine the relationship of the 2 values given
by the user
THANK YOU