Python Tutorial
Learn Python
Python is a popular programming language. Python can be used on a server to create web applications.Learning by Examples
With our "Try it Yourself" editor, you can edit Python code and view the result. Example print("Hello, World!")Python File Handling
In our File Handling section you will learn how to open, read, write, and delete files.Python Database Handling
In our database section you will learn how to access and work with MySQL and MongoDB databases: Exercise: Insert the missing part of the code below to output "Hello World". ("Hello World")Python Examples
Learn by examples! This tutorial supplements all explanations with clarifying examples.My Learning
Track your progress with the free "My Learning" program here at W3Schools. Log in to your account, and start earning points! This is an optional feature. You can study W3Schools without using My Learning.Python Reference
You will also find complete function and method references:Download Python
Download Python from the official Python web site: https://python.orgKickstart your career
Get certified by completing the coursePython Introduction
What is Python?
Python is a popular programming language. It was created by Guido van Rossum, and released in 1991. It is used for: web development (server-side), software development, mathematics, system scripting. Example print("Hello, World!")Python Getting Started
Python Install
Many PCs and Macs will have python already installed. To check if you have python installed on a Windows PC, search in the start bar for Python or run the following on the Command Line (cmd.exe): C:\Users\Your Name>python --version To check if you have python installed on a Linux or Mac, then on linux open the command line or on Mac open the Terminal and type: python --version If you find that you do not have Python installed on your computer, then you can download it for free from the following website: https://www.python.org/Python Quickstart
Python is an interpreted programming language, this means that as a developer you write Python (.py) files in a text editor and then put those files into the python interpreter to be executed. The way to run a python file is like this on the command line: C:\Users\Your Name>python helloworld.py Where "helloworld.py" is the name of your python file. Let's write our first Python file, called helloworld.py, which can be done in any text editor. helloworld.py print("Hello, World!") Simple as that. Save your file. Open your command line, navigate to the directory where you saved your file, and run: C:\Users\Your Name>python helloworld.py The output should read: Hello, World! Congratulations, you have written and executed your first Python program.The Python Command Line
To test a short amount of code in python sometimes it is quickest and easiest not to write the code in a file. This is made possible because Python can be run as a command line itself. Type the following on the Windows, Mac or Linux command line: C:\Users\Your Name>python Or, if the "python" command did not work, you can try "py": C:\Users\Your Name>py From there you can write any python, including our hello world example from earlier in the tutorial: C:\Users\Your Name>python Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print("Hello, World!") Which will write "Hello, World!" in the command line: C:\Users\Your Name>python Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print("Hello, World!") Hello, World! Whenever you are done in the python command line, you can simply type the following to quit the python command line interface: exit()Python Syntax
Execute Python Syntax
As we learned in the previous page, Python syntax can be executed by writing directly in the Command Line: >>> print("Hello, World!") Hello, World! Creating a python file on the server, using the .py file extension, and running it in the Command Line: C:\Users\Your Name>python myfile.pyPython Indentation
Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code. Example if 5 > 2: print("Five is greater than two!") Python will give you an error if you skip the indentation: Example Syntax Error: if 5 > 2: print("Five is greater than two!") The number of spaces is up to you as a programmer, the most common use is four, but it has to be at least one. Example if 5 > 2: print("Five is greater than two!") if 5 > 2: print("Five is greater than two!") You have to use the same number of spaces in the same block of code, otherwise Python will give you an error: Example Syntax Error: if 5 > 2: print("Five is greater than two!") print("Five is greater than two!")Python Variables
In Python, variables are created when you assign a value to it: Example Variables in Python: x = 5 y = "Hello, World!" Python has no command for declaring a variable. You will learn more about variables in the Python Variables chapter.Comments
Python has commenting capability for the purpose of in-code documentation. Comments start with a #, and Python will render the rest of the line as a comment: Example Comments in Python: #This is a comment. print("Hello, World!") Exercise: Insert the missing part of the code below to output "Hello World". ("Hello World")Python Comments
Comments can be used to explain Python code. Comments can be used to make the code more readable. Comments can be used to prevent execution when testing code.Creating a Comment
Comments starts with a#
, and Python will ignore them: Example #This is a comment print("Hello, World!") Comments can be placed at the end of a line, and Python will ignore the rest of the line: Example print("Hello, World!") #This is a comment A comment does not have to be text that explains the code, it can also be used to prevent Python from executing code: Example #print("Hello, World!") print("Cheers, Mate!")Multi Line Comments
Python does not really have a syntax for multi line comments. To add a multiline comment you could insert a#
for each line: Example #This is a comment #written in #more than just one line print("Hello, World!") Or, not quite as intended, you can use a multiline string. Since Python will ignore string literals that are not assigned to a variable, you can add a multiline string (triple quotes) in your code, and place your comment inside it: Example " This is a comment written in more than just one line " print("Hello, World!") As long as the string is not assigned to a variable, Python will read the code, but then ignore it, and you have made a multiline comment. Exercise: Comments in Python are written with a special character, which one? This is a commentPython Variables
Variables
Variables are containers for storing data values.Creating Variables
Python has no command for declaring a variable. A variable is created the moment you first assign a value to it. Example x = 5 y = "John" print(x) print(y) Variables do not need to be declared with any particular type, and can even change type after they have been set. Example x = 4 # x is of type int x = "Sally" # x is now of type str print(x)Casting
If you want to specify the data type of a variable, this can be done with casting. Example x = str(3) # x will be '3' y = int(3) # y will be 3 z = float(3) # z will be 3.0Get the Type
You can get the data type of a variable with thetype()
function. Example x = 5 y = "John" print(type(x)) print(type(y)) You will learn more about and later in this tutorial.Single or Double Quotes?
String variables can be declared either by using single or double quotes: Example x = "John" # is the same as x = 'John'Case-Sensitive
Variable names are case-sensitive. Example This will create two variables: a = 4 A = "Sally" #A will not overwrite aPython Variables
Variables
Variables are containers for storing data values.Creating Variables
Python has no command for declaring a variable. A variable is created the moment you first assign a value to it. Example x = 5 y = "John" print(x) print(y) Variables do not need to be declared with any particular type, and can even change type after they have been set. Example x = 4 # x is of type int x = "Sally" # x is now of type str print(x)Casting
If you want to specify the data type of a variable, this can be done with casting. Example x = str(3) # x will be '3' y = int(3) # y will be 3 z = float(3) # z will be 3.0Get the Type
You can get the data type of a variable with thetype()
function. Example x = 5 y = "John" print(type(x)) print(type(y)) You will learn more about and later in this tutorial.Single or Double Quotes?
String variables can be declared either by using single or double quotes: Example x = "John" # is the same as x = 'John'Case-Sensitive
Variable names are case-sensitive. Example This will create two variables: a = 4 A = "Sally" #A will not overwrite aPython - Variable Names
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables: A variable name must start with a letter or the underscore character A variable name cannot start with a number A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables) Example Legal variable names: myvar = "John" my_var = "John" _my_var = "John" myVar = "John" MYVAR = "John" myvar2 = "John" Example Illegal variable names: 2myvar = "John" my-var = "John" my var = "John" Remember that variable names are case-sensitiveMulti Words Variable Names
Variable names with more than one word can be difficult to read. There are several techniques you can use to make them more readable:Camel Case
Each word, except the first, starts with a capital letter: myVariableName = "John"Pascal Case
Each word starts with a capital letter: MyVariableName = "John"Snake Case
Each word is separated by an underscore character: my_variable_name = "John"Python Variables - Assign Multiple Values
Many Values to Multiple Variables
Python allows you to assign values to multiple variables in one line: Example x, y, z = "Orange", "Banana", "Cherry" print(x) print(y) print(z) Note: Make sure the number of variables matches the number of values, or else you will get an error.One Value to Multiple Variables
And you can assign the same value to multiple variables in one line: Example x = y = z = "Orange" print(x) print(y) print(z)Unpack a Collection
If you have a collection of values in a list, tuple etc. Python allows you to extract the values into variables. This is called unpacking. Example Unpack a list: fruits = ["apple", "banana", "cherry"] x, y, z = fruits print(x) print(y) print(z) Learn more about unpacking in our Chapter.Python - Output Variables
Output Variables
The Pythonprint()
function is often used to output variables. Example x = "Python is awesome" print(x) In theprint()
function, you output multiple variables, separated by a comma: Example x = "Python" y = "is" z = "awesome" print(x, y, z) You can also use the+
operator to output multiple variables: Example x = "Python " y = "is " z = "awesome" print(x + y + z) Notice the space character after"Python "
and"is "
, without them the result would be "Pythonisawesome". For numbers, the+
character works as a mathematical operator: Example x = 5 y = 10 print(x + y) In theprint()
function, when you try to combine a string and a number with the+
operator, Python will give you an error: Example x = 5 y = "John" print(x + y) The best way to output multiple variables in theprint()
function is to separate them with commas, which even support different data types: Example x = 5 y = "John" print(x, y)Python - Global Variables
Global Variables
Variables that are created outside of a function (as in all of the examples above) are known as global variables. Global variables can be used by everyone, both inside of functions and outside. Example Create a variable outside of a function, and use it inside the function x = "awesome" def myfunc(): print("Python is " + x) myfunc() If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value. Example Create a variable inside a function, with the same name as the global variable x = "awesome" def myfunc(): x = "fantastic" print("Python is " + x) myfunc() print("Python is " + x)The global Keyword
Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use theglobal
keyword. Example If you use theglobal
keyword, the variable belongs to the global scope: def myfunc(): global x x = "fantastic" myfunc() print("Python is " + x) Also, use theglobal
keyword if you want to change a global variable inside a function. Example To change the value of a global variable inside a function, refer to the variable by using theglobal
keyword: x = "awesome" def myfunc(): global x x = "fantastic" myfunc() print("Python is " + x)Python - Variable Exercises
Now you have learned a lot about variables, and how to use them in Python. Are you ready for a test? Try to insert the missing part to make the code work as expected: Exercise: Create a variable namedcarname
and assign the valueVolvo
to it. = "" Go to the Exercise section and test all of our Python Variable Exercises: Python Variable ExercisesPython Data Types
Built-in Data Types
In programming, data type is an important concept. Variables can store data of different types, and different types can do different things. Python has the following data types built-in by default, in these categories:
Text Type: | str |
Numeric Types: | int , float , complex |
Sequence Types: | list , tuple , range |
Mapping Type: | dict |
Set Types: | set , frozenset |
Boolean Type: | bool |
Binary Types: | bytes , bytearray , memoryview |
None Type: | NoneType |
type()
function:
Example
Print the data type of the variable x:
x = 5
print(type(x))
Example | Data Type | Try it |
---|---|---|
x = "Hello World" | str | |
x = 20 | int | |
x = 20.5 | float | |
x = 1j | complex | |
x = ["apple", "banana", "cherry"] | list | |
x = ("apple", "banana", "cherry") | tuple | |
x = range(6) | range | |
x = {"name" : "John", "age" : 36} | dict | |
x = {"apple", "banana", "cherry"} | set | |
x = frozenset({"apple", "banana", "cherry"}) | frozenset | |
x = True | bool | |
x = b"Hello" | bytes | |
x = bytearray(5) | bytearray | |
x = memoryview(bytes(5)) | memoryview | |
x = None | NoneType |
Example | Data Type | Try it |
---|---|---|
x = str("Hello World") | str | |
x = int(20) | int | |
x = float(20.5) | float | |
x = complex(1j) | complex | |
x = list(("apple", "banana", "cherry")) | list | |
x = tuple(("apple", "banana", "cherry")) | tuple | |
x = range(6) | range | |
x = dict(name="John", age=36) | dict | |
x = set(("apple", "banana", "cherry")) | set | |
x = frozenset(("apple", "banana", "cherry")) | frozenset | |
x = bool(5) | bool | |
x = bytes(5) | bytes | |
x = bytearray(5) | bytearray | |
x = memoryview(bytes(5)) | memoryview |
int
float
complex
Variables of numeric types are created when you assign a value to them:
Example
x = 1
# int
y = 2.8 # float
z = 1j # complex
To verify the type of any object in Python, use the type()
function:
Example
print(type(x))
print(type(y))
print(type(z))
int()
,
float()
, and complex()
methods:
Example
Convert from one type to another:
x = 1 # int
y = 2.8 # float
z = 1j # complex
#convert from int to float:
a = float(x)
#convert from float to int:
b = int(y)
#convert from int to complex:
c = complex(x)
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
Note: You cannot convert complex numbers into another number type.
random()
function to
make a random number, but Python has a built-in module called
random
that can be used to make random numbers:
Example
Import the random module, and display a random number between 1 and 9:
import random
print(random.randrange(1, 10))
In our Random Module Reference you will learn more about the Random module.
Exercise:
Insert the correct syntax to convert x into a floating point number.
x = 5
x = (x)
print()
function:
Example
print("Hello")
print('Hello')
for
loop.
Example
Loop through the letters in the word "banana":
for x in "banana":
print(x)
Learn more about For Loops in our chapter.
len()
function.
Example
The len()
function returns the length of a string:
a = "Hello, World!"
print(len(a))
in
.
Example
Check if "free" is present in the following text:
txt = "The best things in life are free!"
print("free" in txt)
Use it in an if
statement:
Example
Print only if "free" is present:
txt = "The best things in life are free!"
if "free" in txt:
print("Yes, 'free' is present.")
Learn more about If statements in our Python
If...Else chapter.
not in
.
Example
Check if "expensive" is NOT present in the following text:
txt = "The best things in life are free!"
print("expensive" not in txt)
Use it in an if
statement:
Example
print only if "expensive" is NOT present:
txt = "The best things in life are free!"
if "expensive" not in txt:
print("No, 'expensive' is NOT present.")
print()
function:
Example
print("Hello")
print('Hello')
for
loop.
Example
Loop through the letters in the word "banana":
for x in "banana":
print(x)
Learn more about For Loops in our chapter.
len()
function.
Example
The len()
function returns the length of a string:
a = "Hello, World!"
print(len(a))
in
.
Example
Check if "free" is present in the following text:
txt = "The best things in life are free!"
print("free" in txt)
Use it in an if
statement:
Example
Print only if "free" is present:
txt = "The best things in life are free!"
if "free" in txt:
print("Yes, 'free' is present.")
Learn more about If statements in our Python
If...Else chapter.
not in
.
Example
Check if "expensive" is NOT present in the following text:
txt = "The best things in life are free!"
print("expensive" not in txt)
Use it in an if
statement:
Example
print only if "expensive" is NOT present:
txt = "The best things in life are free!"
if "expensive" not in txt:
print("No, 'expensive' is NOT present.")
upper()
method returns the string in upper case:
a = "Hello, World!"
print(a.upper())
lower()
method returns the string in lower case:
a = "Hello, World!"
print(a.lower())
strip()
method removes any whitespace from the beginning or the end:
a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"
replace()
method replaces a string with another string:
a = "Hello, World!"
print(a.replace("H", "J"))
split()
method returns a list where the text between the specified separator becomes the list items.
Example
The split()
method splits the string into substrings if it finds instances of the separator:
a = "Hello, World!"
print(a.split(",")) #
returns ['Hello', ' World!']
Learn more about Lists in our chapter.
a
with variable
b
into variable c
:
a = "Hello"
b = "World"
c = a + b
print(c)
Example
To add a space between them, add a " "
:
a = "Hello"
b = "World"
c = a + " " + b
print(c)
format()
method!
The format()
method takes the passed arguments,
formats them, and places them in the string where the placeholders
{}
are:
Example
Use the format()
method to insert numbers
into strings:
age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))
The format() method takes unlimited number of arguments, and are placed into
the respective placeholders:
Example
quantity = 3
itemno = 567
price = 49.95
myorder = "I want {}
pieces of item {} for {} dollars."
print(myorder.format(quantity,
itemno, price))
You can use index numbers {0}
to be sure the arguments are placed
in the correct placeholders:
Example
quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2}
dollars for {0} pieces of item {1}."
print(myorder.format(quantity,
itemno, price))
Learn more about String Formatting in our chapter.
\
followed by the character you want to insert.
An example of an illegal character is a double quote inside a string that is surrounded by double quotes:
Example
You will get an error if you use double quotes inside a string that is
surrounded by double quotes:
txt = "We are the so-called "Vikings" from the north."
To fix this problem, use the escape character \"
:
Example
The escape character allows you to use double quotes when you normally would not be allowed:
txt = "We are the so-called \"Vikings\" from the north."
Code | Result | Try it |
---|---|---|
\' | Single Quote | |
\\ | Backslash | |
\n | New Line | |
\r | Carriage Return | |
\t | Tab | |
\b | Backspace | Try it » |
\f | Form Feed | |
\ooo | Octal value | |
\xhh | Hex value |
Method | Description |
---|---|
capitalize() | Converts the first character to upper case |
casefold() | Converts string into lower case |
center() | Returns a centered string |
count() | Returns the number of times a specified value occurs in a string |
encode() | Returns an encoded version of the string |
endswith() | Returns true if the string ends with the specified value |
expandtabs() | Sets the tab size of the string |
find() | Searches the string for a specified value and returns the position of where it was found |
format() | Formats specified values in a string |
format_map() | Formats specified values in a string |
index() | Searches the string for a specified value and returns the position of where it was found |
isalnum() | Returns True if all characters in the string are alphanumeric |
isalpha() | Returns True if all characters in the string are in the alphabet |
isdecimal() | Returns True if all characters in the string are decimals |
isdigit() | Returns True if all characters in the string are digits |
isidentifier() | Returns True if the string is an identifier |
islower() | Returns True if all characters in the string are lower case |
isnumeric() | Returns True if all characters in the string are numeric |
isprintable() | Returns True if all characters in the string are printable |
isspace() | Returns True if all characters in the string are whitespaces |
istitle() | Returns True if the string follows the rules of a title |
isupper() | Returns True if all characters in the string are upper case |
join() | Joins the elements of an iterable to the end of the string |
ljust() | Returns a left justified version of the string |
lower() | Converts a string into lower case |
lstrip() | Returns a left trim version of the string |
maketrans() | Returns a translation table to be used in translations |
partition() | Returns a tuple where the string is parted into three parts |
replace() | Returns a string where a specified value is replaced with a specified value |
rfind() | Searches the string for a specified value and returns the last position of where it was found |
rindex() | Searches the string for a specified value and returns the last position of where it was found |
rjust() | Returns a right justified version of the string |
rpartition() | Returns a tuple where the string is parted into three parts |
rsplit() | Splits the string at the specified separator, and returns a list |
rstrip() | Returns a right trim version of the string |
split() | Splits the string at the specified separator, and returns a list |
splitlines() | Splits the string at line breaks and returns a list |
startswith() | Returns true if the string starts with the specified value |
strip() | Returns a trimmed version of the string |
swapcase() | Swaps cases, lower case becomes upper case and vice versa |
title() | Converts the first character of each word to upper case |
translate() | Returns a translated string |
upper() | Converts a string into upper case |
zfill() | Fills the string with a specified number of 0 values at the beginning |
len
method to print the length of the string.
x = "Hello World"
print()
Go to the Exercise section and test all of our Python Strings Exercises:
Python String Exercises
True
or False
.
True
or False
.
You can evaluate any expression in Python, and get one of two
answers,
True
or False
.
When you compare two values, the expression is evaluated and Python returns
the Boolean answer:
Example
print(10 > 9)
print(10 == 9)
print(10 < 9)
When you run a condition in an if statement, Python returns
True
or False
:
Example
Print a message based on whether the condition is True
or
False
:
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
bool()
function allows you to evaluate
any value, and give you
True
or False
in return,
Example
Evaluate a string and a number:
print(bool("Hello"))
print(bool(15))
Example
Evaluate two variables:
x = "Hello"
y = 15
print(bool(x))
print(bool(y))
True
if it
has some sort of content.
Any string is True
, except empty strings.
Any number is True
, except
0
.
Any list, tuple, set, and dictionary are True
, except
empty ones.
Example
The following will return True:
bool("abc")
bool(123)
bool(["apple", "cherry", "banana"])
False
, except empty values, such as ()
,
[]
, {}
,
"
, the number
0
, and the value None
.
And of course the value False
evaluates to
False
.
Example
The following will return False:
bool(False)
bool(None)
bool(0)
bool(")
bool(())
bool([])
bool({})
One more value, or object in this case, evaluates to
False
, and that is if you have an object that
is made from a class with a __len__
function that returns
0
or
False
:
Example
class myclass():
def __len__(self):
return 0
myobj = myclass()
print(bool(myobj))
isinstance()
function, which can be used to determine if an object is of a certain data type:
Example
Check if an object is an integer or not:
x = 200
print(isinstance(x, int))
Exercise:
The statement below would print a Boolean value, which one?
print(10 > 9)
+
operator to add together two values:
Example
print(10 + 5)
Python divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Operator | Name | Example | Try it |
---|---|---|---|
+ | Addition | x + y | |
- | Subtraction | x - y | |
* | Multiplication | x * y | |
/ | Division | x / y | |
% | Modulus | x % y | |
** | Exponentiation | x ** y | |
// | Floor division | x // y |
Operator | Example | Same As | Try it |
---|---|---|---|
= | x = 5 | x = 5 | |
+= | x += 3 | x = x + 3 | |
-= | x -= 3 | x = x - 3 | |
*= | x *= 3 | x = x * 3 | |
/= | x /= 3 | x = x / 3 | |
%= | x %= 3 | x = x % 3 | |
//= | x //= 3 | x = x // 3 | |
**= | x **= 3 | x = x ** 3 | |
&= | x &= 3 | x = x & 3 | |
|= | x |= 3 | x = x | 3 | |
^= | x ^= 3 | x = x ^ 3 | |
>>= | x >>= 3 | x = x >> 3 | |
<<= | x <<= 3 | x = x << 3 |
Operator | Name | Example | Try it |
---|---|---|---|
== | Equal | x == y | |
!= | Not equal | x != y | |
> | Greater than | x > y | |
< | Less than | x < y | |
>= | Greater than or equal to | x >= y | |
<= | Less than or equal to | x <= y |
Operator | Description | Example | Try it |
---|---|---|---|
and | Returns True if both statements are true | x < 5 and x < 10 | |
or | Returns True if one of the statements is true | x < 5 or x < 4 | |
not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) |
Operator | Description | Example | Try it |
---|---|---|---|
is | Returns True if both variables are the same object | x is y | |
is not | Returns True if both variables are not the same object | x is not y |
Operator | Description | Example | Try it |
---|---|---|---|
in | Returns True if a sequence with the specified value is present in the object | x in y | |
not in | Returns True if a sequence with the specified value is not present in the object | x not in y |
Operator | Name | Description |
---|---|---|
& | AND | Sets each bit to 1 if both bits are 1 |
| | OR | Sets each bit to 1 if one of two bits is 1 |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
~ | NOT | Inverts all the bits |
<< | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off |
>> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
10
with 5
, and print the result.
print(10 5)
[0]
,
the second item has index [1]
etc.
len()
function:
Example
Print the number of items in the list:
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
list()
constructor to make a List:
thislist = list(("apple", "banana", "cherry")) # note the double round-brackets
print(thislist)
[0]
,
the second item has index [1]
etc.
len()
function:
Example
Print the number of items in the list:
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
list()
constructor to make a List:
thislist = list(("apple", "banana", "cherry")) # note the double round-brackets
print(thislist)
-1
refers to the last item,
-2
refers to the second last item etc.
Example
Print the last item of the list:
thislist = ["apple", "banana", "cherry"]
print(thislist[-1])
in
keyword:
Example
Check if "apple" is present in the list:
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
insert()
method.
The insert()
method inserts an item at the specified index:
Example
Insert "watermelon" as the third item:
thislist = ["apple", "banana", "cherry"]
thislist.insert(2, "watermelon")
print(thislist)
Note: As a result of the example above, the list will now contain 4 items.
append()
method to append an item:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
insert()
method.
The insert()
method inserts an item at the specified index:
Example
Insert an item as the second position:
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
Note: As a result of the examples above, the lists will now contain 4 items.
extend()
method.
Example
Add the elements of tropical
to thislist
:
thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)
The elements will be added to the end of the list.
extend()
method does not have to append
lists, you can add any iterable object (tuples, sets, dictionaries
etc.).
Example
Add elements of a tuple to a list:
thislist = ["apple", "banana", "cherry"]
thistuple = ("kiwi", "orange")
thislist.extend(thistuple)
print(thislist)
remove()
method removes the specified item.
Example
Remove "banana":
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
pop()
method removes the specified
index.
Example
Remove the second item:
thislist = ["apple", "banana", "cherry"]
thislist.pop(1)
print(thislist)
If you do not specify the index, the pop()
method removes the last item.
Example
Remove the last item:
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
The del
keyword also removes the specified
index:
Example
Remove the first item:
thislist = ["apple", "banana", "cherry"]
del
thislist[0]
print(thislist)
The del
keyword can also delete the list completely.
Example
Delete the entire list:
thislist = ["apple", "banana", "cherry"]
del
thislist
clear()
method empties the list.
The list still remains, but it has no content.
Example
Clear the list content:
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)
for
loop:
Example
Print all items in the list, one by one:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
Learn more about for
loops in our Chapter.
range()
and
len()
functions to create a suitable iterable.
Example
Print all items by referring to their index number:
thislist = ["apple", "banana", "cherry"]
for i
in range(len(thislist)):
print(thislist[i])
The iterable created in the example above is [0, 1, 2]
.
while
loop.
Use the len()
function to determine the length of the list,
then start at 0 and loop your way through the list items by referring to their indexes.
Remember to increase the index by 1 after each iteration.
Example
Print all items, using a while
loop to go
through all the index numbers
thislist = ["apple", "banana", "cherry"]
i = 0
while i < len(thislist):
print(thislist[i])
i = i + 1
Learn more about while
loops in our
Chapter.
for
loop that will print all items in a list:
thislist = ["apple", "banana", "cherry"]
[print(x) for x in thislist]
Learn more about list comprehension in the next chapter:
.
for
statement
with a conditional test inside:
Example
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist)
With list comprehension you can do all that with only one line of code:
Example
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x
for x in fruits if "a" in x]
print(newlist)
True
.
Example
Only accept items that are not "apple":
newlist = [x for x in fruits if x != "apple"]
The condition
if x != "apple"
will return True
for all elements other
than "apple", making the new list contain all fruits except "apple".
The condition is optional and can be omitted:
Example
With no if
statement:
newlist = [x for x in fruits]
range()
function to create an iterable:
newlist = [x for x in range(10)]
Same example, but with a condition:
Example
Accept only numbers lower than 5:
newlist = [x for x in range(10) if x < 5]
sort()
method that will sort the list alphanumerically, ascending, by default:
Example
Sort the list alphabetically:
thislist = ["orange", "mango", "kiwi",
"pineapple", "banana"]
thislist.sort()
print(thislist)
Example
Sort the list numerically:
thislist = [100, 50, 65, 82, 23]
thislist.sort()
print(thislist)
reverse = True
:
Example
Sort the list descending:
thislist = ["orange", "mango", "kiwi",
"pineapple", "banana"]
thislist.sort(reverse = True)
print(thislist)
Example
Sort the list descending:
thislist = [100, 50, 65, 82, 23]
thislist.sort(reverse = True)
print(thislist)
key =
function
.
The function will return a number that will be used to sort the list (the
lowest number first):
Example
Sort the list based on how close the number is to 50:
def myfunc(n):
return abs(n - 50)
thislist = [100, 50, 65, 82, 23]
thislist.sort(key =
myfunc)
print(thislist)
sort()
method is case sensitive,
resulting in all capital letters being sorted before lower case letters:
Example
Case sensitive sorting can give an unexpected result:
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.sort()
print(thislist)
Luckily we can use built-in functions as key functions when sorting a list.
So if you want a case-insensitive sort function, use str.lower as a key function:
Example
Perform a case-insensitive sort of the list:
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.sort(key
= str.lower)
print(thislist)
reverse()
method reverses the current sorting order of the elements.
Example
Reverse the order of the list items:
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.reverse()
print(thislist)
list2 =
list1
, because: list2
will only be a
reference to list1
, and changes made in
list1
will automatically also be made in
list2
.
There are ways to make a copy, one way is to use the built-in List
method
copy()
.
Example
Make a copy of a list with the copy()
method:
thislist = ["apple", "banana", "cherry"]
mylist
= thislist.copy()
print(mylist)
Another way to make a copy is to use the built-in method list()
.
Example
Make a copy of a list with the list()
method:
thislist = ["apple", "banana", "cherry"]
mylist
= list(thislist)
print(mylist)
+
operator.
Example
Join two list:
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
Another way to join two lists is by appending all the items from list2 into
list1, one by one:
Example
Append list2 into list1:
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
for x in list2:
list1.append(x)
print(list1)
Or you can use the extend()
method,
which purpose is to add elements from one list to another
list:
Example
Use the extend()
method to add list2 at the end of list1:
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)
Method | Description |
---|---|
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
fruits
list.
fruits = ["apple", [0]
, the second item has index [1]
etc.
len()
function:
Example
Print the number of items in the tuple:
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))
[0]
, the second item has index [1]
etc.
len()
function:
Example
Print the number of items in the tuple:
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))
-1
refers to the last item,
-2
refers to the second last item etc.
Example
Print the last item of the tuple:
thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])
in
keyword:
Example
Check if "apple" is present in the tuple:
thistuple = ("apple", "banana", "cherry")
if "apple" in thistuple:
print("Yes, 'apple' is in the fruits
tuple")
append()
method, but
there are other ways to add items to a tuple.
1. Convert into a list: Just like the workaround for changing a tuple, you can convert it into a list, add your item(s), and convert it back into a tuple.
Example
Convert the tuple into a list, add "orange", and convert it back into a tuple:
thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.append("orange")
thistuple =
tuple(y)
2. Add tuple to a tuple. You are allowed to add tuples to
tuples, so if you want to add one item, (or many), create a new tuple with the
item(s), and add it to the existing tuple:
Example
Create a new tuple with the value "orange", and add that tuple:
thistuple = ("apple", "banana", "cherry")
y = ("orange",)
thistuple += y
print(thistuple)
Note: When creating a tuple with only one item, remember to include a comma
after the item, otherwise it will not be identified as a tuple.
del
keyword can delete the tuple
completely:
thistuple = ("apple", "banana", "cherry")
del
thistuple
print(thistuple)
#this will raise an error because the tuple no longer exists
*
*
to the variable name and the
values will be assigned to the variable as a list:
Example
Assign the rest of the values as a list called "red":
fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")
(green, yellow, *red) = fruits
print(green)
print(yellow)
print(red)
If the asterisk is added to another variable name than the last,
Python will assign values to the variable until the number of values left matches the number of variables left.
Example
Add a list of values the "tropic" variable:
fruits = ("apple", "mango", "papaya", "pineapple", "cherry")
(green, *tropic, red) = fruits
print(green)
print(tropic)
print(red)
for
loop.
Example
Iterate through the items and print the values:
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)
Learn more about for
loops in our Chapter.
range()
and len()
functions to create a suitable iterable.
Example
Print all items by referring to their index number:
thistuple = ("apple", "banana", "cherry")
for i in range(len(thistuple)):
print(thistuple[i])
while
loop.
Use the len()
function to determine the length of the tuple,
then start at 0 and loop your way through the tuple items by refering to their indexes.
Remember to increase the index by 1 after each iteration.
Example
Print all items, using a while
loop to go through all the index numbers:
thistuple = ("apple", "banana", "cherry")
i = 0
while i < len(thistuple):
print(thistuple[i])
i = i + 1
Learn more about while
loops in our
Chapter.
+
operator:
Example
Join two tuples:
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
*
operator:
Example
Multiply the fruits tuple by 2:
fruits = ("apple", "banana", "cherry")
mytuple = fruits * 2
print(mytuple)
Method | Description |
---|---|
count() | Returns the number of times a specified value occurs in a tuple |
index() | Searches the tuple for a specified value and returns the position of where it was found |
fruits
tuple.
fruits = ("apple", len()
function.
Example
Get the number of items in a set:
thisset = {"apple", "banana", "cherry"}
print(len(thisset))
len()
function.
Example
Get the number of items in a set:
thisset = {"apple", "banana", "cherry"}
print(len(thisset))
for
loop, or ask if a specified value is present in a set, by using the
in
keyword.
Example
Loop through the set, and print the values:
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
Example
Check if "banana" is present in the set:
thisset = {"apple", "banana", "cherry"}
print("banana"
in thisset)
add()
method.
Example
Add an item to a set, using the add()
method:
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
update()
method.
Example
Add elements from tropical
into
thisset
:
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)
update()
method does not have
to be a set, it can be any iterable object (tuples, lists, dictionaries etc.).
Example
Add elements of a list to at set:
thisset = {"apple", "banana", "cherry"}
mylist = ["kiwi", "orange"]
thisset.update(mylist)
print(thisset)
remove()
, or the discard()
method.
Example
Remove "banana" by using the remove()
method:
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)
Note: If the item to remove does not exist, remove()
will raise an error.
Example
Remove "banana" by using the discard()
method:
thisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
print(thisset)
Note: If the item to remove does not exist, discard()
will
NOT raise an error.
You can also use the pop()
method to remove
an item, but this method will remove the last item. Remember that sets
are unordered, so you will not know what item that gets removed.
The return value of the pop()
method is the
removed item.
Example
Remove the last item by using the pop()
method:
thisset = {"apple", "banana", "cherry"}
x =
thisset.pop()
print(x)
print(thisset)
Note: Sets are unordered, so when using the pop()
method,
you do not know which item that gets removed.
Example
The clear()
method empties the set:
thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)
Example
The del
keyword will delete the set
completely:
thisset = {"apple", "banana", "cherry"}
del
thisset
print(thisset)
for
loop:
Example
Loop through the set, and print the values:
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
union()
method that returns a new set containing all items from both sets,
or the update()
method that inserts all the items from one set into another:
Example
The union()
method returns a new set with all items from both sets:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
Example
The update()
method inserts the items in set2 into set1:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set1.update(set2)
print(set1)
Note: Both union()
and update()
will exclude any duplicate items.
intersection_update()
method will keep only the items that are present in both sets.
Example
Keep the items that exist in both set x
, and set y
:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.intersection_update(y)
print(x)
The intersection()
method will return a new set, that only contains the items that are present in both sets.
Example
Return a set that contains the items that exist in both set x
, and set y
:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.intersection(y)
print(z)
symmetric_difference_update()
method will
keep only the elements that are NOT present in both sets.
Example
Keep the items that are not present in both sets:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.symmetric_difference_update(y)
print(x)
The symmetric_difference()
method will return a new set,
that contains only the elements that are NOT present in both sets.
Example
Return a set that contains all items from both sets, except items that are
present in both:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.symmetric_difference(y)
print(z)
Method | Description |
---|---|
add() | Adds an element to the set |
clear() | Removes all the elements from the set |
copy() | Returns a copy of the set |
difference() | Returns a set containing the difference between two or more sets |
difference_update() | Removes the items in this set that are also included in another, specified set |
discard() | Remove the specified item |
intersection() | Returns a set, that is the intersection of two other sets |
intersection_update() | Removes the items in this set that are not present in other, specified set(s) |
isdisjoint() | Returns whether two sets have a intersection or not |
issubset() | Returns whether another set contains this set or not |
issuperset() | Returns whether this set contains another set or not |
pop() | Removes an element from the set |
remove() | Removes the specified element |
symmetric_difference() | Returns a set with the symmetric differences of two sets |
symmetric_difference_update() | inserts the symmetric differences from this set and another |
union() | Return a set containing the union of sets |
update() | Update the set with the union of this set and others |
fruits
set.
fruits = {"apple",
len()
function:
Example
Print the number of items in the dictionary:
print(len(thisdict))
len()
function:
Example
Print the number of items in the dictionary:
print(len(thisdict))
get()
that will give you the same result:
Example
Get the value of the "model" key:
x = thisdict.get("model")
keys()
method will return a list of all the keys in the dictionary.
Example
Get a list of the keys:
x = thisdict.keys()
The list of the keys is a view of the dictionary, meaning that any
changes done to the dictionary will be reflected in the keys list.
Example
Add a new item to the original dictionary, and see that the keys list gets
updated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.keys()
print(x) #before the change
car["color"] =
"white"
print(x) #after the change
values()
method will return a list of all the values in the dictionary.
Example
Get a list of the values:
x = thisdict.values()
The list of the values is a view of the dictionary, meaning that any
changes done to the dictionary will be reflected in the values list.
Example
Make a change in the original dictionary, and see that the values list gets
updated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values()
print(x) #before the change
car["year"]
= 2020
print(x) #after the change
Example
Add a new item to the original dictionary, and see that the values list gets
updated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values()
print(x) #before the change
car["color"]
= "red"
print(x) #after the change
items()
method will return each item in a dictionary, as tuples in a list.
Example
Get a list of the key:value pairs
x = thisdict.items()
The returned list is a view of the items of the dictionary, meaning that any
changes done to the dictionary will be reflected in the items list.
Example
Make a change in the original dictionary, and see that the items list gets
updated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
print(x) #before the change
car["year"]
= 2020
print(x) #after the change
Example
Add a new item to the original dictionary, and see that the items list gets
updated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
print(x) #before the change
car["color"]
= "red"
print(x) #after the change
in
keyword:
Example
Check if "model" is present in the dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is
one of the keys in the thisdict dictionary")
update()
method will update the dictionary with the items from the given
argument.
The argument must be a dictionary, or an iterable object with key:value pairs.
Example
Update the "year" of the car by using the update()
method:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.update({"year": 2020})
update()
method will update the dictionary with the items from
a given
argument. If the item does not exist, the item will be added.
The argument must be a dictionary, or an iterable object with key:value pairs.
Example
Add a color item to the dictionary by using the update()
method:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.update({"color":
"red"})
pop()
method removes the item with the specified key name:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)
Example
The popitem()
method removes the last
inserted item (in versions before 3.7, a random item is removed instead):
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.popitem()
print(thisdict)
Example
The del
keyword removes the item with the specified
key name:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict)
Example
The del
keyword can also delete the
dictionary completely:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict
print(thisdict) #this will cause an error because "thisdict"
no longer exists.
Example
The clear()
method empties the
dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.clear()
print(thisdict)
for
loop.
When looping through a dictionary, the return value are the keys of
the dictionary, but there are methods to return the values as well.
Example
Print all key names in the dictionary, one by one:
for x in thisdict:
print(x)
Example
Print all values in the dictionary, one by one:
for x in thisdict:
print(thisdict[x])
Example
You can also use the values()
method to
return values of a dictionary:
for x in thisdict.values():
print(x)
Example
You can use the keys()
method to
return the keys of a dictionary:
for x in thisdict.keys():
print(x)
Example
Loop through both keys and values, by using the
items()
method:
for x, y in thisdict.items():
print(x, y)
dict2 =
dict1
, because: dict2
will only be a
reference to dict1
, and changes made in
dict1
will automatically also be made in
dict2
.
There are ways to make a copy, one way is to use the built-in Dictionary
method
copy()
.
Example
Make a copy of a dictionary with the copy()
method:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict
= thisdict.copy()
print(mydict)
Another way to make a copy is to use the built-in function
dict()
.
Example
Make a copy of a dictionary with the dict()
function:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict
= dict(thisdict)
print(mydict)
Method | Description |
---|---|
clear() | Removes all the elements from the dictionary |
copy() | Returns a copy of the dictionary |
fromkeys() | Returns a dictionary with the specified keys and value |
get() | Returns the value of the specified key |
items() | Returns a list containing a tuple for each key value pair |
keys() | Returns a list containing the dictionary's keys |
pop() | Removes the element with the specified key |
popitem() | Removes the last inserted key-value pair |
setdefault() | Returns the value of the specified key. If the key does not exist: insert the key, with the specified value |
update() | Updates the dictionary with the specified key-value pairs |
values() | Returns a list of all the values in the dictionary |
get
method to print the value of the "model" key of the car
dictionary.
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print()
Go to the Exercise section and test all of our Python Dictionary Exercises:
Python Dictionary Exercises
else
without the
elif
:
Example
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
a
is greater than
b
, AND if c
is greater than a
:
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
or
keyword is a logical operator, and
is used to combine conditional statements:
Example
Test if a
is greater than
b
, OR if a
is greater than c
:
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
if
statements inside
if
statements, this is called nested
if
statements.
Example
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and
also above 20!")
else:
print("but not
above 20.")
if
statements cannot be empty, but if you
for some reason have an if
statement with no content, put in the pass
statement to avoid getting an error.
Example
a = 33
b = 200
if b > a:
pass
Exercise:
Print "Hello World" if a
is greater than b
.
a = 50
b = 10
a b
print("Hello World")
i
as long as i
is less than 6.
i = 1
i < 6
print(i)
i += 1
x
is "banana":
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x ==
"banana":
break
Example
Exit the loop when x
is "banana",
but this time the break comes before the print:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x ==
"banana":
break
print(x)
else
keyword in a
for
loop specifies a block of code to be
executed when the loop is finished:
Example
Print all numbers from 0 to 5, and print a message when the loop has ended:
for x in range(6):
print(x)
else:
print("Finally finished!")
Note: The else
block will NOT be executed if the loop is stopped by a break
statement.
Example
Break the loop when x
is 3, and see what happens with the
else
block:
for x in range(6):
if x == 3: break
print(x)
else:
print("Finally finished!")
for
loops cannot be empty, but if you for
some reason have a for
loop with no content, put in the pass
statement to avoid getting an error.
Example
for x in [0, 1, 2]:
pass
Exercise:
Loop through the items in the fruits
list.
fruits = ["apple", *
before the parameter name in the function definition.
This way the function will receive a tuple of arguments, and can access the items accordingly:
Example
If the number of arguments is unknown, add a *
before the parameter name:
def my_function(*kids):
print("The youngest child
is " + kids[2])
my_function("Emil", "Tobias", "Linus")
Arbitrary Arguments are often shortened to *args in Python documentations.
**
before the parameter name in the function definition.
This way the function will receive a dictionary of arguments, and can access the items accordingly:
Example
If the number of keyword arguments is unknown, add a double
**
before the parameter name:
def my_function(**kid):
print("His last name is " + kid["lname"])
my_function(fname = "Tobias", lname = "Refsnes")
Arbitrary Kword Arguments are often shortened to **kwargs in Python documentations.
return
statement:
Example
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
function
definitions cannot be empty, but if
you for some reason have a function
definition with no content, put in the pass
statement to avoid getting an error.
Example
def myfunction():
pass
my_function
.
:
print("Hello from a function")
a
, and
return the result:
x = lambda a : a + 10
print(x(5))
Lambda functions can take any number of arguments:
Example
Multiply argument a
with argument
b
and return the
result:
x = lambda a, b : a * b
print(x(5, 6))
Example
Summarize argument a
,
b
, and c
and
return the
result:
x = lambda a, b, c : a + b + c
print(x(5, 6,
2))
a
) and returns it.
x =
len()
method to return the length of
an array (the number of elements in an array).
Example
Return the number of elements in the cars
array:
x = len(cars)
Note: The length of an array is always one more than the highest array index.
for in
loop to loop through all the elements of an array.
Example
Print each item in the cars
array:
for x in cars:
print(x)
append()
method to add an element to an array.
Example
Add one more element to the cars
array:
cars.append("Honda")
pop()
method to remove an element from the array.
Example
Delete the second element of the cars
array:
cars.pop(1)
You can also use the remove()
method to remove an element from the array.
Example
Delete the element that has the value "Volvo":
cars.remove("Volvo")
Note: The list's remove()
method
only removes the first occurrence of the specified value.
Method | Description |
---|---|
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the first item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
class
:
Example
Create a class named MyClass, with a property named x:
class MyClass:
x = 5
__init__()
function is called automatically every time the class is being used to create a new object.
self
parameter
is a reference to the current instance of the class, and is used to access variables that belong to the class.
self
parameter is a reference to the
current instance of the class, and is used to access variables that belongs to the class.
It does not have to be named self
, you can
call it whatever you like, but it has to be the first parameter of any function
in the class:
Example
Use the words mysillyobject and abc instead of self:
class Person:
def __init__(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("John",
36)
p1.myfunc()
del
keyword:
Example
Delete the age property from the p1 object:
del p1.age
del
keyword:
Example
Delete the p1 object:
del p1
class
definitions cannot be empty, but if
you for some reason have a class
definition with no content, put in the pass
statement to avoid getting an error.
Example
class Person:
pass
Exercise:
Create a class named MyClass:
MyClass:
x = 5
Person
, with
firstname
and lastname
properties,
and a printname
method:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname,
self.lastname)
#Use the Person class to create an object, and then
execute the printname method:
x = Person("John", "Doe")
x.printname()
Student
, which will inherit the properties
and methods from
the Person
class:
class Student(Person):
pass
Note: Use the pass
keyword when you do not want to add any other properties or methods to the
class.
Now the Student class has the same properties and methods as the Person
class.
Example
Use the Student
class to create an object,
and then execute the printname
method:
x = Student("Mike", "Olsen")
x.printname()
__init__()
function to the child class (instead of the pass
keyword).
Note: The __init__()
function is called automatically every time the class is being used to create a new object.
Example
Add the __init__()
function to the
Student
class:
class Student(Person):
def __init__(self, fname, lname):
#add properties etc.
When you add the __init__()
function, the child class will no longer inherit
the parent's __init__()
function.
Note: The child's __init__()
function overrides the inheritance of the parent's
__init__()
function.
To keep the inheritance of the parent's __init__()
function, add a call to the
parent's __init__()
function:
Example
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
Now we have successfully added the __init__() function, and kept the
inheritance of the parent class, and we are ready to add functionality in the
__init__()
function.
super()
function that
will make the child class inherit all the methods and properties from its
parent:
Example
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
By using the super()
function, you do not
have to use the name of the parent element, it will automatically inherit the
methods and properties from its parent.
graduationyear
to the
Student
class:
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
self.graduationyear
= 2019
In the example below, the year 2019
should be a variable, and passed into the
Student
class when creating student objects.
To do so, add another parameter in the __init__() function:
Example
Add a year
parameter, and pass the correct
year when creating objects:
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear
= year
x = Student("Mike", "Olsen", 2019)
welcome
to the
Student
class:
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear
= year
def welcome(self):
print("Welcome",
self.firstname, self.lastname, "to the class of", self.graduationyear)
If you add a method in the child class with the same name as a function in
the parent class, the inheritance of the parent method will be overridden.
Exercise:
What is the correct syntax to create a class named Student that will inherit properties and methods from a class named Person?
class :
__iter__()
and __next__()
.
iter()
method which is used to get an iterator:
Example
Return an iterator from a tuple, and print each value:
mytuple = ("apple", "banana", "cherry")
myit = iter(mytuple)
print(next(myit))
print(next(myit))
print(next(myit))
Even strings are iterable objects, and can return an iterator:
Example
Strings are also iterable objects, containing a sequence of characters:
mystr = "banana"
myit = iter(mystr)
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
for
loop to iterate through an iterable object:
Example
Iterate the values of a tuple:
mytuple = ("apple", "banana", "cherry")
for x in mytuple:
print(x)
Example
Iterate the characters of a string:
mystr = "banana"
for x in mystr:
print(x)
The for
loop actually creates an iterator object and executes the next()
method for each loop.
__iter__()
and
__next__()
to your object.
As you have learned in the Python
Classes/Objects chapter, all classes have a function called
__init__()
, which allows you to do some
initializing when the object is being created.
The __iter__()
method acts similar, you can
do operations (initializing etc.), but must always return the iterator object
itself.
The __next__()
method also allows you to do
operations, and must return the next item in the sequence.
Example
Create an iterator that returns numbers, starting with 1, and each sequence
will increase by one (returning 1,2,3,4,5 etc.):
class MyNumbers:
def __iter__(self):
self.a =
1
return self
def __next__(self):
x = self.a
self.a += 1
return x
myclass = MyNumbers()
myiter = iter(myclass)
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
for
loop.
To prevent the iteration to go on forever, we can use the
StopIteration
statement.
In the __next__()
method, we can add a terminating condition to raise an error if the iteration is done a specified number of times:
Example
Stop after 20 iterations:
class MyNumbers:
def __iter__(self):
self.a =
1
return self
def __next__(self):
if self.a <= 20:
x = self.a
self.a += 1
return x
else:
raise StopIteration
myclass =
MyNumbers()
myiter = iter(myclass)
for x in myiter:
print(x)
x
is not available outside the function,
but it is available for any function inside the function:
Example
The local variable can be accessed from a function within the function:
def myfunc():
x = 300
def myinnerfunc():
print(x)
myinnerfunc()
myfunc()
x
, and
then the code will print the global x
:
x = 300
def myfunc():
x = 200
print(x)
myfunc()
print(x)
global
keyword.
The global
keyword makes the variable global.
Example
If you use the global
keyword, the variable belongs to the global scope:
def myfunc():
global x
x = 300
myfunc()
print(x)
Also, use the global
keyword if you want to
make a change to a global variable inside a function.
Example
To change the value of a global variable inside a function, refer to the
variable by using the global
keyword:
x = 300
def myfunc():
global x
x = 200
myfunc()
print(x)
.py
:
Example
Save this code in a file named mymodule.py
def greeting(name):
print("Hello, " + name)
import
statement:
Example
Import the module named mymodule, and call the greeting function:
import mymodule
mymodule.greeting("Jonathan")
Run Example »
Note: When using a function from a module, use the syntax: module_name.function_name.
mymodule.py
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Example
Import the module named mymodule, and access the person1 dictionary:
import mymodule
a = mymodule.person1["age"]
print(a)
Run Example »
.py
as
keyword:
Example
Create an alias for mymodule
called mx
:
import mymodule as mx
a = mx.person1["age"]
print(a)
Run Example »
platform
module:
import platform
x = platform.system()
print(x)
dir()
function:
Example
List all the defined names belonging to the platform module:
import platform
x = dir(platform)
print(x)
Note: The dir() function can be used on all
modules, also the ones you create yourself.
from
keyword.
Example
The module named mymodule
has one function
and one dictionary:
def greeting(name):
print("Hello, " + name)
person1
= {
"name": "John",
"age": 36,
"country":
"Norway"
}
Example
Import only the person1 dictionary from the module:
from mymodule import person1
print (person1["age"])
Run Example »
Note: When importing using the from
keyword, do not use the module name when referring to elements in the module.
Example: person1["age"]
, not
mymodule.person1["age"]
Exercise:
What is the correct syntax to import a module named "mymodule"?
mymodule
datetime
to work with dates as date
objects.
Example
Import the datetime module and display the current date:
import datetime
x = datetime.datetime.now()
print(x)
The date contains year, month, day, hour, minute, second, and microsecond.
The datetime
module has many methods to return information about the date
object.
Here are a few examples, you will learn more about them later in this
chapter:
Example
Return the year and name of weekday:
import datetime
x = datetime.datetime.now()
print(x.year)
print(x.strftime("%A"))
datetime()
class (constructor) of the
datetime
module.
The datetime()
class requires three parameters to create a date: year,
month, day.
Example
Create a date object:
import datetime
x = datetime.datetime(2020, 5, 17)
print(x)
The datetime()
class also takes parameters for time and timezone (hour,
minute, second, microsecond, tzone), but they are optional, and has a default
value of 0
, (None
for timezone).
datetime
object has a method for formatting date objects into readable strings.
The method is called strftime()
, and takes one parameter,
format
, to specify the format of the returned string:
Example
Display the name of the month:
import datetime
x = datetime.datetime(2018, 6, 1)
print(x.strftime("%B"))
A reference of all the legal format codes:
Directive | Description | Example | Try it |
---|---|---|---|
%a | Weekday, short version | Wed | |
%A | Weekday, full version | Wednesday | |
%w | Weekday as a number 0-6, 0 is Sunday | 3 | |
%d | Day of month 01-31 | 31 | |
%b | Month name, short version | Dec | |
%B | Month name, full version | December | |
%m | Month as a number 01-12 | 12 | |
%y | Year, short version, without century | 18 | |
%Y | Year, full version | 2018 | |
%H | Hour 00-23 | 17 | |
%I | Hour 00-12 | 05 | |
%p | AM/PM | PM | |
%M | Minute 00-59 | 41 | |
%S | Second 00-59 | 08 | |
%f | Microsecond 000000-999999 | 548513 | |
%z | UTC offset | +0100 | |
%Z | Timezone | CST | |
%j | Day number of year 001-366 | 365 | |
%U | Week number of year, Sunday as the first day of week, 00-53 | 52 | |
%W | Week number of year, Monday as the first day of week, 00-53 | 52 | |
%c | Local version of date and time | Mon Dec 31 17:41:00 2018 | |
%C | Century | 20 | |
%x | Local version of date | 12/31/18 | |
%X | Local version of time | 17:41:00 | |
%% | A % character | % | |
%G | ISO 8601 year | 2018 | |
%u | ISO 8601 weekday (1-7) | 1 | |
%V | ISO 8601 weeknumber (01-53) | 01 |
min()
and max()
functions can be used to find the lowest or highest value in an iterable:
Example
x = min(5, 10, 25)
y = max(5, 10, 25)
print(x)
print(y)
The abs()
function returns the absolute (positive) value of the specified number:
Example
x = abs(-7.25)
print(x)
The pow(x, y)
function returns the value of x to the power of y (xy).
Example
Return the value of 4 to the power of 3 (same as 4 * 4 * 4):
x = pow(4, 3)
print(x)
math
, which extends the list of mathematical functions.
To use it, you must import the math
module:
import math
When you have imported the math
module, you
can start using methods and constants of the module.
The math.sqrt()
method for example, returns the square root of a number:
Example
import
math
x = math.sqrt(64)
print(x)
The math.ceil()
method rounds a number upwards to
its nearest integer, and the math.floor()
method rounds a number downwards to its nearest integer, and returns the result:
Example
import
math
x = math.ceil(1.4)
y = math.floor(1.4)
print(x) #
returns 2
print(y) # returns 1
The math.pi
constant, returns the value of
PI (3.14...):
Example
import
math
x = math.pi
print(x)
json
, which can be used to work with JSON data.
Example
Import the json module:
import json
json.loads()
method.
The result will be a .
Example
Convert from JSON to Python:
import json
# some JSON:
x = '{ "name":"John", "age":30, "city":"New
York"}'
# parse x:
y = json.loads(x)
# the result is a
Python dictionary:
print(y["age"])
json.dumps()
method.
Example
Convert from Python to JSON:
import json
# a Python object (dict):
x = {
"name":
"John",
"age": 30,
"city": "New York"
}
#
convert into JSON:
y = json.dumps(x)
# the result is a JSON string:
print(y)
You can convert Python objects of the following types, into JSON strings:
dict
list
tuple
string
int
float
True
False
None
Example
Convert Python objects into JSON strings, and print the values:
import json
print(json.dumps({"name": "John", "age": 30}))
print(json.dumps(["apple",
"bananas"]))
print(json.dumps(("apple", "bananas")))
print(json.dumps("hello"))
print(json.dumps(42))
print(json.dumps(31.76))
print(json.dumps(True))
print(json.dumps(False))
print(json.dumps(None))
When you convert from Python to JSON, Python objects are converted into the JSON (JavaScript) equivalent:
Python | JSON |
---|---|
dict | Object |
list | Array |
tuple | Array |
str | String |
int | Number |
float | Number |
True | true |
False | false |
None | null |
json.dumps()
method has parameters to
make it easier to read the result:
Example
Use the indent
parameter to define the numbers
of indents:
json.dumps(x, indent=4)
You can also define the separators, default value is (", ", ": "), which
means using a comma and a space to separate each object, and a colon and a space
to separate keys from values:
Example
Use the separators
parameter to change the
default separator:
json.dumps(x, indent=4, separators=(". ", " = "))
json.dumps()
method has parameters to
order the keys in the result:
Example
Use the sort_keys
parameter to specify if
the result should be sorted or not:
json.dumps(x, indent=4, sort_keys=True)
re
, which can be used to work with
Regular Expressions.
Import the re
module:
import re
re
module, you
can start using regular expressions:
Example
Search the string to see if it starts with "The" and ends with "Spain":
import
re
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
re
module offers a set of functions that allows
us to search a string for a match:
Function | Description |
---|---|
findall | Returns a list containing all matches |
search | Returns a Match object if there is a match anywhere in the string |
split | Returns a list where the string has been split at each match |
sub | Replaces one or many matches with a string |
Character | Description | Example | Try it |
---|---|---|---|
[] | A set of characters | "[a-m]" | |
\ | Signals a special sequence (can also be used to escape special characters) | "\d" | |
. | Any character (except newline character) | "he..o" | |
^ | Starts with | "^hello" | |
$ | Ends with | "planet$" | |
* | Zero or more occurrences | "he.*o" | |
+ | One or more occurrences | "he.+o" | |
? | Zero or one occurrences | "he.?o" | |
{} | Exactly the specified number of occurrences | "he.{2}o" | |
| | Either or | "falls|stays" | |
() | Capture and group |
\
followed by one of the characters in the list below, and has a special meaning:
Character | Description | Example | Try it |
---|---|---|---|
\A | Returns a match if the specified characters are at the beginning of the string | "\AThe" | |
\b | Returns a match where the specified characters are at the beginning or at the end of a word (the "r" in the beginning is making sure that the string is being treated as a "raw string") | r"\bain" r"ain\b" | |
\B | Returns a match where the specified characters are present, but NOT at the beginning (or at the end) of a word (the "r" in the beginning is making sure that the string is being treated as a "raw string") | r"\Bain" r"ain\B" | |
\d | Returns a match where the string contains digits (numbers from 0-9) | "\d" | |
\D | Returns a match where the string DOES NOT contain digits | "\D" | |
\s | Returns a match where the string contains a white space character | "\s" | |
\S | Returns a match where the string DOES NOT contain a white space character | "\S" | |
\w | Returns a match where the string contains any word characters (characters from a to Z, digits from 0-9, and the underscore _ character) | "\w" | |
\W | Returns a match where the string DOES NOT contain any word characters | "\W" | |
\Z | Returns a match if the specified characters are at the end of the string | "Spain\Z" |
[]
with a special meaning:
Set | Description | Try it |
---|---|---|
[arn] | Returns a match where one of the specified characters (a ,
r , or n ) is
present | |
[a-n] | Returns a match for any lower case character, alphabetically between
a and n | |
[^arn] | Returns a match for any character EXCEPT a ,
r , and n | |
[0123] | Returns a match where any of the specified digits (0 ,
1 , 2 , or
3 ) are
present | |
[0-9] | Returns a match for any digit between
0 and 9 | |
[0-5][0-9] | Returns a match for any two-digit numbers from 00 and
59 | |
[a-zA-Z] | Returns a match for any character alphabetically between
a and z , lower case OR upper case | |
[+] | In sets, + , * ,
. , | ,
() , $ ,{}
has no special meaning, so [+] means: return a match for any
+ character in the string |
findall()
function returns a list containing all matches.
Example
Print a list of all matches:
import re
txt = "The rain in Spain"
x = re.findall("ai",
txt)
print(x)
The list contains the matches in the order they are found.
If no matches are found, an empty list is returned:
Example
Return an empty list if no match was found:
import re
txt = "The rain in Spain"
x = re.findall("Portugal",
txt)
print(x)
search()
function searches the string
for a match, and returns a Match object if there is a
match.
If there is more than one match,
only the first occurrence of the match will be returned:
Example
Search for the first white-space character in the string:
import re
txt = "The rain in Spain"
x = re.search("\s",
txt)
print("The first white-space character is located in
position:", x.start())
If no matches are found, the value None
is returned:
Example
Make a search that returns no match:
import re
txt = "The rain in Spain"
x = re.search("Portugal",
txt)
print(x)
split()
function returns a list where
the string has been split at each match:
Example
Split at each white-space character:
import re
txt = "The rain in Spain"
x = re.split("\s",
txt)
print(x)
You can control the number of occurrences by specifying the
maxsplit
parameter:
Example
Split the string only at the first occurrence:
import re
txt = "The rain in Spain"
x = re.split("\s",
txt,
1)
print(x)
sub()
function replaces the matches with
the text of your choice:
Example
Replace every white-space character with the number 9:
import re
txt = "The rain in Spain"
x = re.sub("\s",
"9", txt)
print(x)
You can control the number of replacements by specifying the
count
parameter:
Example
Replace the first 2 occurrences:
import re
txt = "The rain in Spain"
x = re.sub("\s",
"9", txt, 2)
print(x)
None
will be
returned, instead of the Match Object.
Example
Do a search that will return a Match Object:
import re
txt = "The rain in Spain"
x = re.search("ai",
txt)
print(x) #this will print an object
The Match object has properties and methods used to retrieve information
about the search, and the result:
.span()
returns a tuple containing the start-, and end positions of the match.
.string
returns the string passed into the function
.group()
returns the part of the string where there was a match
Example
Print the position (start- and end-position) of the first match occurrence.
The regular expression looks for any words that starts with an upper case
"S":
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.span())
Example
Print the string passed into the function:
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.string)
Example
Print the part of the string where there was a match.
The regular expression looks for any words that starts with an upper case
"S":
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.group())
Note: If there is no match, the value None
will be
returned, instead of the Match Object.
uninstall
command to remove a package:
Example
Uninstall the package named "camelcase":
C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>pip
uninstall camelcase
The PIP Package Manager will ask you to confirm that you want to remove the
camelcase package:
Uninstalling camelcase-02.1:
Would remove:
c:\users\Your Name\appdata\local\programs\python\python36-32\lib\site-packages\camecase-0.2-py3.6.egg-info
c:\users\Your Name\appdata\local\programs\python\python36-32\lib\site-packages\camecase\*
Proceed (y/n)?
Press y
and the package will be removed.
list
command to list all the packages installed on your system:
Example
List installed packages:
C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>pip list
Result:
Package Version
-----------------------
camelcase 0.2
mysql-connector 2.1.6
pip
18.1
pymongo 3.6.1
setuptools 39.0.1
try
block lets you test a
block of code for errors.
The except
block lets you
handle the error.
The else
block lets you
execute code when there is no error.
The finally
block lets you
execute code, regardless of the result of the try- and except blocks.
try
statement:
Example
The try
block will generate an exception,
because x
is not defined:
try:
print(x)
except:
print("An exception occurred")
Since the try block raises an error, the except block will be executed.
Without the try block, the program will crash and raise an error:
Example
This statement will raise an error,
because x
is not defined:
print(x)
NameError
and another
for other errors:
try:
print(x)
except NameError:
print("Variable x
is not defined")
except:
print("Something else went
wrong")
else
keyword to define a
block of code to be executed if no errors were raised:
Example
In this example, the try
block does not
generate any error:
try:
print("Hello")
except:
print("Something went
wrong")
else:
print("Nothing went wrong")
finally
block, if specified, will be executed
regardless if the try block
raises an error or not.
Example
try:
print(x)
except:
print("Something went
wrong")
finally:
print("The 'try except' is finished")
This can be useful to close objects and clean up resources:
Example
Try to open and write to a file that is not writable:
try:
f = open("demofile.txt")
try:
f.write("Lorum Ipsum")
except:
print("Something went wrong when writing to the file")
finally:
f.close()
except:
print("Something went wrong when opening the
file")
Try it Yourself »
The program can continue, without leaving the file object open.
raise
keyword.
Example
Raise an error and stop the program if x is lower than 0:
x = -1
if x < 0:
raise Exception("Sorry, no numbers below
zero")
The raise
keyword is used to raise an
exception.
You can define what kind of error to raise, and the text to print to the user.
Example
Raise a TypeError if x is not an integer:
x = "hello"
if not type(x) is int:
raise TypeError("Only
integers are allowed")
input()
method.
Python 2.7 uses the raw_input()
method.
The following example asks for the username, and when you entered the username, it gets printed on
the screen:
input()
function, and continues
when the user has given some input.
format()
method.
format()
method allows you to format selected parts of a string.
Sometimes there are parts of a text that you do not control, maybe
they come from a database, or user input?
To control such values,
add placeholders (curly brackets {}
) in the text, and run the values through the
format()
method:
Example
Add a placeholder where you want to display the price:
price = 49
txt = "The price is {} dollars"
print(txt.format(price))
You can add parameters inside the curly brackets to specify how to convert
the value:
Example
Format the price to be displayed as a number with two decimals:
txt = "The price is {:.2f} dollars"
Check out all formatting types in our String format() Reference.
{0}
) to be sure the
values are placed
in the correct placeholders:
Example
quantity = 3
itemno = 567
price = 49
myorder = "I want {0} pieces of
item number {1} for {2:.2f} dollars."
print(myorder.format(quantity, itemno, price))
Also, if you want to refer to the same value more than once, use the index number:
Example
age = 36
name = "John"
txt = "His name is {1}. {1} is {0} years old."
print(txt.format(age,
name))
{carname}
,
but then you must use names when you pass the parameter values
txt.format(carname = "Ford")
:
Example
myorder = "I have a {carname}, it is a {model}."
print(myorder.format(carname
= "Ford", model = "Mustang"))
open()
function.
The open()
function takes two parameters;
filename, and mode.
There are four different methods (modes) for opening a file:
"r"
- Read - Default value. Opens a
file for reading, error if the file does not exist
"a"
- Append - Opens a file for
appending, creates the file if it does not exist
"w"
- Write - Opens a file for writing,
creates the file if it does not exist
"x"
- Create - Creates the specified file, returns
an error if the file exists
In addition you can specify if the file should be handled as binary or text mode
"t"
- Text - Default value. Text mode
"b"
- Binary - Binary mode (e.g.
images)
"r"
for read, and
"t"
for text are the default values, you do not need to specify them.
Note: Make sure the file exists, or else you will get an error.
open()
function.
The open()
function returns a file object, which has a
read()
method for reading the content of the file:
Example
f = open("demofile.txt", "r")
print(f.read())
Run Example »
If the file is located in a different location, you will have to specify the file path,
like this:
Example
Open a file on a different location:
f = open("D:\\myfiles\welcome.txt", "r")
print(f.read())
Run Example »
read()
method returns the whole text, but you can also specify how many characters you want to return:
Example
Return the 5 first characters of the file:
f = open("demofile.txt", "r")
print(f.read(5))
Run Example »
readline()
method:
Example
Read one line of the file:
f = open("demofile.txt", "r")
print(f.readline())
Run Example »
By calling readline()
two times, you can read the
two first lines:
Example
Read two lines of the file:
f = open("demofile.txt", "r")
print(f.readline())
print(f.readline())
Run Example »
By looping through the lines of the file, you can read the whole file, line by line:
Example
Loop through the file line by line:
f = open("demofile.txt", "r")
for x in f:
print(x)
Run Example »
open()
function:
"a"
- Append - will append to the end of the file
"w"
- Write - will overwrite any existing content
Example
Open the file "demofile2.txt" and append content to the file:
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
#open and read the file after the appending:
f =
open("demofile2.txt", "r")
print(f.read())
Run Example »
Example
Open the file "demofile3.txt" and overwrite the content:
f = open("demofile3.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()
#open and read the file after the appending:
f = open("demofile3.txt", "r")
print(f.read())
Run Example »
Note: the "w" method will overwrite the entire file.
open()
method,
with one of the following parameters:
"x"
- Create - will create a file, returns
an error if the file exist
"a"
- Append - will create a file if the
specified file does not exist
"w"
- Write - will create a file if the
specified file does not exist
Example
Create a file called "myfile.txt":
f = open("myfile.txt", "x")
Result: a new empty file is created!
Example
Create a new file if it does not exist:
f = open("myfile.txt", "w")
os.remove()
function:
Example
Remove the file "demofile.txt":
import os
os.remove("demofile.txt")
os.rmdir()
method:
Example
Remove the folder "myfolder":
import os
os.rmdir("myfolder")
Note: You can only remove empty folders.
import module
statement:
import matplotlib
Now Matplotlib is imported and ready to use:
__version__
attribute.
Example
import matplotlib
print(matplotlib.__version__)
Note: two underscore characters are used in __version__
.
pyplot
submodule,
and are usually imported under the plt
alias:
import matplotlib.pyplot as plt
Now the Pyplot package can be referred to as plt
.
Example
Draw a line in a diagram from position (0,0) to position (6,250):
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([0, 6])
ypoints = np.array([0, 250])
plt.plot(xpoints,
ypoints)
plt.show()
plot()
function is used to draw points (markers) in a diagram.
By default, the plot()
function draws a line from point to point.
The function takes parameters for specifying points in the diagram.
Parameter 1 is an array containing the points on the x-axis.
Parameter 2 is an array containing the points on the y-axis.
If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8] and [3, 10] to the plot function.
Example
Draw a line in a diagram from position (1, 3) to position (8, 10):
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1, 8])
ypoints = np.array([3, 10])
plt.plot(xpoints, ypoints)
plt.show()
marker
to
emphasize each point with a specified marker:
Example
Mark each point with a circle:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker = 'o')
plt.show()
Marker | Description | |
---|---|---|
'o' | Circle | |
'*' | Star | |
'.' | Point | |
',' | Pixel | |
'x' | X | |
'X' | X (filled) | |
'+' | Plus | |
'P' | Plus (filled) | |
's' | Square | |
'D' | Diamond | |
'd' | Diamond (thin) | |
'p' | Pentagon | |
'H' | Hexagon | |
'h' | Hexagon | |
'v' | Triangle Down | |
'^' | Triangle Up | |
'<' | Triangle Left | |
'>' | Triangle Right | |
'1' | Tri Down | |
'2' | Tri Up | |
'3' | Tri Left | |
'4' | Tri Right | |
'|' | Vline | |
'_' | Hline |
fmt
fmt
, and is written with this syntax:
marker|line|color
Example
Mark each point with a circle:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, 'o:r')
plt.show()
Line Syntax | Description | |
---|---|---|
'-' | Solid line | |
':' | Dotted line | |
'--' | Dashed line | |
'-.' | Dashed/dotted line |
Color Syntax | Description | |
---|---|---|
'r' | Red | |
'g' | Green | |
'b' | Blue | |
'c' | Cyan | |
'm' | Magenta | |
'y' | Yellow | |
'k' | Black | |
'w' | White |
markersize
or the
shorter version, ms
to set the size of the markers:
Example
Set the size of the markers to 20:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker = 'o', ms = 20)
plt.show()
markeredgecolor
or
the shorter mec
to set the color of the
edge of the markers:
Example
Set the EDGE color to red:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker = 'o', ms = 20, mec = 'r')
plt.show()
markerfacecolor
or
the shorter mfc
to set the color inside the edge of the markers:
Example
Set the FACE color to red:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker = 'o', ms = 20, mfc = 'r')
plt.show()
mec
and mfc
arguments to color of the entire marker:
Example
Set the color of both the edge and the face to red:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker = 'o', ms = 20, mec = 'r', mfc = 'r')
plt.show()
linestyle
, or shorter ls
, to
change the style of the plotted line:
Example
Use a dotted line:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, linestyle = 'dotted')
plt.show()
linestyle
can be written as ls
.
dotted
can be written as :
.
dashed
can be written as --
.
Example
Shorter syntax:
plt.plot(ypoints, ls = ':')
Style | Or | |
---|---|---|
'solid' (default) | '-' | |
'dotted' | ':' | |
'dashed' | '--' | |
'dashdot' | '-.' | |
'None' | '' or ' ' |
color
or
the shorter c
to set the color of the line:
Example
Set the line color to red:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, color = 'r')
plt.show()
linewidth
or
the shorter lw
to change the width of the line.
The value is a floating number, in points:
Example
Plot with a 20.5pt wide line:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, linewidth = '20.5')
plt.show()
plt.plot()
functions:
Example
Draw two lines by specifying a plt.plot()
function for each line:
import matplotlib.pyplot as plt
import numpy as np
y1 = np.array([3, 8, 1, 10])
y2 = np.array([6, 2, 7, 11])
plt.plot(y1)
plt.plot(y2)
plt.show()
plt.plot()
function.
(In the examples above we only specified the points on the y-axis, meaning that the points on the x-axis got the the default values (0, 1, 2, 3).)
The x- and y- values come in pairs:
Example
Draw two lines by specifiyng the x- and y-point values for both lines:
import matplotlib.pyplot as plt
import numpy as np
x1 = np.array([0, 1, 2, 3])
y1 = np.array([3, 8, 1, 10])
x2 = np.array([0, 1, 2, 3])
y2 = np.array([6, 2, 7, 11])
plt.plot(x1, y1, x2, y2)
plt.show()
xlabel()
and
ylabel()
functions to set a label for the x- and y-axis.
Example
Add labels to the x- and y-axis:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
plt.plot(x, y)
plt.xlabel("Average
Pulse")
plt.ylabel("Calorie Burnage")
plt.show()
title()
function to set a title for the plot.
Example
Add a plot title and labels for the x- and y-axis:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
plt.plot(x, y)
plt.title("Sports Watch Data")
plt.xlabel("Average
Pulse")
plt.ylabel("Calorie Burnage")
plt.show()
fontdict
parameter in
xlabel()
, ylabel()
,
and title()
to set font properties for the
title and labels.
Example
Set font properties for the title and labels:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
font1 = {'family':'serif','color':'blue','size':20}
font2 = {'family':'serif','color':'darkred','size':15}
plt.title("Sports
Watch Data", fontdict = font1)
plt.xlabel("Average Pulse", fontdict =
font2)
plt.ylabel("Calorie Burnage", fontdict = font2)
plt.plot(x,
y)
plt.show()
loc
parameter in
title()
to position the title.
Legal values are: 'left', 'right', and 'center'. Default value is 'center'.
Example
Position the title to the left:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
plt.title("Sports Watch Data", loc = 'left')
plt.xlabel("Average
Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x,
y)
plt.show()
grid()
function to add grid lines to the plot.
Example
Add grid lines to the plot:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
plt.title("Sports Watch Data")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x,
y)
plt.grid()
plt.show()
axis
parameter in
the grid()
function to specify which grid lines
to display.
Legal values are: 'x', 'y', and 'both'. Default value is 'both'.
Example
Display only grid lines for the x-axis:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
plt.title("Sports Watch Data")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x,
y)
plt.grid(axis = 'x')
plt.show()
subplot()
function you can draw multiple plots in one figure:
Example
Draw 2 plots:
import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x =
np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.show()
subplot()
function takes three arguments that describes the layout of the figure.
The layout is organized in rows and columns, which are represented by the first
and second argument.
The third argument represents the index of the current plot.
plt.subplot(1, 2, 1)
#the figure has 1 row, 2 columns, and this plot is the first plot.
plt.subplot(1, 2, 2)
#the figure has 1 row, 2 columns, and this plot is the second plot.
So, if we want a figure with 2 rows an 1 column (meaning that the two plots will be displayed on top of each other instead of side-by-side),
we can write the syntax like this:
Example
Draw 2 plots on top of each other:
import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x =
np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 1, 1)
plt.plot(x,y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
40])
plt.subplot(2, 1, 2)
plt.plot(x,y)
plt.show()
title()
function:
Example
2 plots, with titles:
import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x =
np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("SALES")
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")
plt.show()
suptitle()
function:
Example
Add a title for the entire figure:
import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x =
np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("SALES")
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")
plt.suptitle("MY SHOP")
plt.show()
scatter()
function
to draw a scatter plot.
The scatter()
function plots one dot for
each observation. It needs two arrays of the same length, one for the values of
the x-axis, and one for values on the y-axis:
Example
A simple scatter plot:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y)
plt.show()
color
or the c
argument:
Example
Set your own color of the markers:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x,
y, color = 'hotpink')
x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])
plt.scatter(x, y, color = '#88c999')
plt.show()
c
argument:
Note: You cannot use the color
argument for this, only the c
argument.
Example
Set your own color of the markers:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array(["red","green","blue","yellow","pink","black","orange","purple","beige","brown","gray","cyan","magenta"])
plt.scatter(x, y, c=colors)
plt.show()
cmap
with the value of the colormap, in this
case 'viridis'
which is one of the
built-in colormaps available in Matplotlib.
In addition you have to create an array with values (from 0 to 100), one value for each of
the point in the scatter plot:
Example
Create a color array, and specify a colormap in the scatter plot:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0,
10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])
plt.scatter(x, y, c=colors, cmap='viridis')
plt.show()
plt.colorbar()
statement:
Example
Include the actual colormap:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0,
10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])
plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar()
plt.show()
Name | Reverse | |||
---|---|---|---|---|
Accent | Accent_r | |||
Blues | Blues_r | |||
BrBG | BrBG_r | |||
BuGn | BuGn_r | |||
BuPu | BuPu_r | |||
CMRmap | CMRmap_r | |||
Dark2 | Dark2_r | |||
GnBu | GnBu_r | |||
Greens | Greens_r | |||
Greys | Greys_r | |||
OrRd | OrRd_r | |||
Oranges | Oranges_r | |||
PRGn | PRGn_r | |||
Paired | Paired_r | |||
Pastel1 | Pastel1_r | |||
Pastel2 | Pastel2_r | |||
PiYG | PiYG_r | |||
PuBu | PuBu_r | |||
PuBuGn | PuBuGn_r | |||
PuOr | PuOr_r | |||
PuRd | PuRd_r | |||
Purples | Purples_r | |||
RdBu | RdBu_r | |||
RdGy | RdGy_r | |||
RdPu | RdPu_r | |||
RdYlBu | RdYlBu_r | |||
RdYlGn | RdYlGn_r | |||
Reds | Reds_r | |||
Set1 | Set1_r | |||
Set2 | Set2_r | |||
Set3 | Set3_r | |||
Spectral | Spectral_r | |||
Wistia | Wistia_r | |||
YlGn | YlGn_r | |||
YlGnBu | YlGnBu_r | |||
YlOrBr | YlOrBr_r | |||
YlOrRd | YlOrRd_r | |||
afmhot | afmhot_r | |||
autumn | autumn_r | |||
binary | binary_r | |||
bone | bone_r | |||
brg | brg_r | |||
bwr | bwr_r | |||
cividis | cividis_r | |||
cool | cool_r | |||
coolwarm | coolwarm_r | |||
copper | copper_r | |||
cubehelix | cubehelix_r | |||
flag | flag_r | |||
gist_earth | gist_earth_r | |||
gist_gray | gist_gray_r | |||
gist_heat | gist_heat_r | |||
gist_ncar | gist_ncar_r | |||
gist_rainbow | gist_rainbow_r | |||
gist_stern | gist_stern_r | |||
gist_yarg | gist_yarg_r | |||
gnuplot | gnuplot_r | |||
gnuplot2 | gnuplot2_r | |||
gray | gray_r | |||
hot | hot_r | |||
hsv | hsv_r | |||
inferno | inferno_r | |||
jet | jet_r | |||
magma | magma_r | |||
nipy_spectral | nipy_spectral_r | |||
ocean | ocean_r | |||
pink | pink_r | |||
plasma | plasma_r | |||
prism | prism_r | |||
rainbow | rainbow_r | |||
seismic | seismic_r | |||
spring | spring_r | |||
summer | summer_r | |||
tab10 | tab10_r | |||
tab20 | tab20_r | |||
tab20b | tab20b_r | |||
tab20c | tab20c_r | |||
terrain | terrain_r | |||
twilight | twilight_r | |||
twilight_shifted | twilight_shifted_r | |||
viridis | viridis_r | |||
winter | winter_r |
s
argument.
Just like colors, make sure the array for sizes has the same length as the arrays for the x- and y-axis:
Example
Set your own size for the markers:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
sizes =
np.array([20,50,100,200,500,1000,60,90,10,300,600,800,75])
plt.scatter(x,
y, s=sizes)
plt.show()
alpha
argument.
Just like colors, make sure the array for sizes has the same length as the arrays for the x- and y-axis:
Example
Set your own size for the markers:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
sizes =
np.array([20,50,100,200,500,1000,60,90,10,300,600,800,75])
plt.scatter(x,
y, s=sizes, alpha=0.5)
plt.show()
bar()
function
to draw bar graphs:
Example
Draw 4 bars:
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["A",
"B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.bar(x,y)
plt.show()
bar()
function takes arguments that describes the
layout of the bars.
The categories and their values represented by the first
and second argument as arrays.
Example
x = ["APPLES", "BANANAS"]
y = [400, 350]
plt.bar(x, y)
barh()
function:
Example
Draw 4 horizontal bars:
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["A",
"B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.barh(x, y)
plt.show()
bar()
and barh()
takes the keyword argument
color
to set the color of the bars:
Example
Draw 4 red bars:
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["A",
"B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.bar(x, y, color = "red")
plt.show()
bar()
takes the keyword argument
width
to set the width of the bars:
Example
Draw 4 very thin bars:
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["A",
"B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.bar(x, y, width = 0.1)
plt.show()
height
instead of width
.
barh()
takes the keyword argument
height
to set the height of the bars:
Example
Draw 4 very thin bars:
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["A",
"B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.barh(x, y, height = 0.1)
plt.show()
hist()
function to
create histograms.
The hist()
function will use an array of
numbers to create a histogram, the array is sent into the function as an
argument.
For simplicity we use NumPy to randomly generate an array with 250 values,
where the values will concentrate around 170, and the standard deviation is 10.
Learn more about Normal Data
Distribution in our Machine Learning
Tutorial.
Example
A Normal Data Distribution by NumPy:
import numpy as np
x =
np.random.normal(170, 10, 250)
print(x)
hist()
function will read the array and produce a histogram:
Example
A simple histogram:
import matplotlib.pyplot as plt
import numpy as np
x =
np.random.normal(170, 10, 250)
plt.hist(x)
plt.show()
pie()
function
to draw pie charts:
Example
A simple pie chart:
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35,
25, 25, 15])
plt.pie(y)
plt.show()
x/sum(x)
label
parameter.
The label
parameter must be an array with one label for each wedge:
Example
A simple pie chart:
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35,
25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
plt.pie(y,
labels = mylabels)
plt.show()
startangle
parameter.
The startangle
parameter is defined with an angle in degrees, default angle is 0:
explode
parameter allows you to do that.
The explode
parameter, if specified, and not None
,
must be an array with one value for each wedge.
Each value represents how far from the center each wedge is displayed:
Example
Pull the "Apples" wedge 0.2 from the center of the pie:
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35,
25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]
plt.pie(y,
labels = mylabels, explode = myexplode)
plt.show()
shadows
parameter to True
:
Example
Add a shadow:
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35,
25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]
plt.pie(y,
labels = mylabels, explode = myexplode, shadow = True)
plt.show()
colors
parameter.
The colors
parameter, if specified,
must be an array with one value for each wedge:
Example
Specify a new color for each wedge:
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35,
25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
mycolors = ["black", "hotpink", "b", "#4CAF50"]
plt.pie(y, labels =
mylabels, colors = mycolors)
plt.show()
'r'
- Red
'g'
- Green
'b'
- Blue
'c'
- Cyan
'm'
- Magenta
'y'
- Yellow
'k'
- Black
'w'
- White
legend()
function:
Example
Add a legend:
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35,
25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
plt.pie(y, labels = mylabels)
plt.legend()
plt.show()
title
parameter to the legend
function.
Example
Add a legend with a header:
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35,
25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
plt.pie(y, labels = mylabels)
plt.legend(title = "Four Fruits:")
plt.show()
[99,86,87,88,111,86,103,87,94,78,77,85,86]
Example of a database:
Carname | Color | Age | Speed | AutoPass |
BMW | red | 5 | 99 | Y |
Volvo | black | 7 | 86 | Y |
VW | gray | 8 | 87 | N |
VW | white | 7 | 88 | Y |
Ford | white | 2 | 111 | Y |
VW | white | 17 | 86 | Y |
Tesla | red | 2 | 103 | Y |
BMW | black | 9 | 87 | Y |
Volvo | gray | 4 | 94 | N |
Ford | white | 11 | 78 | N |
Toyota | gray | 12 | 77 | N |
VW | white | 9 | 85 | N |
Toyota | blue | 6 | 86 | Y |
speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]
What is the average, the middle, or the most common speed value?
(99+86+87+88+111+86+103+87+94+78+77+85+86) / 13 =
89.77
The NumPy module has a method for this. Learn about the NumPy module in our NumPy Tutorial.
Example
Use the NumPy mean()
method to find the
average speed:
import numpy
speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]
x = numpy.mean(speed)
print(x)
77, 78, 85, 86, 86, 86,
87
, 87, 88, 94, 99, 103, 111
It is important that the numbers are sorted before you can find the median.
The NumPy module has a method for this:
Example
Use the NumPy median()
method to find the
middle value:
import numpy
speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]
x = numpy.median(speed)
print(x)
If there are two numbers in the middle, divide the sum of those numbers by
two.
77, 78, 85, 86, 86,
86, 87
,
87, 94, 98, 99, 103
(86 + 87) / 2 =
86.5
Example
Using the NumPy module:
import numpy
speed = [99,86,87,88,86,103,87,94,78,77,85,86]
x = numpy.median(speed)
print(x)
99,
86
, 87, 88, 111,
86
, 103, 87, 94, 78, 77, 85,
86
= 86
The SciPy module has a method for this. Learn about the SciPy module in our
SciPy Tutorial.
Example
Use the SciPy mode()
method to find the
number that appears the most:
from scipy import stats
speed =
[99,86,87,88,111,86,103,87,94,78,77,85,86]
x = stats.mode(speed)
print(x)
speed = [86,87,88,86,87,85,86]
The standard deviation is:
0.9
Meaning that most of the values are within the range of 0.9 from the mean
value, which is 86.4.
Let us do the same with a selection of numbers with a wider range:
speed = [32,111,138,28,59,77,97]
The standard deviation is:
37.85
Meaning that most of the values are within the range of 37.85 from the mean
value, which is 77.4.
As you can see, a higher standard deviation indicates that the values are
spread out over a wider range.
The NumPy module has a method to calculate the standard deviation:
Example
Use the NumPy std()
method to find the
standard deviation:
import numpy
speed = [86,87,88,86,87,85,86]
x = numpy.std(speed)
print(x)
Example
import numpy
speed = [32,111,138,28,59,77,97]
x = numpy.std(speed)
print(x)
(32+111+138+28+59+77+97) / 7 = 77.4
2. For each value: find the difference from the mean:
32 - 77.4 = -45.4
111 - 77.4 = 33.6
138
- 77.4 = 60.6
28 - 77.4 = -49.4
59 - 77.4 = -18.4
77
- 77.4 = - 0.4
97 - 77.4 = 19.6
3. For each difference: find the square value:
(-45.4)2 = 2061.16
(33.6)2 = 1128.96
(60.6)2 = 3672.36
(-49.4)2 = 2440.36
(-18.4)2 = 338.56
(- 0.4)2 = 0.16
(19.6)2 = 384.16
4. The variance is the average number of these squared differences:
(2061.16+1128.96+3672.36+2440.36+338.56+0.16+384.16)
/ 7 = 1432.2
Luckily, NumPy has a method to calculate the variance:
Example
Use the NumPy var()
method to find the variance:
import numpy
speed = [32,111,138,28,59,77,97]
x = numpy.var(speed)
print(x)
1432.25 = 37.85
Or, as in the example from before, use the NumPy to calculate the standard deviation:
Example
Use the NumPy std()
method to find the standard deviation:
import numpy
speed = [32,111,138,28,59,77,97]
x = numpy.std(speed)
print(x)
ages = [5,31,43,48,50,41,7,11,15,39,80,82,32,2,8,6,25,36,27,61,31]
What is the 75. percentile? The answer is 43, meaning that 75% of the people
are 43 or younger.
The NumPy module has a method for finding the specified percentile:
Example
Use the NumPy percentile()
method to find
the percentiles:
import numpy
ages =
[5,31,43,48,50,41,7,11,15,39,80,82,32,2,8,6,25,36,27,61,31]
x = numpy.percentile(ages, 75)
print(x)
Example
What is the age that 90% of the people are younger than?
import numpy
ages =
[5,31,43,48,50,41,7,11,15,39,80,82,32,2,8,6,25,36,27,61,31]
x = numpy.percentile(ages, 90)
print(x)
numpy.random.normal()
method, with 100000 values, to draw a histogram with 100 bars.
We specify that the mean value is 5.0, and the standard deviation is 1.0.
Meaning that the values should be concentrated around 5.0, and rarely further
away than 1.0 from the mean.
And as you can see from the histogram, most values are between 4.0 and 6.0,
with a top at approximately 5.0.
x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
y = [99,86,87,88,111,86,103,87,94,78,77,85,86]
The x
array represents the age of each car.
The y
array represents the speed of each car.
Example
Use the scatter()
method to draw a scatter
plot diagram:
import matplotlib.pyplot as plt
x =
[5,7,8,7,2,17,2,9,4,11,12,9,6]
y =
[99,86,87,88,111,86,103,87,94,78,77,85,86]
plt.scatter(x, y)
plt.show()
scipy
and draw the line of Linear Regression:
import matplotlib.pyplot as plt
from scipy import stats
x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
y =
[99,86,87,88,111,86,103,87,94,78,77,85,86]
slope, intercept, r,
p, std_err = stats.linregress(x, y)
def myfunc(x):
return slope * x + intercept
mymodel = list(map(myfunc, x))
plt.scatter(x, y)
plt.plot(x, mymodel)
plt.show()
import matplotlib.pyplot as plt
from scipy
import stats
Create the arrays that represent the values of the x and y axis:
x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
y = [99,86,87,88,111,86,103,87,94,78,77,85,86]
Execute a method that returns some important key values of Linear Regression:
slope, intercept, r,
p, std_err = stats.linregress(x, y)
Create a function that uses the slope
and
intercept
values to return a new value. This
new value represents where on the y-axis the corresponding x value will be
placed:
def myfunc(x):
return slope * x + intercept
Run each value of the x array through the function. This will result in a new
array with new values for the y-axis:
mymodel = list(map(myfunc, x))
Draw the original scatter plot:
plt.scatter(x, y)
Draw the line of linear regression:
plt.plot(x, mymodel)
Display the diagram:
plt.show()
r
.
The r
value ranges from -1 to 1, where 0 means no relationship, and 1
(and -1)
means 100% related.
Python and the Scipy module will compute this value for you, all you have to
do is feed it with the x and y values.
Example
How well does my data fit in a linear regression?
from scipy import stats
x =
[5,7,8,7,2,17,2,9,4,11,12,9,6]
y =
[99,86,87,88,111,86,103,87,94,78,77,85,86]
slope, intercept, r,
p, std_err = stats.linregress(x, y)
print(r)
Note: The result -0.76 shows that there is a relationship,
not perfect, but it indicates that we could use linear regression in future
predictions.
myfunc()
function
from the example above:
def myfunc(x):
return slope * x + intercept
Example
Predict the speed of a 10 years old car:
from scipy import stats
x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
y =
[99,86,87,88,111,86,103,87,94,78,77,85,86]
slope, intercept, r,
p, std_err = stats.linregress(x, y)
def myfunc(x):
return slope * x + intercept
speed = myfunc(10)
print(speed)
The example predicted a speed at 85.6, which we also could read from the
diagram:
r
for relationship?
Example
You should get a very low r
value.
import numpy
from scipy import stats
x =
[89,43,36,36,95,10,66,34,38,20,26,29,48,64,6,5,36,66,72,40]
y =
[21,46,3,35,67,95,53,72,58,10,26,34,90,33,38,20,56,2,47,15]
slope, intercept, r,
p, std_err = stats.linregress(x, y)
print(r)
The result: 0.013 indicates a very bad relationship, and tells us that this data set is not suitable for linear regression.
numpy
and
matplotlib
then draw the line of
Polynomial Regression:
import numpy
import matplotlib.pyplot as plt
x = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y =
[100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]
mymodel =
numpy.poly1d(numpy.polyfit(x, y, 3))
myline = numpy.linspace(1, 22, 100)
plt.scatter(x, y)
plt.plot(myline, mymodel(myline))
plt.show()
import numpy
import matplotlib.pyplot as plt
Create the arrays that represent the values of the x and y axis:
x = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y =
[100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]
NumPy has a method that lets us make a polynomial model:
mymodel =
numpy.poly1d(numpy.polyfit(x, y, 3))
Then specify how the line will display, we start at position 1, and end at
position 22:
myline = numpy.linspace(1, 22, 100)
Draw the original scatter plot:
plt.scatter(x, y)
Draw the line of polynomial regression:
plt.plot(myline, mymodel(myline))
Display the diagram:
plt.show()
mymodel
array
from the example above:
mymodel = numpy.poly1d(numpy.polyfit(x, y, 3))
Example
Predict the speed of a car passing at 17:00:
import numpy
from sklearn.metrics import r2_score
x =
[1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y =
[100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]
mymodel =
numpy.poly1d(numpy.polyfit(x, y, 3))
speed = mymodel(17)
print(speed)
The example predicted a speed to be 88.87, which we also could read from the diagram:
Car | Model | Volume | Weight | CO2 |
Toyota | Aygo | 1000 | 790 | 99 |
Mitsubishi | Space Star | 1200 | 1160 | 95 |
Skoda | Citigo | 1000 | 929 | 95 |
Fiat | 500 | 900 | 865 | 90 |
Mini | Cooper | 1500 | 1140 | 105 |
VW | Up! | 1000 | 929 | 105 |
Skoda | Fabia | 1400 | 1109 | 90 |
Mercedes | A-Class | 1500 | 1365 | 92 |
Ford | Fiesta | 1500 | 1112 | 98 |
Audi | A1 | 1600 | 1150 | 99 |
Hyundai | I20 | 1100 | 980 | 99 |
Suzuki | Swift | 1300 | 990 | 101 |
Ford | Fiesta | 1000 | 1112 | 99 |
Honda | Civic | 1600 | 1252 | 94 |
Hundai | I30 | 1600 | 1326 | 97 |
Opel | Astra | 1600 | 1330 | 97 |
BMW | 1 | 1600 | 1365 | 99 |
Mazda | 3 | 2200 | 1280 | 104 |
Skoda | Rapid | 1600 | 1119 | 104 |
Ford | Focus | 2000 | 1328 | 105 |
Ford | Mondeo | 1600 | 1584 | 94 |
Opel | Insignia | 2000 | 1428 | 99 |
Mercedes | C-Class | 2100 | 1365 | 99 |
Skoda | Octavia | 1600 | 1415 | 99 |
Volvo | S60 | 2000 | 1415 | 99 |
Mercedes | CLA | 1500 | 1465 | 102 |
Audi | A4 | 2000 | 1490 | 104 |
Audi | A6 | 2000 | 1725 | 114 |
Volvo | V70 | 1600 | 1523 | 109 |
BMW | 5 | 2000 | 1705 | 114 |
Mercedes | E-Class | 2100 | 1605 | 115 |
Volvo | XC70 | 2000 | 1746 | 117 |
Ford | B-Max | 1600 | 1235 | 104 |
BMW | 2 | 1600 | 1390 | 108 |
Opel | Zafira | 1600 | 1405 | 109 |
Mercedes | SLK | 2500 | 1395 | 120 |
import pandas
Learn about the Pandas module in our Pandas Tutorial.
The Pandas module allows us to read csv files and return a DataFrame object.
The file is meant for testing purposes only, you can download it here: data.csv
df = pandas.read_csv("data.csv")
Then make a list of the independent values and call this
variable X
.
Put the dependent values in a variable called y
.
X = df[['Weight', 'Volume']]
y = df['CO2']
Tip: It is common to name the list of independent values with a upper
case X, and the list of dependent values with a lower case y.
We will use some methods from the sklearn module, so we will have to import that module as well:
from sklearn import linear_model
From the sklearn module we will use the LinearRegression()
method
to create a linear regression object.
This object has a method called fit()
that takes
the independent and dependent values as parameters and fills the regression object with data that describes the relationship:
regr = linear_model.LinearRegression()
regr.fit(X, y)
Now we have a regression object that are ready to predict CO2 values based on
a car's weight and volume:
#predict the CO2 emission of a car where the weight
is 2300kg, and the volume is 1300cm3:
predictedCO2 = regr.predict([[2300, 1300]])
Example
See the whole example in action:
import pandas
from sklearn import linear_model
df = pandas.read_csv("data.csv")
X = df[['Weight', 'Volume']]
y = df['CO2']
regr =
linear_model.LinearRegression()
regr.fit(X, y)
#predict the CO2
emission of a car where the weight is 2300kg, and the volume is 1300cm3:
predictedCO2 = regr.predict([[2300, 1300]])
print(predictedCO2)
x
is a variable, then
2x
is x
two
times. x
is the unknown variable, and the
number 2
is the coefficient.
In this case, we can ask for the coefficient value of weight against CO2, and
for volume against CO2. The answer(s) we get tells us what would happen if we
increase, or decrease, one of the independent values.
Example
Print the coefficient values of the regression object:
import pandas
from sklearn import linear_model
df = pandas.read_csv("data.csv")
X = df[['Weight', 'Volume']]
y = df['CO2']
regr =
linear_model.LinearRegression()
regr.fit(X, y)
print(regr.coef_)
Car | Model | Volume | Weight | CO2 |
Toyota | Aygo | 1.0 | 790 | 99 |
Mitsubishi | Space Star | 1.2 | 1160 | 95 |
Skoda | Citigo | 1.0 | 929 | 95 |
Fiat | 500 | 0.9 | 865 | 90 |
Mini | Cooper | 1.5 | 1140 | 105 |
VW | Up! | 1.0 | 929 | 105 |
Skoda | Fabia | 1.4 | 1109 | 90 |
Mercedes | A-Class | 1.5 | 1365 | 92 |
Ford | Fiesta | 1.5 | 1112 | 98 |
Audi | A1 | 1.6 | 1150 | 99 |
Hyundai | I20 | 1.1 | 980 | 99 |
Suzuki | Swift | 1.3 | 990 | 101 |
Ford | Fiesta | 1.0 | 1112 | 99 |
Honda | Civic | 1.6 | 1252 | 94 |
Hundai | I30 | 1.6 | 1326 | 97 |
Opel | Astra | 1.6 | 1330 | 97 |
BMW | 1 | 1.6 | 1365 | 99 |
Mazda | 3 | 2.2 | 1280 | 104 |
Skoda | Rapid | 1.6 | 1119 | 104 |
Ford | Focus | 2.0 | 1328 | 105 |
Ford | Mondeo | 1.6 | 1584 | 94 |
Opel | Insignia | 2.0 | 1428 | 99 |
Mercedes | C-Class | 2.1 | 1365 | 99 |
Skoda | Octavia | 1.6 | 1415 | 99 |
Volvo | S60 | 2.0 | 1415 | 99 |
Mercedes | CLA | 1.5 | 1465 | 102 |
Audi | A4 | 2.0 | 1490 | 104 |
Audi | A6 | 2.0 | 1725 | 114 |
Volvo | V70 | 1.6 | 1523 | 109 |
BMW | 5 | 2.0 | 1705 | 114 |
Mercedes | E-Class | 2.1 | 1605 | 115 |
Volvo | XC70 | 2.0 | 1746 | 117 |
Ford | B-Max | 1.6 | 1235 | 104 |
BMW | 2 | 1.6 | 1390 | 108 |
Opel | Zafira | 1.6 | 1405 | 109 |
Mercedes | SLK | 2.5 | 1395 | 120 |
z = (x - u) / s
Where z
is the new value,
x
is the original value,
u
is the mean and s
is the
standard deviation.
If you take the weight column from the data set above, the first value
is 790, and the scaled value will be:
(790 - 1292.23) / 238.74 = -2.1
If you take the volume column from the data set above, the first value
is 1.0, and the scaled value
will be:
(1.0 - 1.61) / 0.38 = -1.59
Now you can compare -2.1 with -1.59 instead of comparing 790 with 1.0.
You do not have to do this manually,
the Python sklearn module has a method called StandardScaler()
which returns a Scaler object with methods for transforming data sets.
Example
Scale all values in the Weight and Volume columns:
import pandas
from sklearn import linear_model
from
sklearn.preprocessing import StandardScaler
scale = StandardScaler()
df = pandas.read_csv("data.csv")
X = df[['Weight', 'Volume']]
scaledX = scale.fit_transform(X)
print(scaledX)
train_x = x[:80]
train_y = y[:80]
test_x = x[80:]
test_y = y[80:]
plot()
method of the matplotlib module:
Example
Draw a polynomial regression line through the data points:
import numpy
import
matplotlib.pyplot as plt
numpy.random.seed(2)
x =
numpy.random.normal(3, 1, 100)
y = numpy.random.normal(150, 40, 100) / x
train_x = x[:80]
train_y = y[:80]
test_x = x[80:]
test_y =
y[80:]
mymodel = numpy.poly1d(numpy.polyfit(train_x, train_y, 4))
myline = numpy.linspace(0, 6, 100)
plt.scatter(train_x, train_y)
plt.plot(myline, mymodel(myline))
plt.show()
r2_score()
that will help us find this relationship.
In this case we would like to measure the relationship
between the minutes a customer stays in the shop and how much money they spend.
Example
How well does my training data fit in a polynomial regression?
import numpy
from sklearn.metrics import r2_score
numpy.random.seed(2)
x = numpy.random.normal(3, 1, 100)
y = numpy.random.normal(150, 40,
100) / x
train_x = x[:80]
train_y = y[:80]
test_x = x[80:]
test_y = y[80:]
mymodel = numpy.poly1d(numpy.polyfit(train_x, train_y,
4))
r2 = r2_score(train_y, mymodel(train_x))
print(r2)
Note: The result 0.799 shows that there is a OK relationship.
Age | Experience | Rank | Nationality | Go |
36 | 10 | 9 | UK | NO |
42 | 12 | 4 | USA | NO |
23 | 4 | 6 | N | NO |
52 | 4 | 4 | USA | NO |
43 | 21 | 8 | USA | YES |
44 | 14 | 5 | UK | NO |
66 | 3 | 7 | N | YES |
35 | 14 | 9 | UK | YES |
52 | 13 | 7 | N | YES |
35 | 5 | 9 | N | YES |
24 | 3 | 5 | USA | NO |
18 | 3 | 7 | UK | YES |
45 | 9 | 9 | UK | YES |
map()
method that takes a dictionary with information on how to
convert the values.
{'UK': 0, 'USA': 1, 'N': 2}
Means convert the values 'UK' to 0, 'USA' to 1, and 'N' to 2.
Example
Change string values into numerical values:
d = {'UK': 0,
'USA': 1, 'N': 2}
df['Nationality'] = df['Nationality'].map(d)
d =
{'YES': 1, 'NO': 0}
df['Go'] = df['Go'].map(d)
print(df)
Run example »
Then we have to separate the feature columns from the target column.
The feature columns are the columns that we try to predict from, and
the target column is the column with the values we try to predict.
Example
X
is the feature columns,
y
is the target column:
features = ['Age', 'Experience', 'Rank', 'Nationality']
X = df[features]
y = df['Go']
print(X)
print(y)
Run example »
Now we can create the actual decision tree, fit it with our details. Start by
importing the modules we need:
Example
Create and display a Decision Tree:
import pandas
from sklearn import tree
from sklearn.tree import
DecisionTreeClassifier
import matplotlib.pyplot as plt
df =
pandas.read_csv("data.csv")
d = {'UK': 0, 'USA': 1, 'N': 2}
df['Nationality']
= df['Nationality'].map(d)
d = {'YES': 1, 'NO': 0}
df['Go'] = df['Go'].map(d)
features = ['Age', 'Experience', 'Rank', 'Nationality']
X = df[features]
y = df['Go']
dtree = DecisionTreeClassifier()
dtree = dtree.fit(X,
y)
tree.plot_tree(dtree, feature_names=features)
Run example »
Rank <= 6.5
means that every comedian with a rank of 6.5 or
lower will follow the
True
arrow (to the left), and the rest will
follow the False
arrow (to the right).
gini = 0.497
refers to the quality of the
split, and is always a number between 0.0 and 0.5, where 0.0 would mean all of
the samples got the same result, and 0.5 would mean that the split is done
exactly in the middle.
samples = 13
means that there are 13
comedians left at this point in the decision, which is all of them since this is
the first step.
value = [6, 7]
means that of these 13
comedians, 6 will get a "NO", and 7 will get a
"GO".
Gini = 1 - (x/n)2 + (y/n)2
Where x
is the number of positive answers("GO"),
n
is the number of samples, and
y
is the number of negative answers ("NO"),
which gives us this calculation:
1 - (7 / 13)2 + (6 / 13)2 = 0.497
gini = 0.0
means all of the samples got the
same result.
samples = 5
means that there are 5 comedians
left in this branch (5 comedian with a Rank of 6.5 or lower).
value = [5, 0]
means that 5 will get a "NO"
and 0 will get a "GO".
Nationality <= 0.5
means that the comedians
with a nationality value of less than 0.5 will follow the arrow to the left
(which means everyone from the UK, ), and the rest will follow the arrow to the
right.
gini = 0.219
means that about 22% of the
samples would go in one direction.
samples = 8
means that there are 8 comedians
left in this branch (8 comedian with a Rank higher than 6.5).
value = [1, 7]
means that of these 8
comedians, 1 will get a "NO" and 7 will get a "GO".
Age <= 35.5
means that comedians
at the age of 35.5 or younger will follow the arrow to the left, and the rest will follow the arrow to the
right.
gini = 0.375
means that about 37,5% of the
samples would go in one direction.
samples = 4
means that there are 4 comedians
left in this branch (4 comedians from the UK).
value = [1, 3]
means that of these 4
comedians, 1 will get a "NO" and 3 will get a "GO".
gini = 0.0
means all of the samples got the
same result.
samples = 4
means that there are 4 comedians
left in this branch (4 comedians not from the UK).
value = [0, 4]
means that of these 4
comedians, 0 will get a "NO" and 4 will get a "GO".
gini = 0.0
means all of the samples got the
same result.
samples = 2
means that there are 2 comedians
left in this branch (2 comedians at the age 35.5 or younger).
value = [0, 2]
means that of these 2
comedians, 0 will get a "NO" and 2 will get a "GO".
Experience <= 9.5
means that comedians
with 9.5 years of experience, or less, will follow the arrow to the left, and the rest will follow the arrow to the
right.
gini = 0.5
means that 50% of the samples
would go in one direction.
samples = 2
means that there are 2 comedians
left in this branch (2 comedians older than 35.5).
value = [1, 1]
means that of these 2
comedians, 1 will get a "NO" and 1 will get a "GO".
gini = 0.0
means all of the samples got the
same result.
samples = 1
means that there is 1 comedian
left in this branch (1 comedian with 9.5 years of experience or less).
value = [0, 1]
means that 0 will get a "NO" and
1 will get a "GO".
gini = 0.0
means all of the samples got the
same result.
samples = 1
means that there is 1 comedians
left in this branch (1 comedian with more than 9.5 years of experience).
value = [1, 0]
means that 1 will get a "NO" and
0 will get a "GO".
import numpy
Next we will need to generate the numbers for "actual" and "predicted" values.
actual = numpy.random.binomial(1, 0.9, size = 1000)
predicted = numpy.random.binomial(1, 0.9, size = 1000)
In order to create the confusion matrix we need to import metrics from the sklearn module.
from sklearn import metrics
Once metrics is imported we can use the confusion matrix function on our actual and predicted values.
confusion_matrix = metrics.confusion_matrix(actual, predicted)
To create a more interpretable visual display we need to convert the table into a confusion matrix display.
cm_display = metrics.ConfusionMatrixDisplay(confusion_matrix = confusion_matrix, display_labels = [False, True])
Vizualizing the display requires that we import pyplot from matplotlib.
import matplotlib.pyplot as plt
Finally to display the plot we can use the functions plot() and show() from pyplot.
cm_display.plot()
plt.show()
See the whole example in action:
Example
import matplotlib.pyplot as plt
import numpy
from sklearn import metrics
actual = numpy.random.binomial(1,.9,size = 1000)
predicted =
numpy.random.binomial(1,.9,size = 1000)
confusion_matrix =
metrics.confusion_matrix(actual, predicted)
cm_display =
metrics.ConfusionMatrixDisplay(confusion_matrix = confusion_matrix,
display_labels = [False, True])
cm_display.plot()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage
from sklearn.cluster import AgglomerativeClustering
You can learn about the Matplotlib module in our "Matplotlib Tutorial.
You can learn about the SciPy module in our SciPy Tutorial.
NumPy is a library for working with arrays and matricies in Python,
you can learn about the NumPy module in our NumPy Tutorial.
scikit-learn is a popular library for machine learning.
Create arrays that resemble two variables in a dataset. Note that while we only two variables here, this method will work with any number of variables:
x = [4, 5, 10, 4, 3, 11, 14 , 6, 10, 12]
y = [21, 19, 24, 17, 16, 25, 24, 22, 21, 21]
Turn the data into a set of points:
data = list(zip(x, y))
print(data)
Result:
[(4, 21), (5, 19), (10, 24), (4, 17), (3, 16), (11, 25), (14, 24), (6, 22), (10, 21), (12, 21)]
Compute the linkage between all of the different points. Here we use a simple euclidean distance measure and Ward's linkage, which seeks to minimize the variance between clusters.
linkage_data = linkage(data, method='ward', metric='euclidean')
Finally, plot the results in a dendrogram. This plot will show us the hierarchy of clusters from the bottom (individual points) to the top (a single cluster consisting of all data points).
plt.show()
lets us visualize the dendrogram instead of just the raw linkage data.
dendrogram(linkage_data)
plt.show()
Result:
AgglomerativeClustering
class with 2 clusters, using the same euclidean distance and Ward linkage.
hierarchical_cluster = AgglomerativeClustering(n_clusters=2, affinity='euclidean', linkage='ward')
The .fit_predict
method can be called on our data to compute the clusters using the defined parameters across our chosen number of clusters.
labels = hierarchical_cluster.fit_predict(data)
print(labels)
Result:
[0 0 1 0 0 1 1 0 1 1]
Finally, if we plot the same data and color the points using the labels assigned to each index by the hierarchical clustering method, we can see the cluster each point was assigned to:
plt.scatter(x, y, c=labels)
plt.show()
Result:
import numpy
Store the independent variables in X.
Store the dependent variable in y.
Below is a sample dataset:
#X represents the size of a tumor in centimeters.
X = numpy.array([3.78, 2.44, 2.09, 0.14, 1.72, 1.65, 4.92, 4.37, 4.96, 4.52, 3.69, 5.88]).reshape(-1,1)
#Note: X has to be reshaped into a column from a row for the LogisticRegression() function to work.
#y represents whether or not the tumor is cancerous (0 for "No", 1 for "Yes").
y = numpy.array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1])
We will use a method from the sklearn module, so we will have to import that module as well:
from sklearn import linear_model
From the sklearn module we will use the LogisticRegression() method to create a logistic regression object.
This object has a method called fit()
that takes the independent and dependent values as parameters and fills the regression object with data that describes the relationship:
logr = linear_model.LogisticRegression()
logr.fit(X,y)
Now we have a logistic regression object that is ready to whether a tumor is cancerous based on the tumor size:
#predict if tumor is cancerous where the size is 3.46mm:
predicted = logr.predict(numpy.array([3.46]).reshape(-1,1))
Example
See the whole example in action:
import numpy
from sklearn import linear_model
#Reshaped for Logistic function.
X = numpy.array([3.78, 2.44, 2.09, 0.14, 1.72, 1.65, 4.92, 4.37, 4.96, 4.52, 3.69, 5.88]).reshape(-1,1)
y = numpy.array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1])
logr = linear_model.LogisticRegression()
logr.fit(X,y)
#predict if tumor is cancerous where the size is 3.46mm:
predicted = logr.predict(numpy.array([3.46]).reshape(-1,1))
print(predicted)
[0]
We have predicted that a tumor with a size of 3.46mm will not be cancerous.
ADVERTISEMENT
[4.03541657]
This tells us that as the size of a tumor increases by 1mm the odds of it being a tumor increases by 4x.
def logit2prob(logr,x):
log_odds = logr.coef_ * x + logr.intercept_
odds = numpy.exp(log_odds)
probability = odds / (1 + odds)
return(probability)
log_odds = logr.coef_ * x + logr.intercept_
To then convert the log-odds to odds we must exponentiate the log-odds.
odds = numpy.exp(log_odds)
Now that we have the odds, we can convert it to probability by dividing it by 1 plus the odds.
probability = odds / (1 + odds)
Let us now use the function with what we have learned to find out the probability that each tumor is cancerous.
Example
See the whole example in action:
import numpy
from sklearn import linear_model
X = numpy.array([3.78, 2.44, 2.09, 0.14, 1.72, 1.65, 4.92, 4.37, 4.96, 4.52, 3.69, 5.88]).reshape(-1,1)
y = numpy.array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1])
logr = linear_model.LogisticRegression()
logr.fit(X,y)
def logit2prob(logr, X):
log_odds = logr.coef_ * X + logr.intercept_
odds = numpy.exp(log_odds)
probability = odds / (1 + odds)
return(probability)
print(logit2prob(logr, X))
sklearn
,
has a parameter C
that controls regularization,which affects the complexity of the model.
How do we pick the best value for C
?
The best value is dependent on the data used to train the model.
C
tell the model, the training data resembles real world information,
place a greater weight on the training data. While lower values of C
do the opposite.
from sklearn import datasets
iris = datasets.load_iris()
Next in order to create the model we must have a set of independent variables X and a dependant variable y.
X = iris['data']
y = iris['target']
Now we will load the logistic model for classifying the iris flowers.
from sklearn.linear_model import LogisticRegression
Creating the model, setting max_iter to a higher value to ensure that the model finds a result.
Keep in mind the default value for C
in a logistic regression model is 1
, we will compare this later.
In the example below, we look at the iris data set and try to train a model with varying values for C
in logistic regression.
logit = LogisticRegression(max_iter = 10000)
After we create the model, we must fit the model to the data.
print(logit.fit(X,y))
To evaluate the model we run the score method.
print(logit.score(X,y))
Example
from sklearn import datasets
from sklearn.linear_model import
LogisticRegression
iris = datasets.load_iris()
X = iris['data']
y = iris['target']
logit = LogisticRegression(max_iter = 10000)
print(logit.fit(X,y))
print(logit.score(X,y))
With the default setting of C = 1
, we achieved a score of 0.973
.
Let's see if we can do any better by implementing a grid search with difference values of 0.973.
ADVERTISEMENT
C
.
Knowing which values to set for the searched parameters will take a combination of domain knowledge and practice.
Since the default value for C
is 1
, we will set a range of values surrounding it.
C = [0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2]
Next we will create a for loop to change out the values of C
and evaluate the model with each change.
First we will create an empty list to store the score within.
scores = []
To change the values of C
we must loop over the range of values and update the parameter each time.
for choice in C:
logit.set_params(C=choice)
logit.fit(X, y)
scores.append(logit.score(X, y))
With the scores stored in a list, we can evaluate what the best choice of C
is.
print(scores)
Example
from sklearn import datasets
from sklearn.linear_model import
LogisticRegression
iris = datasets.load_iris()
X = iris['data']
y = iris['target']
logit = LogisticRegression(max_iter = 10000)
C = [0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2]
scores = []
for choice in C:
logit.set_params(C=choice)
logit.fit(X, y)
scores.append(logit.score(X, y))
print(scores)
C
performed worse than the base parameter of 1
. However, as we increased the value of C
to 1.75
the model experienced increased accuracy.
It seems that increasing C
beyond this amount does not help increase model accuracy.
get_dummies()
which does one hot encoding.
Learn about the Pandas module in our Pandas Tutorial.
Example
One Hot Encode the Car column:
import pandas as pd
cars = pd.read_csv('data.csv')
ohe_cars =
pd.get_dummies(cars[['Car']])
print(ohe_cars.to_string())
concat()
function from pandas.
First we will need to import a couple modules.
We will start with importing the Pandas.
import pandas
The pandas module allows us to read csv files and manipulate DataFrame objects:
cars = pandas.read_csv("data.csv")
It also allows us to create the dummy variables:
ohe_cars = pandas.get_dummies(cars[['Car']])
Then we must select the independent variables (X) and add the dummy variables columnwise.
Also store the dependent variable in y.
X = pandas.concat([cars[['Volume', 'Weight']], ohe_cars], axis=1)
y = cars['CO2']
We also need to import a method from sklearn to create a linear model
Learn about .
from sklearn import linear_model
Now we can fit the data to a linear regression:
regr = linear_model.LinearRegression()
regr.fit(X,y)
Finally we can predict the CO2 emissions based on the car's weight, volume, and manufacturer.
##predict the CO2 emission of a Volvo where the weight is 2300kg, and the volume is 1300cm3:
predictedCO2 = regr.predict([[2300, 1300,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0]])
Example
import pandas
from sklearn import linear_model
cars = pandas.read_csv("data.csv")
ohe_cars = pandas.get_dummies(cars[['Car']])
X = pandas.concat([cars[['Volume', 'Weight']], ohe_cars], axis=1)
y = cars['CO2']
regr = linear_model.LinearRegression()
regr.fit(X,y)
##predict the CO2 emission of a Volvo where the weight is 2300kg, and the volume is 1300cm3:
predictedCO2 = regr.predict([[2300, 1300,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0]])
print(predictedCO2)
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
You can learn about the Matplotlib module in our "Matplotlib Tutorial.
scikit-learn is a popular library for machine learning.
Create arrays that resemble two variables in a dataset. Note that while we only use two variables here, this method will work with any number of variables:
x = [4, 5, 10, 4, 3, 11, 14 , 6, 10, 12]
y = [21, 19, 24, 17, 16, 25, 24, 22, 21, 21]
Turn the data into a set of points:
data = list(zip(x, y))
print(data)
Result:
[(4, 21), (5, 19), (10, 24), (4, 17), (3, 16), (11, 25), (14, 24), (6, 22), (10, 21), (12, 21)]
In order to find the best value for K, we need to run K-means across our data for a range of possible values. We only have 10 data points, so the maximum number of clusters is 10. So for each value K in range(1,11), we train a K-means model and plot the intertia at that number of clusters:
inertias = []
for i in range(1,11):
kmeans = KMeans(n_clusters=i)
kmeans.fit(data)
inertias.append(kmeans.inertia_)
plt.plot(range(1,11), inertias, marker='o')
plt.title('Elbow method')
plt.xlabel('Number of clusters')
plt.ylabel('Inertia')
plt.show()
Result:
kmeans = KMeans(n_clusters=2)
kmeans.fit(data)
plt.scatter(x, y, c=kmeans.labels_)
plt.show()
Result:
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.tree import DecisionTreeClassifier
Next we need to load in the data and store it into X (input features) and y (target). The parameter as_frame is set equal to True so we do not lose the feature names when loading the data.
(sklearn
version older than 0.23 must skip the
as_frame
argument as it is not supported)
data = datasets.load_wine(as_frame = True)
X = data.data
y = data.target
In order to properly evaluate our model on unseen data, we need to split X and y into train and test sets. For information on splitting data, see the Train/Test lesson.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 22)
With our data prepared, we can now instantiate a base classifier and fit it to the training data.
dtree = DecisionTreeClassifier(random_state = 22)
dtree.fit(X_train,y_train)
Result:
DecisionTreeClassifier(random_state=22)
We can now predict the class of wine the unseen test set and evaluate the model performance.
y_pred = dtree.predict(X_test)
print("Train data accuracy:",accuracy_score(y_true = y_train, y_pred = dtree.predict(X_train)))
print("Test data accuracy:",accuracy_score(y_true = y_test, y_pred = y_pred))
Result:
Train data accuracy: 1.0
Test data accuracy: 0.8222222222222222
Example
Import the necessary data and evaluate base classifier performance.
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.tree import DecisionTreeClassifier
data = datasets.load_wine(as_frame = True)
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 22)
dtree = DecisionTreeClassifier(random_state = 22)
dtree.fit(X_train,y_train)
y_pred = dtree.predict(X_test)
print("Train data accuracy:",accuracy_score(y_true = y_train, y_pred = dtree.predict(X_train)))
print("Test data accuracy:",accuracy_score(y_true = y_test, y_pred = y_pred))
The base classifier performs reasonably well on the dataset achieving 82% accuracy on the test dataset with the current parameters (Different results may occur if you do not have the random_state
parameter set).
Now that we have a baseline accuracy for the test dataset, we can see how the Bagging Classifier out performs a single Decision Tree Classifier.
ADVERTISEMENT
from sklearn.ensemble import BaggingClassifier
Now lets create a range of values that represent the number of estimators we want to use in each ensemble.
estimator_range = [2,4,6,8,10,12,14,16]
To see how the Bagging Classifier performs with differing values of n_estimators we need a way to iterate over the range of values and store the results from each ensemble. To do this we will create a for loop, storing the models and scores in separate lists for later vizualizations.
Note: The default parameter for the base classifier in BaggingClassifier
is the DicisionTreeClassifier
therefore we do not need to set it when instantiating the bagging model.
models = []
scores = []
for n_estimators in estimator_range:
# Create bagging classifier
clf = BaggingClassifier(n_estimators = n_estimators, random_state = 22)
# Fit the model
clf.fit(X_train, y_train)
# Append the model and score to their respective list
models.append(clf)
scores.append(accuracy_score(y_true = y_test, y_pred = clf.predict(X_test)))
With the models and scores stored, we can now visualize the improvement in model performance.
import matplotlib.pyplot as plt
# Generate the plot of scores against number of estimators
plt.figure(figsize=(9,6))
plt.plot(estimator_range, scores)
# Adjust labels and font (to make visable)
plt.xlabel("n_estimators", fontsize = 18)
plt.ylabel("score", fontsize = 18)
plt.tick_params(labelsize = 16)
# Visualize plot
plt.show()
BaggingClassifier
performance.
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.ensemble import BaggingClassifier
data = datasets.load_wine(as_frame = True)
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 22)
estimator_range = [2,4,6,8,10,12,14,16]
models = []
scores = []
for n_estimators in estimator_range:
# Create bagging classifier
clf = BaggingClassifier(n_estimators = n_estimators, random_state = 22)
# Fit the model
clf.fit(X_train, y_train)
# Append the model and score to their respective list
models.append(clf)
scores.append(accuracy_score(y_true = y_test, y_pred = clf.predict(X_test)))
# Generate the plot of scores against number of estimators
plt.figure(figsize=(9,6))
plt.plot(estimator_range, scores)
# Adjust labels and font (to make visable)
plt.xlabel("n_estimators", fontsize = 18)
plt.ylabel("score", fontsize = 18)
plt.tick_params(labelsize = 16)
# Visualize plot
plt.show()
random_state
the values you see will vary.
That is why it is best practice to use to ensure stable results.
In this case, we see a 13.3% increase in accuracy when it comes to identifying the type of the wine.
oob_score
to true to evaluate the model with out-of-bag score.
Example
Create a model with out-of-bag metric.
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.ensemble import BaggingClassifier
data = datasets.load_wine(as_frame = True)
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 22)
oob_model = BaggingClassifier(n_estimators = 12, oob_score = True,random_state = 22)
oob_model.fit(X_train, y_train)
print(oob_model.oob_score_)
Since the samples used in OOB and the test set are different, and the dataset is relatively small, there is a difference in the accuracy. It is rare that they would be exactly the same, again OOB should be used quick means for estimating error, but is not the only evaluation metric.
plot_tree
function from sklearn.tree
. The different trees can be graphed by changing the estimator you wish to visualize.
Example
Generate Decision Trees from Bagging Classifier
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import plot_tree
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 22)
clf = BaggingClassifier(n_estimators = 12, oob_score = True,random_state = 22)
clf.fit(X_train, y_train)
plt.figure(figsize=(30, 20))
plot_tree(clf.estimators_[0], feature_names = X.columns)
from sklearn import datasets
X, y = datasets.load_iris(return_X_y=True)
There are many methods to cross validation, we will start by looking at k-fold cross validation.
DecisionTreeClassifier
. We will also need to import CV modules from sklearn
.
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import KFold, cross_val_score
With the data loaded we can now create and fit a model for evaluation.
clf = DecisionTreeClassifier(random_state=42)
Now let's evaluate our model and see how it performs on each k-fold.
k_folds = KFold(n_splits = 5)
scores = cross_val_score(clf, X, y, cv = k_folds)
It is also good pratice to see how CV performed overall by averaging the scores for all folds.
Example
Run k-fold CV:
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import KFold, cross_val_score
X, y = datasets.load_iris(return_X_y=True)
clf = DecisionTreeClassifier(random_state=42)
k_folds = KFold(n_splits = 5)
scores = cross_val_score(clf, X, y, cv = k_folds)
print("Cross Validation Scores: ", scores)
print("Average CV Score: ", scores.mean())
print("Number of CV Scores used in Average: ", len(scores))
ADVERTISEMENT
KFold
, ShuffleSplit
leaves out a percentage of the data, not to be used in the train or validation sets. To do so we must decide what the train and test sizes are, as well as the number of splits.
Example
Run Shuffle Split CV:
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import ShuffleSplit, cross_val_score
X, y = datasets.load_iris(return_X_y=True)
clf = DecisionTreeClassifier(random_state=42)
ss = ShuffleSplit(train_size=0.6, test_size=0.3, n_splits = 5)
scores = cross_val_score(clf, X, y, cv = ss)
print("Cross Validation Scores: ", scores)
print("Average CV Score: ", scores.mean())
print("Number of CV Scores used in Average: ", len(scores))
import matplotlib.pyplot as plt
def plot_roc_curve(true_y, y_prob):
"""
plots the roc curve based of the probabilities
"""
fpr, tpr, thresholds = roc_curve(true_y, y_prob)
plt.plot(fpr, tpr)
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
Example
Model 1:
plot_roc_curve(y, y_proba)
print(f'model 1 AUC score: {roc_auc_score(y, y_proba)}')
from sklearn.neighbors import KNeighborsClassifier
data = list(zip(x, y))
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(data, classes)
And use it to classify a new data point:
Example
new_x = 8
new_y = 21
new_point = [(new_x, new_y)]
prediction = knn.predict(new_point)
plt.scatter(x + [new_x], y + [new_y], c=classes + [prediction[0]])
plt.text(x=new_x-1.7, y=new_y-0.7, s=f"new point, class: {prediction[0]}")
plt.show()
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier
Create arrays that resemble variables in a dataset.
We have two input features (x
and y
) and then a target class (class
). The input features that are pre-labeled with our target class will be used to predict the class of new data. Note that while we only use two input features here, this method will work with any number of variables:
x = [4, 5, 10, 4, 3, 11, 14 , 8, 10, 12]
y = [21, 19, 24, 17, 16, 25, 24, 22, 21, 21]
classes = [0, 0, 1, 0, 0, 1, 1, 0, 1, 1]
Turn the input features into a set of points:
data = list(zip(x, y))
print(data)
[(4, 21), (5, 19), (10, 24), (4, 17), (3, 16), (11, 25), (14, 24), (8, 22), (10, 21), (12, 21)]
Using the input features and target class, we fit a KNN model on the model using 1 nearest neighbor:
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(data, classes)
Then, we can use the same KNN object to predict the class of new,
unforeseen data points. First we create new x and y features, and then call knn.predict()
on the new data point to get a class of 0 or 1:
new_x = 8
new_y = 21
new_point = [(new_x, new_y)]
prediction = knn.predict(new_point)
print(prediction)
[0]
When we plot all the data along with the new point and class, we can see it's been labeled blue with the 1
class. The text annotation is just to highlight the location of the new point:
plt.scatter(x + [new_x], y + [new_y], c=classes + [prediction[0]])
plt.text(x=new_x-1.7, y=new_y-0.7, s=f"new point, class: {prediction[0]}")
plt.show()
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(data, classes)
prediction = knn.predict(new_point)
print(prediction)
[1]
When we plot the class of the new point along with the older points, we note that the color has changed based on the associated class label:
plt.scatter(x + [new_x], y + [new_y], c=classes + [prediction[0]])
plt.text(x=new_x-1.7, y=new_y-0.7, s=f"new point, class: {prediction[0]}")
plt.show()
mydb.commit()
. It is required to make the
changes, otherwise no
changes are made to the table.
executemany()
method.
The second parameter of the executemany()
method
is a list of tuples, containing the data you want to insert:
Example
Fill the "customers" table with data:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "INSERT INTO customers (name,
address) VALUES (%s, %s)"
val = [
('Peter', 'Lowstreet 4'),
('Amy', 'Apple st 652'),
('Hannah', 'Mountain 21'),
('Michael', 'Valley 345'),
('Sandy', 'Ocean blvd 2'),
('Betty', 'Green Grass 1'),
('Richard', 'Sky st 331'),
('Susan', 'One way 98'),
('Vicky', 'Yellow Garden 2'),
('Ben', 'Park Lane 38'),
('William', 'Central st 954'),
('Chuck', 'Main Road 989'),
('Viola', 'Sideway 1633')
]
mycursor.executemany(sql, val)
mydb.commit()
print(mycursor.rowcount, "was inserted.")
Run example »
fetchall()
method, which fetches all rows from the last executed statement.
fetchone()
method.
The fetchone()
method will return the first row of
the result:
Example
Fetch only one row:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor =
mydb.cursor()
mycursor.execute("SELECT * FROM customers")
myresult = mycursor.fetchone()
print(myresult)
Run example »
%
to represent wildcard
characters:
Example
Select records where the address contains the word "way":
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "SELECT * FROM customers WHERE address
LIKE '%way%'"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Run example »
%s
method:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "SELECT * FROM customers WHERE
address = %s"
adr = ("Yellow Garden 2", )
mycursor.execute(sql, adr)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Run example »
mydb.commit()
. It is required to make the
changes, otherwise no
changes are made to the table.
Notice the WHERE clause in the DELETE syntax: The WHERE clause
specifies which record(s) that should be deleted. If you omit the WHERE
clause, all records will be deleted!
%s
to escape values in the delete statement:
Example
Escape values by using the placeholder %s
method:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "DELETE FROM customers WHERE address =
%s"
adr = ("Yellow Garden 2", )
mycursor.execute(sql, adr)
mydb.commit()
print(mycursor.rowcount, "record(s) deleted")
Run example »
mydb.commit()
. It is required to make the
changes, otherwise no
changes are made to the table.
Notice the WHERE clause in the UPDATE syntax: The WHERE clause
specifies which record or records that should be updated. If you omit the WHERE
clause, all records will be updated!
%s
to escape values in the delete statement:
Example
Escape values by using the placeholder %s
method:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "UPDATE customers SET address = %s
WHERE address = %s"
val = ("Valley 345", "Canyon 123")
mycursor.execute(sql,
val)
mydb.commit()
print(mycursor.rowcount, "record(s)
affected")
Run example »
fav
field and products'
id
field.
Example
Join users and products to see the name of the users favorite product:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "SELECT \
users.name AS user,
\
products.name AS favorite \
FROM users \
INNER JOIN
products ON users.fav = products.id"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Run example »
Note: You can use JOIN instead of INNER JOIN. They will
both give you the same result.
insert_one()
method.
The first parameter of the insert_one()
method is a
dictionary containing the
name(s) and value(s) of each field in the document you want to insert.
Example
Insert a record in the "customers" collection:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mydict = { "name": "John", "address": "Highway 37" }
x =
mycol.insert_one(mydict)
Run example »
insert_one()
method returns a InsertOneResult object, which has a
property, inserted_id
, that holds the id of the inserted document.
Example
Insert another record in the "customers" collection, and return the value of the
_id
field:
mydict = { "name": "Peter", "address": "Lowstreet 27" }
x = mycol.insert_one(mydict)
print(x.inserted_id)
Run example »
If you do not specify an _id
field, then MongoDB
will add one for you and assign a unique id for each document.
In the example above no _id
field was
specified, so MongoDB assigned a unique
_id for the record (document).
insert_many()
method.
The first parameter of the insert_many()
method
is a list containing dictionaries with the data you want to insert:
Example
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mylist = [
{ "name": "Amy", "address": "Apple st 652"},
{ "name": "Hannah", "address": "Mountain 21"},
{ "name":
"Michael", "address": "Valley 345"},
{ "name": "Sandy", "address":
"Ocean blvd 2"},
{ "name": "Betty", "address": "Green Grass 1"},
{ "name": "Richard", "address": "Sky st 331"},
{ "name": "Susan",
"address": "One way 98"},
{ "name": "Vicky", "address": "Yellow
Garden 2"},
{ "name": "Ben", "address": "Park Lane 38"},
{ "name": "William", "address": "Central st 954"},
{ "name":
"Chuck", "address": "Main Road 989"},
{ "name": "Viola",
"address": "Sideway 1633"}
]
x = mycol.insert_many(mylist)
#print list of the _id values of the inserted
documents:
print(x.inserted_ids)
Run example »
The insert_many()
method returns a InsertManyResult object, which has a property, inserted_ids
, that holds the ids of the inserted documents.
find()
and find_one()
methods to find data in a collection.
Just like the SELECT statement is used to find data in a
table in a MySQL database.
find_one()
method.
The find_one()
method returns the first
occurrence in the selection.
Example
Find the first document in the customers collection:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
x = mycol.find_one()
print(x)
Run example »
find()
method.
The find()
method returns all
occurrences in the selection.
The first parameter of the find()
method
is a query object. In this example we use an empty query object, which selects
all documents in the collection.
No parameters in the find() method gives you the same result as SELECT * in MySQL.
Example
Return all documents in the "customers" collection, and print each document:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
for x in mycol.find():
print(x)
Run example »
find()
method
is an object describing which fields to include in the result.
This parameter is optional, and if omitted, all fields will be included in
the result.
Example
Return only the names and addresses, not the _ids:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
for x in mycol.find({},{ "_id": 0, "name": 1, "address": 1 }):
print(x)
Run example »
You are not allowed to specify both 0 and 1 values in the same object (except
if one of the fields is the _id field). If you specify a field with the value 0, all other fields get the value 1,
and vice versa:
Example
This example will exclude "address" from the result:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
for x in mycol.find({},{ "address": 0 }):
print(x)
Run example »
Example
You get an error if you specify both 0 and 1 values in the same object
(except if one of the fields is the _id field):
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
for x in mycol.find({},{ "name": 1, "address": 0 }):
print(x)
find()
method
is a query object, and is used to limit the search.
Example
Find document(s) with the address "Park Lane 38":
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
myquery = { "address": "Park Lane 38" }
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)
Run example »
{"$gt": "S"}
:
Example
Find documents where the address starts with the letter "S" or
higher:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
myquery = { "address": { "$gt": "S" } }
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)
Run example »
{"$regex": "^S"}
:
Example
Find documents where the address starts with the letter "S":
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
myquery = { "address": { "$regex": "^S" } }
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)
Run example »
sort()
method to sort the result in
ascending or descending order.
The sort()
method takes one parameter for
"fieldname" and one parameter for "direction" (ascending is the default
direction).
Example
Sort the result alphabetically by name:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mydoc = mycol.find().sort("name")
for x in mydoc:
print(x)
Run example »
delete_one()
method.
The first parameter of the delete_one()
method
is a query object defining which document to delete.
Note: If the query finds more than one document, only the first
occurrence is deleted.
Example
Delete the document with the address "Mountain 21":
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
myquery = { "address": "Mountain 21" }
mycol.delete_one(myquery)
Run example »
delete_many()
method.
The first parameter of the delete_many()
method
is a query object defining which documents to delete.
Example
Delete all documents were the address starts with the letter S:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
myquery = { "address": {"$regex": "^S"} }
x = mycol.delete_many(myquery)
print(x.deleted_count, " documents
deleted.")
Run example »
delete_many()
method:
Example
Delete all documents in the "customers" collection:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
x = mycol.delete_many({})
print(x.deleted_count, " documents
deleted.")
Run example »
drop()
method.
Example
Delete the "customers" collection:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mycol.drop()
Run example »
The drop()
method returns true if the collection was dropped successfully,
and false if the collection does not exist.
update_one()
method.
The first parameter of the update_one()
method
is a query object defining which document to update.
Note: If the query finds more than one record, only the first
occurrence is updated.
The second parameter
is an object defining the new values of the document.
Example
Change the address from "Valley 345" to "Canyon 123":
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
myquery = { "address": "Valley 345" }
newvalues = { "$set": {
"address": "Canyon 123" } }
mycol.update_one(myquery, newvalues)
#print "customers" after the update:
for x in mycol.find():
print(x)
Run example »
update_many()
method.
Example
Update all documents where the address starts with the letter "S":
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
myquery = { "address": { "$regex": "^S" } }
newvalues = { "$set": {
"name": "Minnie" } }
x = mycol.update_many(myquery, newvalues)
print(x.modified_count, "documents updated.")
Run example »
limit()
method.
The limit()
method takes one parameter, a number defining how many documents
to return.
Consider you have a "customers" collection:
Function | Description |
---|---|
abs() | Returns the absolute value of a number |
all() | Returns True if all items in an iterable object are true |
any() | Returns True if any item in an iterable object is true |
ascii() | Returns a readable version of an object. Replaces none-ascii characters with escape character |
bin() | Returns the binary version of a number |
bool() | Returns the boolean value of the specified object |
bytearray() | Returns an array of bytes |
bytes() | Returns a bytes object |
callable() | Returns True if the specified object is callable, otherwise False |
chr() | Returns a character from the specified Unicode code. |
classmethod() | Converts a method into a class method |
compile() | Returns the specified source as an object, ready to be executed |
complex() | Returns a complex number |
delattr() | Deletes the specified attribute (property or method) from the specified object |
dict() | Returns a dictionary (Array) |
dir() | Returns a list of the specified object's properties and methods |
divmod() | Returns the quotient and the remainder when argument1 is divided by argument2 |
enumerate() | Takes a collection (e.g. a tuple) and returns it as an enumerate object |
eval() | Evaluates and executes an expression |
exec() | Executes the specified code (or object) |
filter() | Use a filter function to exclude items in an iterable object |
float() | Returns a floating point number |
format() | Formats a specified value |
frozenset() | Returns a frozenset object |
getattr() | Returns the value of the specified attribute (property or method) |
globals() | Returns the current global symbol table as a dictionary |
hasattr() | Returns True if the specified object has the specified attribute (property/method) |
hash() | Returns the hash value of a specified object |
help() | Executes the built-in help system |
hex() | Converts a number into a hexadecimal value |
id() | Returns the id of an object |
input() | Allowing user input |
int() | Returns an integer number |
isinstance() | Returns True if a specified object is an instance of a specified object |
issubclass() | Returns True if a specified class is a subclass of a specified object |
iter() | Returns an iterator object |
len() | Returns the length of an object |
list() | Returns a list |
locals() | Returns an updated dictionary of the current local symbol table |
map() | Returns the specified iterator with the specified function applied to each item |
max() | Returns the largest item in an iterable |
memoryview() | Returns a memory view object |
min() | Returns the smallest item in an iterable |
next() | Returns the next item in an iterable |
object() | Returns a new object |
oct() | Converts a number into an octal |
open() | Opens a file and returns a file object |
ord() | Convert an integer representing the Unicode of the specified character |
pow() | Returns the value of x to the power of y |
print() | Prints to the standard output device |
property() | Gets, sets, deletes a property |
range() | Returns a sequence of numbers, starting from 0 and increments by 1 (by default) |
repr() | Returns a readable version of an object |
reversed() | Returns a reversed iterator |
round() | Rounds a numbers |
set() | Returns a new set object |
setattr() | Sets an attribute (property/method) of an object |
slice() | Returns a slice object |
sorted() | Returns a sorted list |
staticmethod() | Converts a method into a static method |
str() | Returns a string object |
sum() | Sums the items of an iterator |
super() | Returns an object that represents the parent class |
tuple() | Returns a tuple |
type() | Returns the type of an object |
vars() | Returns the __dict__ property of an object |
zip() | Returns an iterator, from two or more iterators |
Method | Description |
---|---|
capitalize() | Converts the first character to upper case |
casefold() | Converts string into lower case |
center() | Returns a centered string |
count() | Returns the number of times a specified value occurs in a string |
encode() | Returns an encoded version of the string |
endswith() | Returns true if the string ends with the specified value |
expandtabs() | Sets the tab size of the string |
find() | Searches the string for a specified value and returns the position of where it was found |
format() | Formats specified values in a string |
format_map() | Formats specified values in a string |
index() | Searches the string for a specified value and returns the position of where it was found |
isalnum() | Returns True if all characters in the string are alphanumeric |
isalpha() | Returns True if all characters in the string are in the alphabet |
isascii() | Returns True if all characters in the string are ascii characters |
isdecimal() | Returns True if all characters in the string are decimals |
isdigit() | Returns True if all characters in the string are digits |
isidentifier() | Returns True if the string is an identifier |
islower() | Returns True if all characters in the string are lower case |
isnumeric() | Returns True if all characters in the string are numeric |
isprintable() | Returns True if all characters in the string are printable |
isspace() | Returns True if all characters in the string are whitespaces |
istitle() | Returns True if the string follows the rules of a title |
isupper() | Returns True if all characters in the string are upper case |
join() | Converts the elements of an iterable into a string |
ljust() | Returns a left justified version of the string |
lower() | Converts a string into lower case |
lstrip() | Returns a left trim version of the string |
maketrans() | Returns a translation table to be used in translations |
partition() | Returns a tuple where the string is parted into three parts |
replace() | Returns a string where a specified value is replaced with a specified value |
rfind() | Searches the string for a specified value and returns the last position of where it was found |
rindex() | Searches the string for a specified value and returns the last position of where it was found |
rjust() | Returns a right justified version of the string |
rpartition() | Returns a tuple where the string is parted into three parts |
rsplit() | Splits the string at the specified separator, and returns a list |
rstrip() | Returns a right trim version of the string |
split() | Splits the string at the specified separator, and returns a list |
splitlines() | Splits the string at line breaks and returns a list |
startswith() | Returns true if the string starts with the specified value |
strip() | Returns a trimmed version of the string |
swapcase() | Swaps cases, lower case becomes upper case and vice versa |
title() | Converts the first character of each word to upper case |
translate() | Returns a translated string |
upper() | Converts a string into upper case |
zfill() | Fills the string with a specified number of 0 values at the beginning |
Method | Description |
---|---|
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the first item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
Method | Description |
---|---|
clear() | Removes all the elements from the dictionary |
copy() | Returns a copy of the dictionary |
fromkeys() | Returns a dictionary with the specified keys and value |
get() | Returns the value of the specified key |
items() | Returns a list containing a tuple for each key value pair |
keys() | Returns a list containing the dictionary's keys |
pop() | Removes the element with the specified key |
popitem() | Removes the last inserted key-value pair |
setdefault() | Returns the value of the specified key. If the key does not exist: insert the key, with the specified value |
update() | Updates the dictionary with the specified key-value pairs |
values() | Returns a list of all the values in the dictionary |
Method | Description |
---|---|
count() | Returns the number of times a specified value occurs in a tuple |
index() | Searches the tuple for a specified value and returns the position of where it was found |
Method | Description |
---|---|
add() | Adds an element to the set |
clear() | Removes all the elements from the set |
copy() | Returns a copy of the set |
difference() | Returns a set containing the difference between two or more sets |
difference_update() | Removes the items in this set that are also included in another, specified set |
discard() | Remove the specified item |
intersection() | Returns a set, that is the intersection of two or more sets |
intersection_update() | Removes the items in this set that are not present in other, specified set(s) |
isdisjoint() | Returns whether two sets have a intersection or not |
issubset() | Returns whether another set contains this set or not |
issuperset() | Returns whether this set contains another set or not |
pop() | Removes an element from the set |
remove() | Removes the specified element |
symmetric_difference() | Returns a set with the symmetric differences of two sets |
symmetric_difference_update() | inserts the symmetric differences from this set and another |
union() | Return a set containing the union of sets |
update() | Update the set with another set, or any other iterable |
Method | Description |
---|---|
close() | Closes the file |
detach() | Returns the separated raw stream from the buffer |
fileno() | Returns a number that represents the stream, from the operating system's perspective |
flush() | Flushes the internal buffer |
isatty() | Returns whether the file stream is interactive or not |
read() | Returns the file content |
readable() | Returns whether the file stream can be read or not |
readline() | Returns one line from the file |
readlines() | Returns a list of lines from the file |
seek() | Change the file position |
seekable() | Returns whether the file allows us to change the file position |
tell() | Returns the current file position |
truncate() | Resizes the file to a specified size |
writable() | Returns whether the file can be written to or not |
write() | Writes the specified string to the file |
writelines() | Writes a list of strings to the file |
Keyword | Description |
---|---|
and | A logical operator |
as | To create an alias |
assert | For debugging |
break | To break out of a loop |
class | To define a class |
continue | To continue to the next iteration of a loop |
def | To define a function |
del | To delete an object |
elif | Used in conditional statements, same as else if |
else | Used in conditional statements |
except | Used with exceptions, what to do when an exception occurs |
False | Boolean value, result of comparison operations |
finally | Used with exceptions, a block of code that will be executed no matter if there is an exception or not |
for | To create a for loop |
from | To import specific parts of a module |
global | To declare a global variable |
if | To make a conditional statement |
import | To import a module |
in | To check if a value is present in a list, tuple, etc. |
is | To test if two variables are equal |
lambda | To create an anonymous function |
None | Represents a null value |
nonlocal | To declare a non-local variable |
not | A logical operator |
or | A logical operator |
pass | A null statement, a statement that will do nothing |
raise | To raise an exception |
return | To exit a function and return a value |
True | Boolean value, result of comparison operations |
try | To make a try...except statement |
while | To create a while loop |
with | Used to simplify exception handling |
yield | To end a function, returns a generator |
Exception | Description |
---|---|
ArithmeticError | Raised when an error occurs in numeric calculations |
AssertionError | Raised when an assert statement fails |
AttributeError | Raised when attribute reference or assignment fails |
Exception | Base class for all exceptions |
EOFError | Raised when the input() method hits an "end of file" condition (EOF) |
FloatingPointError | Raised when a floating point calculation fails |
GeneratorExit | Raised when a generator is closed (with the close() method) |
ImportError | Raised when an imported module does not exist |
IndentationError | Raised when indendation is not correct |
IndexError | Raised when an index of a sequence does not exist |
KeyError | Raised when a key does not exist in a dictionary |
KeyboardInterrupt | Raised when the user presses Ctrl+c, Ctrl+z or Delete |
LookupError | Raised when errors raised cant be found |
MemoryError | Raised when a program runs out of memory |
NameError | Raised when a variable does not exist |
NotImplementedError | Raised when an abstract method requires an inherited class to override the method |
OSError | Raised when a system related operation causes an error |
OverflowError | Raised when the result of a numeric calculation is too large |
ReferenceError | Raised when a weak reference object does not exist |
RuntimeError | Raised when an error occurs that do not belong to any specific expections |
StopIteration | Raised when the next() method of an iterator has no further values |
SyntaxError | Raised when a syntax error occurs |
TabError | Raised when indentation consists of tabs or spaces |
SystemError | Raised when a system error occurs |
SystemExit | Raised when the sys.exit() function is called |
TypeError | Raised when two different types are combined |
UnboundLocalError | Raised when a local variable is referenced before assignment |
UnicodeError | Raised when a unicode problem occurs |
UnicodeEncodeError | Raised when a unicode encoding problem occurs |
UnicodeDecodeError | Raised when a unicode decoding problem occurs |
UnicodeTranslateError | Raised when a unicode translation problem occurs |
ValueError | Raised when there is a wrong value in a specified data type |
ZeroDivisionError | Raised when the second operator in a division is zero |
Feature | Description |
---|---|
Indentation | Indentation refers to the spaces at the beginning of a code line |
Comments | Comments are code lines that will not be executed |
Multi Line Comments | How to insert comments on multiple lines |
Creating Variables | Variables are containers for storing data values |
Variable Names | How to name your variables |
Assign Values to Multiple Variables | How to assign values to multiple variables |
Output Variables | Use the print statement to output variables |
String Concatenation | How to combine strings |
Global Variables | Global variables are variables that belongs to the global scope |
Built-In Data Types | Python has a set of built-in data types |
Getting Data Type | How to get the data type of an object |
Setting Data Type | How to set the data type of an object |
Numbers | There are three numeric types in Python |
Int | The integer number type |
Float | The floating number type |
Complex | The complex number type |
Type Conversion | How to convert from one number type to another |
Random Number | How to create a random number |
Specify a Variable Type | How to specify a certain data type for a variable |
String Literals | How to create string literals |
Assigning a String to a Variable | How to assign a string value to a variable |
Multiline Strings | How to create a multi line string |
Strings are Arrays | Strings in Python are arrays of bytes representing Unicode characters |
Slicing a String | How to slice a string |
Negative Indexing on a String | How to use negative indexing when accessing a string |
String Length | How to get the length of a string |
Check In String | How to check if a string contains a specified phrase |
Format String | How to combine two strings |
Escape Characters | How to use escape characters |
Boolean Values | True or False |
Evaluate Booleans | Evaluate a value or statement and return either True or False |
Return Boolean Value | Functions that return a Boolean value |
Operators | Use operator to perform operations in Python |
Arithmetic Operators | Arithmetic operator are used to perform common mathematical operations |
Assignment Operators | Assignment operators are use to assign values to variables |
Comparison Operators | Comparison operators are used to compare two values |
Logical Operators | Logical operators are used to combine conditional statements |
Identity Operators | Identity operators are used to see if two objects are in fact the same object |
Membership Operators | Membership operators are used to test is a sequence is present in an object |
Bitwise Operators | Bitwise operators are used to compare (binary) numbers |
Lists | A list is an ordered, and changeable, collection |
Access List Items | How to access items in a list |
Change List Item | How to change the value of a list item |
Loop Through List Items | How to loop through the items in a list |
List Comprehension | How use a list comprehensive |
Check if List Item Exists | How to check if a specified item is present in a list |
List Length | How to determine the length of a list |
Add List Items | How to add items to a list |
Remove List Items | How to remove list items |
Copy a List | How to copy a list |
Join Two Lists | How to join two lists |
Tuple | A tuple is an ordered, and unchangeable, collection |
Access Tuple Items | How to access items in a tuple |
Change Tuple Item | How to change the value of a tuple item |
Loop List Items | How to loop through the items in a tuple |
Check if Tuple Item Exists | How to check if a specified item is present in a tuple |
Tuple Length | How to determine the length of a tuple |
Tuple With One Item | How to create a tuple with only one item |
Remove Tuple Items | How to remove tuple items |
Join Two Tuples | How to join two tuples |
Set | A set is an unordered, and unchangeable, collection |
Access Set Items | How to access items in a set |
Add Set Items | How to add items to a set |
Loop Set Items | How to loop through the items in a set |
Check if Set Item Exists | How to check if a item exists |
Set Length | How to determine the length of a set |
Remove Set Items | How to remove set items |
Join Two Sets | How to join two sets |
Dictionary | A dictionary is an unordered, and changeable, collection |
Access Dictionary Items | How to access items in a dictionary |
Change Dictionary Item | How to change the value of a dictionary item |
Loop Dictionary Items | How to loop through the items in a tuple |
Check if Dictionary Item Exists | How to check if a specified item is present in a dictionary |
Dictionary Length | How to determine the length of a dictionary |
Add Dictionary Item | How to add an item to a dictionary |
Remove Dictionary Items | How to remove dictionary items |
Copy Dictionary | How to copy a dictionary |
Nested Dictionaries | A dictionary within a dictionary |
If Statement | How to write an if statement |
If Indentation | If statemnts in Python relies on indentation (whitespace at the beginning of a line) |
Elif | elif is the same as "else if" in other programming languages |
Else | How to write an if...else statement |
Shorthand If | How to write an if statement in one line |
Shorthand If Else | How to write an if...else statement in one line |
If AND | Use the and keyword to combine if statements |
If OR | Use the or keyword to combine if statements |
Nested If | How to write an if statement inside an if statement |
The pass Keyword in If | Use the pass keyword inside empty if statements |
While | How to write a while loop |
While Break | How to break a while loop |
While Continue | How to stop the current iteration and continue wit the next |
While Else | How to use an else statement in a while loop |
For | How to write a for loop |
Loop Through a String | How to loop through a string |
For Break | How to break a for loop |
For Continue | How to stop the current iteration and continue wit the next |
Looping Through a rangee | How to loop through a range of values |
For Else | How to use an else statement in a for loop |
Nested Loops | How to write a loop inside a loop |
For pass | Use the pass keyword inside empty for loops |
Function | How to create a function in Python |
Call a Function | How to call a function in Python |
Function Arguments | How to use arguments in a function |
*args | To deal with an unknown number of arguments in a function, use the * symbol before the parameter name |
Keyword Arguments | How to use keyword arguments in a function |
**kwargs | To deal with an unknown number of keyword arguments in a function, use the * symbol before the parameter name |
Default Parameter Value | How to use a default parameter value |
Passing a List as an Argument | How to pass a list as an argument |
Function Return Value | How to return a value from a function |
The pass Statement i Functions | Use the pass statement in empty functions |
Function Recursion | Functions that can call itself is called recursive functions |
Lambda Function | How to create anonymous functions in Python |
Why Use Lambda Functions | Learn when to use a lambda function or not |
Array | Lists can be used as Arrays |
What is an Array | Arrays are variables that can hold more than one value |
Access Arrays | How to access array items |
Array Length | How to get the length of an array |
Looping Array Elements | How to loop through array elements |
Add Array Element | How to add elements from an array |
Remove Array Element | How to remove elements from an array |
Array Methods | Python has a set of Array/Lists methods |
Class | A class is like an object constructor |
Create Class | How to create a class |
The Class __init__() Function | The __init__() function is executed when the class is initiated |
Object Methods | Methods in objects are functions that belongs to the object |
self | The self parameter refers to the current instance of the class |
Modify Object Properties | How to modify properties of an object |
Delete Object Properties | How to modify properties of an object |
Delete Object | How to delete an object |
Class pass Statement | Use the pass statement in empty classes |
Create Parent Class | How to create a parent class |
Create Child Class | How to create a child class |
Create the __init__() Function | How to create the __init__() function |
super Function | The super() function make the child class inherit the parent class |
Add Class Properties | How to add a property to a class |
Add Class Methods | How to add a method to a class |
Iterators | An iterator is an object that contains a countable number of values |
Iterator vs Iterable | What is the difference between an iterator and an iterable |
Loop Through an Iterator | How to loop through the elements of an iterator |
Create an Iterator | How to create an iterator |
StopIteration | How to stop an iterator |
Global Scope | When does a variable belong to the global scope? |
Global Keyword | The global keyword makes the variable global |
Create a Module | How to create a module |
Variables in Modules | How to use variables in a module |
Renaming a Module | How to rename a module |
Built-in Modules | How to import built-in modules |
Using the dir() Function | List all variable names and function names in a module |
Import From Module | How to import only parts from a module |
Datetime Module | How to work with dates in Python |
Date Output | How to output a date |
Create a Date Object | How to create a date object |
The strftime Method | How to format a date object into a readable string |
Date Format Codes | The datetime module has a set of legal format codes |
JSON | How to work with JSON in Python |
Parse JSON | How to parse JSON code in Python |
Convert into JSON | How to convert a Python object in to JSON |
Format JSON | How to format JSON output with indentations and line breaks |
Sort JSON | How to sort JSON |
RegEx Module | How to import the regex module |
RegEx Functions | The re module has a set of functions |
Metacharacters in RegEx | Metacharacters are characters with a special meaning |
RegEx Special Sequences | A backslash followed by a a character has a special meaning |
RegEx Sets | A set is a set of characters inside a pair of square brackets with a special meaning |
RegEx Match Object | The Match Object is an object containing information about the search and the result |
Install PIP | How to install PIP |
PIP Packages | How to download and install a package with PIP |
PIP Remove Package | How to remove a package with PIP |
Error Handling | How to handle errors in Python |
Handle Many Exceptions | How to handle more than one exception |
Try Else | How to use the else keyword in a try statement |
Try Finally | How to use the finally keyword in a try statement |
raise | How to raise an exception in Python |
random
module has a set of methods:
Method | Description |
---|---|
seed() | Initialize the random number generator |
getstate() | Returns the current internal state of the random number generator |
setstate() | Restores the internal state of the random number generator |
getrandbits() | Returns a number representing the random bits |
randrange() | Returns a random number between the given range |
randint() | Returns a random number between the given range |
choice() | Returns a random element from the given sequence |
choices() | Returns a list with a random selection from the given sequence |
shuffle() | Takes a sequence and returns the sequence in a random order |
sample() | Returns a given sample of a sequence |
random() | Returns a random float number between 0 and 1 |
uniform() | Returns a random float number between two given parameters |
triangular() | Returns a random float number between two given parameters, you can also set a mode parameter to specify the midpoint between the two other parameters |
betavariate() | Returns a random float number between 0 and 1 based on the Beta distribution (used in statistics) |
expovariate() | Returns a random float number based on the Exponential distribution (used in statistics) |
gammavariate() | Returns a random float number based on the Gamma distribution (used in statistics) |
gauss() | Returns a random float number based on the Gaussian distribution (used in probability theories) |
lognormvariate() | Returns a random float number based on a log-normal distribution (used in probability theories) |
normalvariate() | Returns a random float number based on the normal distribution (used in probability theories) |
vonmisesvariate() | Returns a random float number based on the von Mises distribution (used in directional statistics) |
paretovariate() | Returns a random float number based on the Pareto distribution (used in probability theories) |
weibullvariate() | Returns a random float number based on the Weibull distribution (used in statistics) |
requests
module allows you to send HTTP
requests using Python.
The HTTP request returns a Response Object with all the response data
(content, encoding, status, etc).
Method | Description |
---|---|
delete(url, args) | Sends a DELETE request to the specified url |
get(url, params, args) | Sends a GET request to the specified url |
head(url, args) | Sends a HEAD request to the specified url |
patch(url, data, args) | Sends a PATCH request to the specified url |
post(url, data, json, args) | Sends a POST request to the specified url |
put(url, data, args) | Sends a PUT request to the specified url |
request(method, url, args) | Sends a request of the specified method to the specified url |
statistics
module was new in Python 3.4.
Method | Description |
---|---|
statistics.harmonic_mean() | Calculates the harmonic mean (central location) of the given data |
statistics.mean() | Calculates the mean (average) of the given data |
statistics.median() | Calculates the median (middle value) of the given data |
statistics.median_grouped() | Calculates the median of grouped continuous data |
statistics.median_high() | Calculates the high median of the given data |
statistics.median_low() | Calculates the low median of the given data |
statistics.mode() | Calculates the mode (central tendency) of the given numeric or nominal data |
statistics.pstdev() | Calculates the standard deviation from an entire population |
statistics.stdev() | Calculates the standard deviation from a sample of data |
statistics.pvariance() | Calculates the variance of an entire population |
statistics.variance() | Calculates the variance from a sample of data |
math
module has a set of methods and constants.
Method | Description |
---|---|
math.acos() | Returns the arc cosine of a number |
math.acosh() | Returns the inverse hyperbolic cosine of a number |
math.asin() | Returns the arc sine of a number |
math.asinh() | Returns the inverse hyperbolic sine of a number |
math.atan() | Returns the arc tangent of a number in radians |
math.atan2() | Returns the arc tangent of y/x in radians |
math.atanh() | Returns the inverse hyperbolic tangent of a number |
math.ceil() | Rounds a number up to the nearest integer |
math.comb() | Returns the number of ways to choose k items from n items without repetition and order |
math.copysign() | Returns a float consisting of the value of the first parameter and the sign of the second parameter |
math.cos() | Returns the cosine of a number |
math.cosh() | Returns the hyperbolic cosine of a number |
math.degrees() | Converts an angle from radians to degrees |
math.dist() | Returns the Euclidean distance between two points (p and q), where p and q are the coordinates of that point |
math.erf() | Returns the error function of a number |
math.erfc() | Returns the complementary error function of a number |
math.exp() | Returns E raised to the power of x |
math.expm1() | Returns Ex - 1 |
math.fabs() | Returns the absolute value of a number |
math.factorial() | Returns the factorial of a number |
math.floor() | Rounds a number down to the nearest integer |
math.fmod() | Returns the remainder of x/y |
math.frexp() | Returns the mantissa and the exponent, of a specified number |
math.fsum() | Returns the sum of all items in any iterable (tuples, arrays, lists, etc.) |
math.gamma() | Returns the gamma function at x |
math.gcd() | Returns the greatest common divisor of two integers |
math.hypot() | Returns the Euclidean norm |
math.isclose() | Checks whether two values are close to each other, or not |
math.isfinite() | Checks whether a number is finite or not |
math.isinf() | Checks whether a number is infinite or not |
math.isnan() | Checks whether a value is NaN (not a number) or not |
math.isqrt() | Rounds a square root number downwards to the nearest integer |
math.ldexp() | Returns the inverse of math.frexp() which is x * (2**i) of the given numbers x and i |
math.lgamma() | Returns the log gamma value of x |
math.log() | Returns the natural logarithm of a number, or the logarithm of number to base |
math.log10() | Returns the base-10 logarithm of x |
math.log1p() | Returns the natural logarithm of 1+x |
math.log2() | Returns the base-2 logarithm of x |
math.perm() | Returns the number of ways to choose k items from n items with order and without repetition |
math.pow() | Returns the value of x to the power of y |
math.prod() | Returns the product of all the elements in an iterable |
math.radians() | Converts a degree value into radians |
math.remainder() | Returns the closest value that can make numerator completely divisible by the denominator |
math.sin() | Returns the sine of a number |
math.sinh() | Returns the hyperbolic sine of a number |
math.sqrt() | Returns the square root of a number |
math.tan() | Returns the tangent of a number |
math.tanh() | Returns the hyperbolic tangent of a number |
math.trunc() | Returns the truncated integer parts of a number |
Constant | Description |
---|---|
math.e | Returns Euler's number (2.7182...) |
math.inf | Returns a floating-point positive infinity |
math.nan | Returns a floating-point NaN (Not a Number) value |
math.pi | Returns PI (3.1415...) |
math.tau | Returns tau (6.2831...) |
int
, float
, and complex
numbers. It even accepts Python objects that has a __complex__()
or __float__()
method.
The methods in this module almost always return a complex number. If the return
value can be expressed as a real number, the return value has an imaginary part
of 0.
The cmath
module has a set of methods and constants.
Method | Description |
---|---|
cmath.acos(x) | Returns the arc cosine value of x |
cmath.acosh(x) | Returns the hyperbolic arc cosine of x |
cmath.asin(x) | Returns the arc sine of x |
cmath.asinh(x) | Returns the hyperbolic arc sine of x |
cmath.atan(x) | Returns the arc tangent value of x |
cmath.atanh(x) | Returns the hyperbolic arctangent value of x |
cmath.cos(x) | Returns the cosine of x |
cmath.cosh(x) | Returns the hyperbolic cosine of x |
cmath.exp(x) | Returns the value of Ex, where E is Euler's number (approximately 2.718281...), and x is the number passed to it |
cmath.isclose() | Checks whether two values are close, or not |
cmath.isfinite(x) | Checks whether x is a finite number |
cmath.isinf(x) | Check whether x is a positive or negative infinty |
cmath.isnan(x) | Checks whether x is NaN (not a number) |
cmath.log(x[, base]) | Returns the logarithm of x to the base |
cmath.log10(x) | Returns the base-10 logarithm of x |
cmath.phase() | Return the phase of a complex number |
cmath.polar() | Convert a complex number to polar coordinates |
cmath.rect() | Convert polar coordinates to rectangular form |
cmath.sin(x) | Returns the sine of x |
cmath.sinh(x) | Returns the hyperbolic sine of x |
cmath.sqrt(x) | Returns the square root of x |
cmath.tan(x) | Returns the tangent of x |
cmath.tanh(x) | Returns the hyperbolic tangent of x |
Constant | Description |
---|---|
cmath.e | Returns Euler's number (2.7182...) |
cmath.inf | Returns a floating-point positive infinity value |
cmath.infj | Returns a complex infinity value |
cmath.nan | Returns floating-point NaN (Not a Number) value |
cmath.nanj | Returns coplext NaN (Not a Number) value |
cmath.pi | Returns PI (3.1415...) |
cmath.tau | Returns tau (6.2831...) |
-1
.
Example
Reverse the string "Hello World":
txt = "Hello World"[::-1]
print(txt)
[::-1]
means start at
the end of the string and end at position 0, move with the
step -1
, negative one, which means one step backwards.
txt
that reads "Hello
World" backwards.
Print the String to demonstrate the result
+
operator to add two numbers:
Example
x = 5
y = 10
print(x + y)
Hello, World!
Python is awesome
Icon | Description |
---|---|
Go to www.w3schools.com | |
Menu button for more options | |
Change orientation (horizontally or vertically) | |
Change color theme (dark or light) |