Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Chapter 1-Introduction To Python Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Chapter 1-Introduction To Python Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Chapter 1

Introduction to Python Programming

By

Hyung Jae (Chris) Chang


Troy University
Ways to rung Python Code
• Installation of Python and related packages
• Anaconda: a complete distribution framework that includes the Python
interpreter, package manager and the commonly used packages in
scientific computing.
• Miniconda: a “light” version of Anaconda that does not include the
commonly used packages. You need to install all the different packages
yourself, but it does include the Python interpreter and package
manager.

• Ways to run Python code


• Python shell or Ipython shell (which stands for Interactive Python)
• Run a Python script/file from the command line (.py)
• Use browser-based Python environment which is called Jupyter
Notebook
• Use IDEs of your choice, such as Spyder or PyCharm.

2
Python as Calculator
• Arithmetic operation calculated and returning answer

• Open up Python console and try the followings


• 1+2
• 5-4
• 29*23
• 15/3
• 2**10
• How about (3x4)/(22+4/2)?

3
Python as Calculator (cont.)

• Let’s try more!


• (552 + 2/3)/(132)
• The computations are following the mathematical precedence.
• Parenthesis can be used to change the order of operation.

• “_” is used to break the complex computations.


• Please try the followings in the Command Prompt
• 55**2
• _+2/3
• _/13**2

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

• What do you see? We will discuss about it later.


5
Math Module (cont.)
• ? in Python
• Shortcut for help
• Try “math.factorial?”
• ZeroDivisionError
• Python raises such an error when the expression 1/0 (which is infinity)
appears.

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

• We can construct truth tables for each logical operator.


11

You might also like