Python Examples
Python Syntax
Python Variables
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
Python Casting
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
Python Operators
Addition operator Subtraction operator Multiplication operator Division operator Modulus operator Assignment operator
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
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
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
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
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
Python While Loop
The while loop Using the break statement in a while loop Using the continue statement in a while loop
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
Python Functions
Create and call a function Function parameters Default parameter value Let a function return a value Recursion
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
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
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
Python Iterators
Return an iterator from a tuple Return an iterator from a string Loop through an iterator Create an iterator Stop iteration
Python Modules
Use a module Variables in module Re-naming a module Built-in modules Using the dir() function Import from module
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
Python Math
Find the lowest and highest value in an iterable Return the absolute value of a number Return the value of x to the power of y (xy) Return the square root of a number Round a number upwards and downwards to its nearest integer Return the value of PI
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
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
Python PIP
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
Python File Handling
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
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
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