Lecture - 01 - Intro to Python
Lecture - 01 - Intro to Python
• General purpose.
• Very comprehensive standard library includes numeric modules, crypto
services, OS interfaces, networking modules, GUI support, development
tools, etc.
NOTABLE FEATURES
• Easy to learn.
•Cross-platform.
• Free and open source.
• Extensible.
• Embeddable.
• Large standard library and active community.
• Useful for a wide variety of applications.
ANACONDA: DATA SCIENCE TOOLKIT
Over 250 packages including: conda (enables installing
additional open-source packages), numpy and scipy.
https://www.anaconda.com/products/distribution
Installations
Jupyter : https://jupyter.org/install
https://anaconda.org/anaconda/jupyter
Spyder: https://docs.spyder-ide.org/current/installation.html
Visual Studio Code: https://code.visualstudio.com/download
PYTHON PROGRAMS
• A program is a sequence of definitions and commands
• definitions evaluated
• commands executed by Python interpreter in a shell
• Objects are
• scalar (cannot be subdivided)
• non-scalar (have internal structure that can be accessed)
SCALAR OBJECTS
• int – represent integers, ex. 5
• float – represent real numbers, ex. 3.27
• bool – represent Boolean values True and False
• NoneType – special and has one value, None
• can use type() to see the type of an object
TYPE CONVERSIONS (CAST)
• can convert object of one type to another
• float(3) converts integer 3 to float 3.0
• int(3.9) truncates float 3.9 to integer 3
PRINTING TO CONSOLE
• to show output from code to a user, use print command
EXPRESSIONS
• Combine objects and operators to form expressions
• An expression has a value, which has a type
• Syntax for a simple expression
$ python helloworld.py
Hello, World!
INTERPRETER: NORMAL MODE
Let’s include a shell-bang in the beginning of helloworld.py:
#!/usr/bin/env python
print ("Hello, World!“)
$ ./helloworld.py
Hello, World! As you studied in OS,
“How to run Batch file”
INTERPRETER: INTERACTIVE MODE $ python
>>> print ("Hello, World!" )
Hello, World!
Let’s accomplish the same task (and more) in
interactive mode. >>> hellostring = "Hello, World!"
>>> hellostring
'Hello, World!’
>>> 2*5
10
>>> 2*hellostring
'Hello, World!Hello, World!’
• Dynamic Typing
• All type checking is done at runtime.
• No need to declare a variable or give it a type before use.
• All numeric types, except complex, support the typical numeric operations you’d
expect to find (a list is available here:
https://docs.python.org/2/library/stdtypes.html#numeric-types-int-float-
long-complex ).
>>> abs(-7)
• Numeric 7
• int: equivalent to C’s long and unlimited in 3.x.
• float: equivalent to C’s doubles. >>> float(9)
9.0
• complex: complex numbers.
>>> int(5.3)
• Supported operations include constructors (i.e. int(3)), 5
arithmetic, negation, modulus, absolute value,
>>> complex(1,2)
exponentiation, etc. (1+2j)
>>> 2 ** 8
256
PYTHON DATA
TYPES
EXAMPLE'S
tr ue
o e s
/") d r s
o r (" e g e
ra t in t
o p e d in g t
s h
EXAMPLE'S x, sla pes in int=f c lu lo a
n 3 . ll t y in t /
tho o r a i.e .,
In Py vision f 2=2.0
di : 4 /
Ex
STRINGS
• Strings are quoted characters. Here are three Nothing special about letters… Any
character from keyboard can be a
• examples: part of string.
EXAMPLE’S
EXAMPLE’S
STRINGS CAN BE COMBINED
We “added” in a blank.