<!--
main_leaderboard, all: [728,90][970,90][320,50][468,60]
-->
Python Examples
❮ Previous
Next ❯
Python Syntax
Print "Hello World"
Comments in Python
Docstrings
Syntax Explained
Python Variables
Create a variable
Output both text and a variable
Add a variable to another variable
Variables Explained
Python Numbers
Verify the type of an object
Create integers
Create floating point numbers
Create scientific numbers with an "e" to indicate the power of 10
Create complex numbers
Numbers Explained
Python Casting
Casting - Integers
Casting - Floats
Casting - Strings
Casting Explained
<!--
mid_content, all: [300,250][336,280][728,90][970,250][970,90][320,50][468,60]
-->
Python Strings
Get the character at position 1 of a string
Substring. Get the characters from position 2 to position 5 (not included)
Remove whitespace from the beginning or at the end of a string
Return the length of a string
Convert a string to lower case
Convert a string to upper case
Replace a string with another string
Split a string into substrings
Strings Explained
Python Operators
Addition operator
Subtraction operator
Multiplication operator
Division operator
Modulus operator
Assignment operator
Operators Explained
Python Lists
Create a list
Access list items
Change the value of a list item
Loop through a list
Check if a list item exists
Get the length of a list
Add an item to the end of a list
Add an item at a specified index
Remove an item
Remove the last item
Remove an item at a specified index
Empty a list
Using the list() constructor to make a list
Lists Explained
Python Tuples
Create a tuple
Access tuple items
Change tuple values
Loop through a tuple
Check if a tuple item exists
Get the length of a tuple
Delete a tuple
Using the tuple() constructor to create a tuple
Tuples Explained
Python Sets
Create a set
Loop through a set
Check if an item exists
Add an item to a set
Add multiple items to a set
Get the length of a set
Remove an item in a set
Remove an item in a set by using the discard() method
Remove the last item in a set by using the pop() method
Empty a set
Delete a set
Using the set() constructor to create a set
Sets Explained
Python Dictionaries
Create a dictionary
Access the items of a dictionary
Change the value of a specific item in a dictionary
Print all key names in a dictionary, one by one
Print all values in a dictionary, one by one
Using the values() function to return values of a dictionary
Loop through both keys an values, by using the items() function
Check if a key exists
Get the length of a dictionary
Add an item to a dictionary
Remove an item from a dictionary
Empty a dictionary
Using the dict() constructor to create a dictionary
Dictionaries Explained
Python If ... Else
The if statement
The elif statement
The else statement
Short hand if
Short hand if ... else
The and keyword
The or keyword
If ... Else Explained
Python While Loop
The while loop
Using the break statement in a while loop
Using the continue statement in a while loop
While Loop Explained
Python For Loop
The for loop
Loop through a string
Using the break statement in a for loop
Using the continue statement in a for loop
Using the range() function in a for loop
Else in for loop
Nested for loop
For Loop Explained
Python Functions
Create and call a function
Function parameters
Default parameter value
Let a function return a valuen
Recursion
Functions Explained
Python Lambda
A lambda function that adds 10 to the number passed in as an argument
A lambda function that multiplies argument a with argument b
A lambda function that sums argument a, b, and c
Lambda Explained
Python Arrays
Create an array
Access the elements of an array
Change the value of an array element
Get the length of an array
Loop through all elements of an array
Add an element to an array
Remove an element from an array
Arrays Explained
Python Classes and Objects
Create a class
Create an object
The __init__() Function
Create object methods
The self parameter
Modify object properties
Delete object properties
Delete an object
Classes/Objects Explained
Python Iterators
Return an iterator from a tuple
Return an iterator from a string
Loop through an iterator
Create an iterator
Stop iteration
Iterators Explained
Python Modules
Use a module
Variables in module
Re-naming a module
Built-in modules
Using the dir() function
Import from module
Modules Explained
Python Dates
Import the datetime module and display the current date
Return the year and name of weekday
Create a date object
The strftime() Method
Dates Explained
Python JSON
Convert from JSON to Python
Convert from Python to JSON
Convert Python objects into JSON strings
Convert a Python object containing all the legal data types
Use the indent parameter to define the numbers of indents
Use the separators parameter to change the default separator
Use the sort_keys parameter to specify if the result should be sorted or not
JSON Explained
Python RegEx
Search a string to see if it starts with "The" and ends with "Spain"
Using the findall() function
Using the search() function
Using the split() function
Using the sub() function
RegEx Explained
Python PIP
Using a package
PIP Explained
Python Try Except
When an error occurs, print a message
Many exceptions
Use the else keyword to define a block of code to be executed if no errors were raised
Use the finally block to execute code regardless if the try block raises an error or not
Try Except Explained
Python Files
Read a file
Read only parts of a file
Read one line of a file
Loop through the lines of a file to read the whole file, line by line
Files Explained
Python MySQL
Create a connection to a database
Create a database in MySQL
Check if a database exist
Create a table
Check if a table exist
Create primary key when creating a table
Insert a record in a table
Insert multiple rows
Get inserted ID
Select all records from a table
Select only some of the columns in a table
Use the fetchone() method to fetch only one row in a table
Select with a filter
Wildcards characters
Prevent SQL injection
Sort the result of a table alphabetically
Sort the result in a descending order (reverse alphabetically)
Delete records from an existing table
Prevent SQL injection
Delete an existing table
Delete a table if it exist
Update existing records in a table
Prevent SQL injection
Limit the number of records returned from a query
Combine rows from two or more tables, based on a related column between them
LEFT JOIN
RIGHT JOIN
MySQL Explained
Python MongoDB
Create a database
Check if a database exist
Create a collection
Check if a collection exist
Insert into collection
Return the id field
Insert multiple documents
Insert multiple documents with specified IDs
Find the first document in the selection
Find all documents in the selection
Find only some fields
Filter the result
Advanced query
Filter with regular expressions
Sort the result alphabetically
Sort the result descending (reverse alphabetically)
Delete document
Delete many documents
Delete all documents in a collection
Delete a collection
Update a document
Update many/all documents
Limit the result
MongoDB Explained
W3Schools' Online Certification
The perfect solution for professionals who need to balance work, family, and career building.
More than 25 000 certificates already issued!
Get Your Certificate »
The HTML Certificate documents your knowledge of HTML.
The CSS Certificate documents your knowledge of advanced CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The jQuery Certificate documents your knowledge of jQuery.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The Bootstrap Certificate documents your knowledge of the Bootstrap framework.
❮ Previous
Next ❯