SUMMARY
1. Syntax
a. Indentations
o Indentations is important
o Indentations on the following lines allow Python to recognize that
they are part of the same block of code
o Ex:
o Skipping the indentation at the beginning of the line , which causes
an execution error. The program will understand that they are
separate statements.
o Ex:
b. Statements
1
o No semicolon “;” is needed at the end of each statement line.
o Each statements must be written on a new line or separated by
semicolon “;” . This will give the same effects when printed on the
screen.
o Ex:
o 2 separate statements on the same line => Error
Ex:
2. Output
o Print() :
+ Text:
2
Use to display text or output the values
Text in Python must be inside quotes. You can use either
double quotes (“ ”) or single quotes (‘ ’).
Ex:
Print multiple words on the same line, you can use the end
parameter:
Ex:
+ Numbers:
In the print() function, unlike text, numbers don’t need to
be enclosed in quotes.
Basic operations can be performed inside a function.
Text and numbers can be combined by separating them
with a comma “,”.
Ex:
3
+ Variables:
For text, to output multiple variables ,separate them by
comma “,” or with “+” operator.
When use “+” for text you need include space at the end.
Not adding any spaces , this doesn’t cause any program
error but when you print the text on the screen , it may be
hard to read.
For number , “+” is added mutiple variables value
together.
Ex:
4
To combine text and number:
Separate by comma ( Correct)
Ex:
5
Use the “+” operator (Wrong)
Ex:
3. Comments
o Comments can be used to explain code , which allows you to
understand it easily.
o A Comment start with “#” or triple quotes (“ ” ” ) .
o You can place these comment inside the quotes or use mutiple “#”
symbols. Automatically, python will ignore them.
4. Variables
o Variables are containers for storing data values.
o To create a variable, you can assign for it any type value and even
can change the type after you have been set it.
6
o Ex:
o Rule in naming a variables:
+ A variable name must start with a letter or an underscore
characters (not a number).
+ A valid variable name can contain letters and underscores.
+ A variable name cannot be the same as a Python function
name.
+ Variable names are case-sensitive. (MYVAR is different
from myvar , MyVar,..)
o Legal variable names:
o Ex:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
o Illegal variable names:
o Ex:
2myvar = "John"
my-var = "John"
my var = "John"
7
o There are several techniques to make variables more readable:
+ Camel case : myVariableName
+ Pascal case : MyVariableName
+ Snake case : my_variable_name
o Many values to mutiple variables:
+ Note: Make sure the number of variables matches the
number of values, or else you will get an error.
o 1 values to mutiple variables
+ Mutiple variables as the same value
o Unpack collection
o Ex:
o Global variables :
o Variables which create outside the function . This can both use
inside of function and also outside.
o Ex:
8
o Different from local variable and global variables is that local
variables use inside the functions but can use it for [Link]
global variables are opposite to the local ones.
o If you want create a global variables, you can use keyword global
inside your function . It changes from local variables to global
variables.
o Ex:
5. Data type
o Some types of data:
Text str
Numeric int , float, complex
9
Sequenc
list , tuple , range
e
Mapping dict
Set set,frozenset
Boolean bool
Binary Bytes, bytearray, memoryview
None None
o The data type have been set after adding a value for variables
o Function type() :
+ The function which you can get the data type of any object
+ Ex:
6. Numbers
o There are 3 numberic types:
+ int : This is a “whole number” ,” without decimals,”
“unlimited length”.
+ float : Number that contains 1 or more decimals.
+ complex : Numbers are written with a "j"
o In Python, you can convert numbers between types such as int,
float, and complex.
o Ex:
10
o Random number
+ Python use a module called random which helps you make
random numbers
+ Ex:
11
7. Casting
o This is the method using to convert from this data types to another
data types
o Some data types can converting :
+ int()
+ float()
+ str()
+ Ex:
8. For loops
a. Definition
12
+ For loops are statements used to perform and create a reptition
with in iterable object such as tuple, list , a dictionary, a string,…
This helps your code look cleaner and more readable.
b. The range function and statement
+ Range()
+ Range (start,end , step)
13
+ Using in making a repition from start values index to end
values index with a specified step. If you don’t add any
value in start and step paramaters , python will default start =
0 , step = 1.
+ Ex:
+ Statements
+ If…else : condition statements
14
Break():
In for loops , it uses for print on the screen to the limit of the
value you specify and exit the loop.
15
Continue:
In for loops , it uses for skipping the valuable index you
have been default in if statement and continue to the next.
+ Pass():
+ Using in skipping the for loops statements
+ Nested loops:
+ It have the inside loops and outer loops are a block of code. For
loops outside and for inner loops.
+ The "inner loop" will be executed one time for each iteration of
the "outer loop"
16
9. Function
a. Definition
o A function is a block of code , which runs when it is called .
It can call in mutiple times
o Must be return a value at the end the function or the program
will excute error ( except the void function)
o Without them , the code repeated in several times -> your
code looks ugly.
b. Creating a function
o Using def keyword + function name
Ex:
def my_function()
c. Function names
o Rules:
+ Must start with letter or underscore
+ Only contains letters, number , underscores
+ Case-sensitive (myFunction is different from
myfunction)
d. Arguments
o Definition:
+ When you call a function after writing it finished, you
send them a value which is suitable for the
parameter you default in a function.
+ Note: You should call exactly the number of
arguments
17
o Different between arguments and parameters
+ Parameters
They are variables inside function with separate
by a comma “,”
+ Arguments
After writng a function, you can add a detail
value for a parameter .
Ex:
o Types of arguments
+ Default parameter values
Assigning a permanent value for parameter.
After you call a function without adding a value
for argument , it uses the permanent value .
Ex:
18
+ Keyword arguments:
Key = value
Ex:
+ Positional arguments:
Without using keyword
Note: Must be in correct order
Ex:
19
+ Mixing positional and keyword arguments:
+ Positonal-Only Arguments :
Add “,/” after the argument
+ Keyword-Only Arguments :
Add “*,” before the argument
+ Combining Positional-Only and Keyword-Only:
Arguments before / are positional-only, and
arguments after * are keyword-only
e. *argrs/ **kwargs:
o Definition
+ *args:
Making a function to accept any number of
arguments.
Ex:
20
+ **kwargs
Making a function to accept any number of
keyword arguments.
Ex:
f. Scope
o Definition:
+ A variable is only available from inside the region it is
created. This is called scope.
o Types of scope
+ Local Scope : Such as local variables , only using in
the function
Ex:
21
+ Global Scope : Such as global variables, use inside
and outside the function
Ex:
o LEGB rules
+ Local - Inside the current function
+ Enclosing - Inside enclosing functions (from inner to
outer)
+ Global - At the top level of the module
+ Built-in - In Python's built-in namespace
g. Decorators
o Decorators let you add extra behavior to a function, without
changing the function's code.
o Step:
Define the decorator first, then apply it
with @decorator_name above the function.
Placing @changecase directly above the function
definition
22
Changecase is the decorator
Ex:
o Preserving Function Metadata:
+ using the “__name__” and “__doc__” attributes.
+ Ex:
+ Using decorator:
+ Ex:
23
+ Import [Link]
+ Ex:
h. Lambda:
o Abridged version of function
o Accept any number of argument but just 1 expression
o Don’t need to return
o Ex: Different between a function and lambda
24
o Map()
o Map(function, iterable, *iterables)
o Feature of lambda: It is a statement
o Ex:
o Filter()
o Filter(condition , iterable)\
o Feature of lambda : It is a condition that filter use to define
which variable is true.
o Ex:
25
o Sorted()
o Sorted (iterable, key,reverse )
o Feature of lambda : Using it like a key statement which
helps sort know the index variable in the list use to compare
to another ones .
o Ex:
i. Recursion
o A function that call itself again. It is a kind of looping
function
o Must have a condition which is a stop point and return it a
value or the python will give us a error.
o Ex:
26
j. Generators
o A generator is a special type of function that can pause and
resume using the yield keyword.
o Calling a generator returns a generator object, which creates
values only when needed.
o
o yield returns a value without ending the function, and it
remembers where it left off.
[Link]
o range() creates a sequence of numbers.
o You can use it with one, two, or three arguments:
o range(stop) → numbers from 0 to stop-1
o range(start, stop) → numbers from start to stop-1
o range(start, stop, step) → numbers with a custom step value
o Usually used in loops, especially for.
[Link]
o Python doesn’t have built-in arrays like some languages.
o But the array module provides list-like arrays that store one single
data type.
o Lists are normally preferred unless strict typing is needed.
[Link]
o An iterator is an object that can be looped over.
o Uses two methods:
o __iter__()
27
o __next__()
o Many Python objects (lists, tuples, dicts) are iterable by default.
[Link]
o A module is a file containing Python code (functions, classes,
variables).
[Link]
Work with dates and times.
o Import: import datetime
o Common uses:
o [Link]() → current date/time
o [Link](year, month, day) → create date
o .strftime() → format date
[Link]
o Work with dates and times.
o Import: import datetime
o Common uses:
o [Link]() → current date/time
o [Link](year, month, day) → create date
o .strftime() → format date
[Link]
o Regex (Regular Expressions) are patterns used to:
o search
o match
o split
o replace
o Python uses the re module.
o Common functions:
o findall()
o search()
o split()
28
o sub()
29