Python Examples

Python Examples

Python Syntax

  1. Print “Hello World”
  2. Comments in Python
  3. Docstrings

Syntax Explained

Python Variables

  1. Create a variable
  2. Output both text and a variable
  3. Add a variable to another variable

Variables Explained

Python Numbers

  1. Verify the type of an object
  2. Create integers
  3. Create floating point numbers
  4. Create scientific numbers with an “e” to indicate the power of 10
  5. Create complex numbers

Numbers Explained

Python Casting

  1. Casting – Integers
  2. Casting – Floats
  3. Casting – Strings

Casting Explained

Python Strings
  1. Get the character at position 1 of a string
  2. Substring. Get the characters from position 2 to position 5 (not included)
  3. Remove whitespace from the beginning or at the end of a string
  4. Return the length of a string
  5. Convert a string to lower case
  6. Convert a string to upper case
  7. Replace a string with another string
  8. Split a string into substrings

Strings Explained

Python Operators

  1. Addition operator
  2. Subtraction operator
  3. Multiplication operator
  4. Division operator
  5. Modulus operator
  6. Assignment operator

Operators Explained

Python Lists

  1. Create a list
  2. Access list items
  3. Change the value of a list item
  4. Loop through a list
  5. Check if a list item exists
  6. Get the length of a list
  7. Add an item to the end of a list
  8. Add an item at a specified index
  9. Remove an item
  10. Remove the last item
  11. Remove an item at a specified index
  12. Empty a list
  13. Using the list() constructor to make a list

Lists Explained

Python Tuples

  1. Create a tuple
  2. Access tuple items
  3. Change tuple values
  4. Loop through a tuple
  5. Check if a tuple item exists
  6. Get the length of a tuple
  7. Delete a tuple
  8. Using the tuple() constructor to create a tuple

Tuples Explained

Python Sets

  1. Create a set
  2. Loop through a set
  3. Check if an item exists
  4. Add an item to a set
  5. Add multiple items to a set
  6. Get the length of a set
  7. Remove an item in a set
  8. Remove an item in a set by using the discard() method
  9. Remove the last item in a set by using the pop() method
  10. Empty a set
  11. Delete a set
  12. Using the set() constructor to create a set

Sets Explained

Python Dictionaries

  1. Create a dictionary
  2. Access the items of a dictionary
  3. Change the value of a specific item in a dictionary
  4. Print all key names in a dictionary, one by one
  5. Print all values in a dictionary, one by one
  6. Using the values() function to return values of a dictionary
  7. Loop through both keys an values, by using the items() function
  8. Check if a key exists
  9. Get the length of a dictionary
  10. Add an item to a dictionary
  11. Remove an item from a dictionary
  12. Empty a dictionary
  13. Using the dict() constructor to create a dictionary

Dictionaries Explained

Python If … Else

  1. The if statement
  2. The elif statement
  3. The else statement
  4. Short hand if
  5. Short hand if … else
  6. The and keyword
  7. The or keyword

If … Else Explained

Python While Loop

  1. The while loop
  2. Using the break statement in a while loop
  3. Using the continue statement in a while loop

While Loop Explained

Python For Loop

  1. The for loop
  2. Loop through a string
  3. Using the break statement in a for loop
  4. Using the continue statement in a for loop
  5. Using the range() function in a for loop
  6. Else in for loop
  7. Nested for loop

For Loop Explained

Python Functions

  1. Create and call a function
  2. Function parameters
  3. Default parameter value
  4. Let a function return a value
  5. Recursion

Functions Explained

Python Lambda

  1. A lambda function that adds 10 to the number passed in as an argument
  2. A lambda function that multiplies argument a with argument b
  3. A lambda function that sums argument a, b, and c

Lambda Explained

Python Arrays

  1. Create an array
  2. Access the elements of an array
  3. Change the value of an array element
  4. Get the length of an array
  5. Loop through all elements of an array
  6. Add an element to an array
  7. Remove an element from an array

Arrays Explained

Python Classes and Objects

  1. Create a class
  2. Create an object
  3. The __init__() Function
  4. Create object methods
  5. The self parameter
  6. Modify object properties
  7. Delete object properties
  8. Delete an object

Classes/Objects Explained

Python Iterators

  1. Return an iterator from a tuple
  2. Return an iterator from a string
  3. Loop through an iterator
  4. Create an iterator
  5. Stop iteration

Iterators Explained

Python Modules

  1. Use a module
  2. Variables in module
  3. Re-naming a module
  4. Built-in modules
  5. Using the dir() function
  6. Import from module

Modules Explained

Python Dates

  1. Import the datetime module and display the current date
  2. Return the year and name of weekday
  3. Create a date object
  4. The strftime() Method

Dates Explained

Python Math

  1. Find the lowest and highest value in an iterable
  2. Return the absolute value of a number
  3. Return the value of x to the power of y (xy)
  4. Return the square root of a number
  5. Round a number upwards and downwards to its nearest integer
  6. Return the value of PI

Math Explained

Python JSON

  1. Convert from JSON to Python
  2. Convert from Python to JSON
  3. Convert Python objects into JSON strings
  4. Convert a Python object containing all the legal data types
  5. Use the indent parameter to define the numbers of indents
  6. Use the separators parameter to change the default separator
  7. Use the sort_keys parameter to specify if the result should be sorted or not

JSON Explained

Python RegEx

  1. Search a string to see if it starts with “The” and ends with “Spain”
  2. Using the findall() function
  3. Using the search() function
  4. Using the split() function
  5. Using the sub() function

RegEx Explained

Python PIP

  1. Using a package

PIP Explained

Python Try Except

  1. When an error occurs, print a message
  2. Many exceptions
  3. Use the else keyword to define a block of code to be executed if no errors were raised
  4. Use the finally block to execute code regardless if the try block raises an error or not

Try Except Explained

Python File Handling

  1. Read a file
  2. Read only parts of a file
  3. Read one line of a file
  4. Loop through the lines of a file to read the whole file, line by line

File Handling Explained

Python MySQL

  1. Create a connection to a database
  2. Create a database in MySQL
  3. Check if a database exist
  4. Create a table
  5. Check if a table exist
  6. Create primary key when creating a table
  7. Insert a record in a table
  8. Insert multiple rows
  9. Get inserted ID
  10. Select all records from a table
  11. Select only some of the columns in a table
  12. Use the fetchone() method to fetch only one row in a table
  13. Select with a filter
  14. Wildcards characters
  15. Prevent SQL injection
  16. Sort the result of a table alphabetically
  17. Sort the result in a descending order (reverse alphabetically)
  18. Delete records from an existing table
  19. Prevent SQL injection
  20. Delete an existing table
  21. Delete a table if it exist
  22. Update existing records in a table
  23. Prevent SQL injection
  24. Limit the number of records returned from a query
  25. Combine rows from two or more tables, based on a related column between them

MySQL Explained

Python MongoDB

  1. Create a database
  2. Check if a database exist
  3. Create a collection
  4. Check if a collection exist
  5. Insert into collection
  6. Return the id field
  7. Insert multiple documents
  8. Insert multiple documents with specified IDs
  9. Find the first document in the selection
  10. Find all documents in the selection
  11. Find only some fields
  12. Filter the result
  13. Advanced query
  14. Filter with regular expressions
  15. Sort the result alphabetically
  16. Sort the result descending (reverse alphabetically)
  17. Delete document
  18. Delete many documents
  19. Delete all documents in a collection
  20. Delete a collection
  21. Update a document
  22. Update many/all documents
  23. Limit the result

MongoDB Explained