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

Lecture - 01 - Intro to Python

Uploaded by

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

Lecture - 01 - Intro to Python

Uploaded by

lenah.buk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

INTRODUCTION TO PYTHON

LECTURE - 01 Dr. Noor Felemban


© Dr. Irfan, Dr. Nida
ABOUT PYTHON
• Development started in the 1980’s by Guido van Rossum.
• Only became popular in the last decade or so.

•Latest version = 3.10


•Interpreted, very-high-level programming language.
• Supports a multitude of programming paradigms.
• OOP, functional, procedural, logic, structured, etc.

• 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

• commands(statements) instruct interpreter to do something


• can be typed directly in a shell or stored in a file that is read into the shell and
evaluated
OBJECTS
• Programs manipulate data objects
• Objects have a type that defines the kinds of things programs can do to them
• Ahmad is a human so she can walk, speak English, etc.
• Dog is an animal and can walk, etc.

• 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

<object> <operator> <object>


SIMPLE OPERATIONS
• Parentheses used to tell Python to do these operations first
• Operator precedence without parentheses
• **
• *,/

• + and – executed left to right, as appear in expression


BINDING VARIABLES AND VALUES
• equal sign is an assignment of a value to a variable name

• value stored in computer memory


• an assignment binds name to value
• retrieve value associated with name or variable by invoking
the name, by typing pi
CHANGING BINDINGS
• can re-bind variable names using new assignment statements
• previous value may still store in memory but lost the handle for it
• value for area does not change until you tell the computer to do the calculation
again
INTERPRETER
• The standard implementation of Python is interpreted.
• The interpreter translates Python code into bytecode, and this bytecode is executed by the Python VM
(similar to Java).

• Two modes: normal and interactive.


• Normal mode: entire .py files are provided to the interpreter.
• Interactive mode: read-eval-print loop (REPL) executes statements piecewise.
INTERPRETER: NORMAL MODE
Let’s write our first Python program!
In our favorite editor, let’s create helloworld.py with the following contents:

print ("Hello, World!“)

From the terminal:

$ 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!“)

Now, from the terminal: (Linux)

$ ./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!’

>>> for i in range(0,3):


... print ("Hello, World!" )
...
Hello, World!
Hello, World!
Hello, World!
SOME FUNDAMENTALS
• Whitespace is significant in Python. Where other languages may use {} or (), Python
uses indentation to denote code blocks.
1
# Python Coding Style
print (“Hello World!“)
• Comments
• Single-line comments denoted by #.
• Multi-line comments begin and end with three “s.
• Typically, multi-line comments are meant for documentation. 2
# here’s a comment
• Comments should express information that cannot be expressed for i in range(0,3):
in code – do not restate code. print i
def myfunc():
"""here’s a comment about
the myfunc function"""
print ("I'm in a function!“)
PYTHON TYPING
• Python is a strongly, dynamically typed language.
• Strong Typing
• Obviously, Python isn’t performing static type checking, but it does prevent mixing operations between
mismatched types.
• Explicit conversions are required in order to mix types.
• Example: 2 + “four” ß not going to fly

• Dynamic Typing
• All type checking is done at runtime.
• No need to declare a variable or give it a type before use.

Let’s start by looking at Python’s built-in data types.


NUMERIC TYPES
The subtypes are int, float and complex.
• Their respective constructors are int(), float(), and complex().

• 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 ).

A complex number is represented by “x + yi”. Python converts the real numbers


x and y into complex using the function complex(x,y). The real part can be accessed
using the function real() and imaginary part can be represented by imag().
>>> 3 + 2
5

NUMERIC TYPES >>> 18 % 5


3

>>> 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.

• s1, s2, and s3 are variables with string value.

The values in s1,s2,and s3 are all different.


Upper and lower-case matters. Blanks matter
STRINGS ARE INDEXED
Negative
Indexing
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Positive
Indexing

• The characters in a string can be referenced through their


indices. Called “subscripting”.
t = s[Small : Big]
t = s[-7 : -4]

EXAMPLE’S
EXAMPLE’S
STRINGS CAN BE COMBINED

We “added” in a blank.

This is called concatenation.


LECTURE – 01
READING ONLINE READING
ASSIGNMENT http://thomas-cokelaer.info/tutorials/python/variables.html

You might also like