Chapter 1-Introduction To Python Programming
Chapter 1-Introduction To Python Programming
By
2
Python as Calculator
• Arithmetic operation calculated and returning answer
3
Python as Calculator (cont.)
4
Math Module
• Python has many basic arithmetic functions like sin, cos, tan, asin,
acos, atan,exp, log, log10 and sqrt.
• These are used by importing “math” module
• Type “import math” to import math module.
• math.sqrt(4): square root of 4 square root function from math
module
• math.pi: pi value in math 3.14259265359…
• math.sin(math.pi/2): sin function
6
Math Module (cont.)
• Infinite number math.inf
7
Math Module (cont.)
• Complex numbers can be represented
• A complex number: c=a+bi with real part a, imaginary part b and
imaginary unit i=-1.
• In Python, the symbol j is used to denote the imaginary unit.
2+5i 2+5j
• Or we can use a constructor complex
complex (2, 5)
• Scientific notation using the letter e
• Python can handle scientific notation
• 1e6 = 1000000
• 1e-3 = 0.001
8
Math Module (cont.)
• Every time a function in math module is typed, it is always typed
math.function_name.
• Alternatively, there is a simpler way. For example, if we want to
use sin and log from math module, we can import them as
follows: “from math import sin, log”.
9
Data Types
• In Python, there are additional data types needed for numerical
values: int, float and complex.
10
Logical Expressions and Operators
• Logical Expression True or False