Module03-Introduction To Python
Module03-Introduction To Python
binnur.kurt@rc.bau.edu.tr
MODULE 3
INTRODUCTION TO PYTHON
Python
> Python is an object-oriented scripting language that was released publicly in
1991.
> It was developed by Guido van Rossum of the National Research Institute for
Mathematics and Computer Science in Amsterdam.
> Name is inspired by Monty Python a comedy show on BBC 1970s
> Python has rapidly become one of the world’s most popular programming
languages.
> It’s now particularly popular for educational and scientific computing, and it
recently surpassed the programming language R as the most popular data-
science programming language.
Python
> Here are some reasons why Python is popular
– It’s open source, free and widely available with a massive open-source
community
– It’s easier to learn than languages like C, C++, C# and Java, enabling novices
and professional developers to get up to speed quickly
– It’s easier to read than many other popular programming languages
– It’s widely used in education
– It enhances developer productivity with extensive standard libraries and
thousands of third-party open-source libraries, so programmers can write
code faster and perform complex tasks with minimal code
– There are massive numbers of free open-source Python applications
– It’s popular in web development: Django, Flask, …
Python
> Here are some reasons why Python is popular
– It supports popular programming paradigms
• Procedural
• Functional
• Object-oriented
• Reflective
– It simplifies concurrent programming
• asyncio and async/await
– There are lots of capabilities for enhancing Python performance
– It’s used to build anything from simple scripts to complex apps with massive
numbers of users, such as Dropbox, YouTube, Reddit, Instagram and Quora
Python
> Here are some reasons why Python is popular
– It’s popular in artificial intelligence, which is enjoying explosive growth, in
part because of its special relationship with data science
– It’s widely used in the financial community
– There’s an extensive job market for Python programmers across many
disciplines, especially in data-science-oriented positions
Philosophy
> PEP 20, The Zen of Python
– Beautiful is better than ugly.
– Explicit is better than implicit.
– Simple is better than complex.
– Complex is better than complicated.
– Flat is better than nested.
– Sparse is better than dense.
– Readability counts.
– Special cases aren't special enough to break the rules.
– Although practicality beats purity.
– Errors should never pass silently.
– Unless explicitly silenced.
Philosophy
> PEP 20, The Zen of Python
– In the face of ambiguity, refuse the temptation to guess.
– There should be one-- and preferably only one --obvious way to do it.
– Although that way may not be obvious at first unless you're Dutch.
– Now is better than never.
– Although never is often better than *right* now.
– If the implementation is hard to explain, it's a bad idea.
– If the implementation is easy to explain, it may be a good idea.
– Namespaces are one honking great idea -- let's do more of those!
import this
What Is Anaconda?
> Anaconda is a Python and R distribution software.
> It aims to provide everything you need for Python “out of the box.”
> Its primary use is for data analytics and data science; however, it’s a superb
tool for learning as well.
> Anconda includes
– The core Python language and libraries
– Jupyter Notebook
– Anaconda’s own package manager
What Is Jupyter Notebook?
> It is an open-source integrated development environment (IDE)
> It allows you to create and share documents that contain live code, equations,
visualizations, and narrative text.
> It also allows you to write snippets of code without needing to know a lot
about Python.
IPython Basics
> You can launch the IPython shell on the command line just like launching the
regular Python interpreter:
ipython
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC
v.1916 32 bit (Intel)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for
help.
In [1]:
ipython
Running Jupyter
Running Jupyter
Running Jupyter
Running Jupyter
Comments
# this is a comment
" " "
This is a multi-Line comment
" " "
print("Hello") # this is also a comment
What Are Data Types?
Type Description
None The Python “null” value (only one instance of the None object exists)
str String type; holds Unicoded (UTF-8 encoded) strings
bytes Raw ASCII bytes (or Unicode encoded as bytes)
float Double-precision (64-bit) floating-point number
bool A true or False value
int Arbitrary precision signed integer
Type System
Type System
Numeric types
> The primary Python types for numbers are int and float.
> An int can store arbitrarily large numbers:
Numeric types
> Floating-point numbers are represented with the Python float type.
– A double-precision (64-bit) value.
> They can also be expressed with scientific notation:
> Integer division not resulting in a whole number will always yield a floating-
point number:
Numeric types
> To get C-style integer division, use the floor division operator //:
> It drops the fractional part if the result is not a whole number
Arithmetic Operators
Python Operation Arithmetic Operator Algebraic Expression Python Expression
Addition + f+7 f+7
Subtraction - p-c p–c
Multiplication * b*m b*m
Exponentiation ** xy x ** y
True division / x/y or or x ÷ y x/y
Floor division // ⁄ 𝑜𝑟 or x ÷ y x // y
Remainder (modulo) % r mod s r%s
Exceptions
> Dividing by zero with / or // is not allowed and results in an exception
Operator Precedence Rules
> Rules of operator precedence
1. Expressions in parentheses evaluate first, so parentheses may force the order
of evaluation to occur in any sequence you desire.
– Parentheses have the highest level of precedence.
– In expressions with nested parentheses, such as (a / (b - c)), the expression
in the innermost parentheses (that is, b - c) evaluates first.
2. Exponentiation operations evaluate next. If an expression contains several
exponentiation operations, Python applies them from right to left.
3. Multiplication, division and modulus operations evaluate next.
– If an expression contains several multiplication, true-division, floor-division
and modulus operations, Python applies them from left to right.
– Multiplication, division and modulus are “on the same level of
precedence.”
None
> None is the Python null value type.
> If a function does not explicitly return a value, it implicitly returns None:
None
> None is also a common default value for function arguments:
> While a technical point, it’s worth bearing in mind that None is not only a
reserved keyword but also a unique instance of NoneType:
Complex Numbers
Strings and Quotes
Trigonometric functions
> math.acos(x)
– Returns the arc cosine of x, in radians.
> math.asin(x)
– Returns the arc sine of x, in radians.
> math.atan(x)
– Returns the arc tangent of x, in radians.
> math.atan2(y, x)
– Returns atan(y / x), in radians.
– The result is between -pi and pi.
> math.cos(x)
– Returns the cosine of x radians.
Trigonometric functions
> math.sin(x)
– Returns the sine of x radians.
> math.tan(x)
– Returns the tangent of x radians.
> math.dist(p, q)
– Returns the Euclidean distance between two points p and q, each given as a
sequence (or iterable) of coordinates.
– The two points must have the same dimension.
Angular conversion
> math.degrees(x)
– Converts angle x from radians to degrees.
> math.radians(x)
– Converts angle x from degrees to radians
Hyperbolic functions
> Hyperbolic functions are analogs of trigonometric functions that are based on
hyperbolas instead of circles.
> math.acosh(x)
– Returns the inverse hyperbolic cosine of x.
> math.asinh(x)
– Returns the inverse hyperbolic sine of x.
> math.atanh(x)
– Returns the inverse hyperbolic tangent of x.
> math.cosh(x)
– Returns the hyperbolic cosine of x.
> math.sinh(x)
– Returns the hyperbolic sine of x.
Hyperbolic functions
> math.tanh(x)
– Returns the hyperbolic tangent of x.
Special functions
> math.erf(x)
– Returns the error function at x.
– The erf() function can be used to compute traditional statistical functions
such as the cumulative standard normal distribution
> math.erfc(x)
– Returns the complementary error function at x.
– The complementary error function is defined as 1.0 - erf(x).
– It is used for large values of x where a subtraction from one would cause a
loss of significance.
> math.gamma(x)
– Returns the Gamma function at x.
Special functions
> math.lgamma(x)
– Returns the natural logarithm of the absolute value of the Gamma function
at x.
Constants
> math.pi
– The mathematical constant π = 3.141592…, to available precision.
> math.e
– The mathematical constant e = 2.718281…, to available precision.
> math.tau
– The mathematical constant τ = 6.283185…, to available precision.
– Tau is a circle constant equal to 2π, the ratio of a circle’s circumference to its
radius.
> math.inf
– A floating-point positive infinity.
– Equivalent to the output of float('inf').
Constants
> math.nan
– A floating-point “not a number” (NaN) value.
– Equivalent to the output of float('nan').
Examples
Examples
Arbitrary Precision Arithmetic