Introduction to Computer
Programming (COMP-122)
Lecture 3: Data Types and Operators in Python
CSIT Department | MIT
Outline
+Python Data Types
+Type Casting
+Arithmetic Operators
+Assignment Operators
+Operator precedence
+Expressions
Python Data Types
+ In programming, data type is an important concept.
+ Variables can store data of different types, and different types can do different things.
+ Python has the following data types built-in by default, in these categories:
Strings
+A string is simply any grouping of characters you place within
single, double or triple quotes.
+Strings in python are surrounded by either single quotation
marks, or double quotation marks.
+'hello' is the same as "hello".
+You can display a string literal with the print() function.
+Example:
print("Hello")
OR
a = "Hello"
print(a)
String Concatenation
+The meaning of an operator may change based on the data
types of the values next to it.
o For example, + is the addition operator when it operates on two
integers or floating-point values.
o However, when + is used on two string values, it joins the strings as the
string concatenation operator.
o For example:
>>> 'Alice' + 'Bob'
'AliceBob'
String Replication
+The * operator is used for multiplication when it operates on
two integer or floating-point values.
o But when the * operator is used on one string value and one integer
value, it becomes the string replication operator.
o For example, enter a string multiplied by a number into the interactive
shell to see this in action.
>>> 'Alice' * 5
'AliceAliceAliceAliceAlice'
Numeric Type
+There are three numeric types in Python:
o integer
o float
o complex
Integer
+ int, or integer, is a whole number, positive or negative, without decimals, of unlimited
length.
+ Any whole number is an integer. For example, the value 1 is a whole number, so it’s an
integer. On the other hand, 1.0 isn’t a whole number; it has a decimal part to it, so it’s not
an integer. Integers are represented by the int data type.
+ Example:
x = 1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
Float
+ Float, or "floating point number" is a number, positive or negative, containing one or more
decimals.
+ Any number that includes a decimal portion is a floating-point value. For example, 1.0 has a
decimal part, so it’s a floating-point value.
+ Floating-point values have an advantage over integer values in that you can store immensely
large or incredibly small values in them.
+ Float can also be scientific numbers with an “e” or “E” to indicate the power of 10.
+ Example:
x = 1.10
y = 35e3
z = -35.59
print(type(x))
print(type(y))
print(type(z))
Complex Numbers
+A complex number consists of a real number and an imaginary number that
are paired together
+Complex numbers are written with a "j“ or “J” as the imaginary part:
x = 3+5j
y = 5J
z = -5j
print(type(x))
print(type(y))
print(type(z))
Type Conversion
+ You can convert from one type to another with the int(), float(), and complex() methods.
+ Example:
x = 1 # int
y = 2.8 # float
z = 1j # complex
#convert from int to float:
a = float(x)
#convert from float to int:
b = int(y)
#convert from int to complex:
c = complex(x)
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
Type Casting
+There may be times when you want to specify a type on to a
variable.
+Casting in python is therefore done using constructor functions:
• int() - constructs an integer number from an integer literal, a float
literal (by removing all decimals), or a string literal (providing the
string represents a whole number)
• float() - constructs a float number from an integer literal, a float literal
or a string literal (providing the string represents a float or an integer)
• str() - constructs a string from a wide variety of data types, including
strings, integer literals and float literals
Examples of Type Casting
a = int(1) # a will be 1
b = int(2.8) # b will be 2
c = int("3") # c will be 3
p = float(1) # p will be 1.0
q = float(2.8) # q will be 2.8
r = float("3") # r will be 3.0
s = float("4.2") # s will be 4.2
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'
Python Operators
+Operators are used to perform operations on variables and
values.
+Python divides the operators in the following groups:
o Arithmetic operators
o Assignment operators
o Comparison / Relational operators
o Logical operators
o Identity operators
o Membership operators
o Bitwise operators
Arithmetic Operators
+Arithmetic operators are used with numeric values to perform
common mathematical operations.
+Refer to the next slide.
Arithmetic Operator Precedence
+When an expression contains more than one operator, the order
of evaluation depends on the order of operations.
+For mathematical operators, Python follows mathematical
convention. The acronym PEMDAS is a useful way to remember
the rules:
o Parentheses have the highest precedence and can be used to force an
expression to evaluate in the order you want.
o Exponentiation has the next highest precedence.
o Multiplication, Modulo and Division have higher precedence than
Addition and Subtraction.
o Operators with the same precedence are evaluated from left to right
(except exponentiation).
Assignment Operators
+Assignment operators are used to assign values to variables:
Comparison Operators
+Comparison operators are used to compare two values:
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Expressions in Python
+An expression is a combination of operators and operands that is
interpreted to produce some other value.
+An expression is a combination of values, variables, and
operators. A value all by itself is considered an expression, and
so is a variable.
+An expression is evaluated as per the precedence of its operators.
o So that if there is more than one operator in an expression, their
precedence decides which operation will be performed first.
o We have many different types of expressions in Python.
Constant Expressions
+These are the expressions that have constant values only.
# Constant Expressions
x = 15 + 1.3
print(x)
Arithmetic Expressions
+ An arithmetic expression is a combination of numeric values, operators, and sometimes
parenthesis.
o The result of this type of expression is also a numeric value.
o The operators used in these expressions are arithmetic operators like addition, subtraction, etc.
o Example:
# Arithmetic Expressions
x = 40
y = 12
add = x + y
sub = x - y
pro = x * y
div = x / y
print(add)
print(sub)
print(pro)
print(div)
Multiple Operators in Expression
+It’s a quite simple process to get the result of an expression if
there is only one operator in an expression.
+But if there is more than one operator in an expression, it may
give different results on basis of the order of operators executed.
+To sort out these confusions, the operator precedence is defined.
Operator Precedence simply defines the priority of operators
that which operator is to be executed first.
+ Here we see the operator precedence in Python, where the
operator higher in the list has more precedence or priority:
Practice Work
+What is the output of the following code in Python:
o print(2 + 3 * 4)
o x = 5; y = 6; print(x == y)
o print(3 / 2)
o x = 5; x += 3; print(x)
o x = 5; x *= 3; print(x)
o print(2 + 3 * 4 / 2 % 5)
o print(2 ** 3 ** 2)
+How would you convert the result of 2 / 3 in Python 3 to an integer?
+How would you perform floor division in Python? Provide an example?
End.