PythonExamples
Python Syntax
Python Variables
Python Numbers
Verify the type of an objectCreate integersCreate floating point numbersCreate scientific numbers with an "e" to indicate the power of 10Create complex numbers
Python Casting
Python Strings
Get the character at position 1 of a stringSubstring. Get the characters from position 2 to position 5 (not included)Remove whitespace from the beginning or at the end of a stringReturn the length of a stringConvert a string to lower caseConvert a string to upper caseReplace a string with another stringSplit a string into substrings
Python Operators
Addition operatorSubtraction operatorMultiplication operatorDivision operatorModulus operatorAssignment operator
Python Lists
Create a listAccess list itemsChange the value of a list itemLoop through a listCheck if a list item existsGet the length of a listAdd an item to the end of a listAdd an item at a specified indexRemove an itemRemove the last itemRemove an item at a specified indexEmpty a listUsing the list() constructor to make a list
Python Tuples
Create a tupleAccess tuple itemsChange tuple valuesLoop through a tupleCheck if a tuple item existsGet the length of a tupleDelete a tupleUsing the tuple() constructor to create a tuple
Python Sets
Create a setLoop through a setCheck if an item existsAdd an item to a setAdd multiple items to a setGet the length of a setRemove an item in a setRemove an item in a set by using the discard() methodRemove the last item in a set by using the pop() methodEmpty a setDelete a setUsing the set() constructor to create a set
Python Dictionaries
Create a dictionaryAccess the items of a dictionaryChange the value of a specific item in a dictionaryPrint all key names in a dictionary, one by onePrint all values in a dictionary, one by oneUsing the values() function to return values of a dictionaryLoop through both keys an values, by using the items() functionCheck if a key existsGet the length of a dictionaryAdd an item to a dictionaryRemove an item from a dictionaryEmpty a dictionaryUsing the dict() constructor to create a dictionary
Python If ... Else
The if statementThe elif statementThe else statementShort hand ifShort hand if ... elseThe and keywordThe or keyword
Python While Loop
Python For Loop
The for loopLoop through a stringUsing the break statement in a for loopUsing the continue statement in a for loopUsing the range() function in a for loopElse in for loopNested for loop
Python Functions
Create and call a functionFunction parametersDefault parameter valueLet a function return a valueRecursion
Python Lambda
A lambda function that adds 10 to the number passed in as an argumentA lambda function that multiplies argument a with argument bA lambda function that sums argument a, b, and c
Python Arrays
Create an arrayAccess the elements of an arrayChange the value of an array elementGet the length of an arrayLoop through all elements of an arrayAdd an element to an arrayRemove an element from an array
Python Classes and Objects
Create a classCreate an objectThe __init__() FunctionCreate object methodsThe self parameterModify object propertiesDelete object propertiesDelete an object
Python Iterators
Return an iterator from a tupleReturn an iterator from a stringLoop through an iteratorCreate an iteratorStop iteration
Python Modules
Use a moduleVariables in moduleRe-naming a moduleBuilt-in modulesUsing the dir() functionImport from module
Python Dates
Import the datetime module and display the current dateReturn the year and name of weekdayCreate a date objectThe strftime() Method
Python Math
Find the lowest and highest value in an iterableReturn the absolute value of a numberReturn the value of x to the power of y (xy)Return the square root of a numberRound a number upwards and downwards to its nearest integerReturn the value of PI
Python JSON
Convert from JSON to PythonConvert from Python to JSONConvert Python objects into JSON stringsConvert a Python object containing all the legal data typesUse the indent parameter to define the numbers of indentsUse the separators parameter to change the default separatorUse 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() functionUsing the search() functionUsing the split() functionUsing the sub() function
Python PIP
Python Try Except
When an error occurs, print a messageMany exceptionsUse the else keyword to define a block of code to be executed if no errors were raisedUse the finally block to execute code regardless if the try block raises an error or not
Python File Handling
Read a fileRead only parts of a fileRead one line of a fileLoop through the lines of a file to read the whole file, line by line
Python MySQL
Create a connection to a databaseCreate a database in MySQLCheck if a database existCreate a tableCheck if a table existCreate primary key when creating a tableInsert a record in a tableInsert multiple rowsGet inserted IDSelect all records from a tableSelect only some of the columns in a tableUse the fetchone() method to fetch only one row in a tableSelect with a filterWildcards charactersPrevent SQL injectionSort the result of a table alphabeticallySort the result in a descending order (reverse alphabetically)Delete records from an existing tablePrevent SQL injectionDelete an existing tableDelete a table if it existUpdate existing records in a tablePrevent SQL injectionLimit the number of records returned from a queryCombine rows from two or more tables, based on a related column between themLEFT JOINRIGHT JOIN
Python MongoDB
Create a databaseCheck if a database existCreate a collectionCheck if a collection existInsert into collectionReturn the id fieldInsert multiple documentsInsert multiple documents with specified IDsFind the first document in the selectionFind all documents in the selectionFind only some fieldsFilter the resultAdvanced queryFilter with regular expressionsSort the result alphabeticallySort the result descending (reverse alphabetically)Delete documentDelete many documentsDelete all documents in a collectionDelete a collectionUpdate a documentUpdate many/all documentsLimit the result