Python3
Introduction to Python3 Programming Language
AGENDA
● Intro to Python
● Why learn Python
● Basic Python Syntax
● Coding Time!
● Python Q/A
COURSE MODULE CODE COMPILER
Python3: Zero to Hero - Introduction to Python Programming
Python
High-level programming language for
general-purpose programming, created by
Guido van Rossum and first released in 1991.
Python is great for backend web development,
data analysis, artificial intelligence, and scientific
computing. Many developers have also used
Python to build productivity tools, games, and
desktop apps, so there are plenty of resources to
help you learn.
Why learn Python?
Python3: Zero to Hero - Introduction to Python Programming
Python takes coding like natural
human-language.
Simple
Elegant
Syntax
You don't need to define the
type of a variable in Python.
Not
overly
Strict
Expressive of Language
Python allows you to write programs having greater
functionality with fewer lines of code.
Very Flexible
As a dynamically typed language, Python is really
flexible. This means there are no hard rules on how to
build features, and you'll have more flexibility solving
problems using different methods.
Furthermore, Python is also more forgiving of errors, so
you'll still be able to compile and run your program
until you hit the problematic part.
The
import
system
Modules importing
Modules
Modules can define functions, classes, and variables that you
can reference in other Python .py files or via the Python
command line interpreter.
Modules are accessed by using the import statement. When you
do this, you execute the code of the module, keeping the scopes
of the definitions so that your current file(s) can make use of
these.
Basic Syntax
Variable
A variable is a location in memory used to store some data
(value).
We don't need to declare a variable before using it.
In Python, we simply assign a value to a variable and it will
exist. We don't even have to declare the type of the variable. This
is handled internally according to the type of value we assign to
the variable.
Variable Assignment
We use the assignment operator (=) to assign values to a
variable. Any type of value can be assigned to any valid
variable.
Multiple Assignment
In Python, multiple assignments can be made in a single
statement as follows:
If we want to assign the same value to multiple variables at
once, we can do this as
Basic Operators
Basic Operators
Python language supports the following types of operators.
● Arithmetic Operators
● Comparison (Relational) Operators
● Assignment Operators
● Logical Operators
● Bitwise Operators
● Membership Operators
● Identity Operators
Basic Operators
● + Addition
- Subtraction
* Multiplication
/ Division
% Modulus
** Exponent
// Floor Division
Python Comparison Operators
● == Equal
!= Not Equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Python Assignment Operators
● = Equal
● += Add AND
-= Subtract AND
*= Multiply AND
/= Divide AND
%= Modulus AND
**= Exponent AND
//= Floor Division
Python Bitwise Operators
& Binary AND
| Binary OR
^ Binary XOR
~ Binary Ones Complement
<< Binary Left Shift
>> Binary Right Shift
Python Membership Operators
Python Identity Operators
Python Data Types
Data Types
Numeric Types
● int: Integers;
● long: Long integers of non-limited length;
● float: Floating-Point numbers, equivalent to C doubles
● complex: Complex Numbers
Sequences Types
● str: String;
● bytes: a sequence of integers in the range of 0-255; only available in Python 3.x
● byte array: like bytes, but mutable
● list
● tuple
Data Types (cont.)
Sets:
● set: an unordered collection of unique objects;
● frozen set: like set, but immutable
Mappings:
● dict: Python dictionaries, also called hashmaps or associative
arrays,
Mutable vs. Immutable Objects
Data types in Python can be distinguished based on whether objects of the type are
mutable or immutable. The content of objects of immutable types cannot be changed
after they are created.
Some immutable types: Some mutable types:
int, float, long, complex byte array
str list
bytes set
tuple dict
frozen set
List
List is the most versatile data type available in Python which
can be written as a list of comma-separated values (items)
between square brackets.
Important thing about a list is that items in a list need not be of
the same type.
List
Basic List Operations
Indexing, Slicing, and Matrixes
Built-in List
Functions
List
Methods
List
Methods
(cont.)
Tuple
Tuples are immutable which means you cannot update or
change the values of tuple elements.
Tuple Examples
Basic Tuple Operations
Indexing, Slicing, and Matrixes
Built-in
Tuple
Functions
Dictionary
Each key is separated from its value by a colon (:), the items are
separated by commas, and the whole thing is enclosed in curly
braces.
Keys are unique within a dictionary while values may not be.
The values of a dictionary can be of any type, but the keys must
be of an immutable data type such as strings, numbers, or tuples.
Dictionary Examples
Accessing Values in Dictionary
Updating Values in Dictionary
Deleting Values in Dictionary
Dictionary
Function and
Methods
Dictionary
Function
and
Methods
(cont.)
Built-in
Dictionary
Functions
Strings
Strings are amongst the most popular types in Python. We can
create them simply by enclosing characters in quotes. Python
treats single quotes the same as double quotes.
Creating strings is as simple as assigning a value to a variable.
Accessing String Values
Python does not support a character type; these are treated as
strings of length one, thus also considered a substring.
Updating Strings
You can "update" an existing string by (re)assigning a variable
to another string. The new value can be related to its previous
value or to a completely different string altogether.
Built-in String
Methods
Built-in
String
Methods
(cont.)
Built-in
String
Methods
(cont.)
Built-in
String
Methods
(cont.)
Built-in
String
Methods
(cont.)
Python
Decision
Making
Decision Making
Decision making is anticipation of conditions occurring
while execution of the program and specifying actions taken
according to the conditions.
Decision structures evaluate multiple expressions which
produce TRUE or FALSE as outcome. You need to determine
which action to take and which statements to execute if
outcome is TRUE or FALSE otherwise.
Decision Making
Decision Making
Python Loops
Loops
In general, statements are executed sequentially: The first
statement in a function is executed first, followed by the second,
and so on. There may be a situation when you need to execute a
block of code several number of times.
A loop statement allows us to execute a statement or group of
statements multiple times.
Loops
Loops Control
Range()
-returns an immutable sequence object of integers between the
given start integer to the stop integer.
Syntax:
range(stop)
range(start, stop[, step])
Range() Parameters
start - integer starting from which the sequence of
integers is to be returned
stop - integer before which the sequence of integers is
to be returned.
The range of integers end at stop - 1.
step (Optional) - integer value which determines the
increment between each integer in the sequence
Scope
Resolution
and the LEGB
Rule
Scope
“hierarchy level” in which we search namespaces for certain
“name-to-object” mappings.
LEGB Rule
● Local can be inside a function or class method, for
example.
● Enclosed can be its enclosing function, e.g., if a function
is wrapped inside another function.
● Global refers to the uppermost level of the executing
script itself, and
● Built-in are special names that Python reserves for itself.
Python Functions
Functions
A function is a block of organized, reusable code that is used to
perform a single, related action. Functions provide better
modularity for your application and a high degree of code
reusing.
Function blocks begin with the keyword Any input
def followed by the function name and parameters or
parentheses ( ( ) ) arguments should
be placed within
these parentheses.
You can also define
parameters inside
the documentation these parentheses.
string of the
function or
docstring.
statement return [expression] exits a
function, optionally passing back an
expression to the caller.
Docstrings
A docstring is a string literal that occurs as the first statement in a
module, function, class, or method definition. Such a docstring
becomes the doc special attribute of that object.
Comments
Comments are little snippets of text embedded inside your code
that are ignored by the Python interpreter.
A comment is denoted by the hash character (#) and extends to
the end of the line.:
Functions Examples
Coding Time
Ask me
anything about
Python :)