comp_python_lecture_03
comp_python_lecture_03
Programming (COMP-122)
Lecture 3: Data Types and Operators in Python
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
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.