Category: 07. Python

  • Python String Formatting

    String format() The 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: ExampleGet your own Python Server…

  • Python User Input

    User Input Python allows for user input. That means we are able to ask the user for input. The method is a bit different in Python 3.6 than Python 2.7. Python 3.6 uses the 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…

  • Python PIP

    What is PIP? PIP is a package manager for Python packages, or modules if you like. Note: If you have Python version 3.4 or later, PIP is included by default. What is a Package? A package contains all the files you need for a module. Modules are Python code libraries you can include in your project.…

  • Python RegEx

    RegEx Module Python has a built-in package called re, which can be used to work with Regular Expressions. Import the re module: import re RegEx in Python When you have imported the re module, you can start using regular expressions: ExampleGet your own Python Server Search the string to see if it starts with “The” and ends with “Spain”: import re txt…

  • Python JSON

    JSON in Python Python has a built-in package called json, which can be used to work with JSON data. ExampleGet your own Python Server Import the json module: Parse JSON – Convert from JSON to Python If you have a JSON string, you can parse it by using the json.loads() method. The result will be a Python dictionary. Example…

  • Python Datetime

    Python Dates A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects. ExampleGet your own Python Server Import the datetime module and display the current date: Date Output When we execute the code from the example above the result will…

  • Python Modules

    What is a Module? Consider a module to be the same as a code library. A file containing a set of functions you want to include in your application. Create a Module To create a module just save the code you want in a file with the file extension .py: ExampleGet your own Python Server Save…

  • Python Scope

    Local Scope A variable created inside a function belongs to the local scope of that function, and can only be used inside that function. ExampleGet your own Python Server A variable created inside a function is available inside that function: Function Inside Function As explained in the example above, the variable x is not available outside the function, but…

  • Python Polymorphism

    Function Polymorphism An example of a Python function that can be used on different objects is the len() function. String For strings len() returns the number of characters: ExampleGet your own Python Server Tuple For tuples len() returns the number of items in the tuple: Example Dictionary For dictionaries len() returns the number of key/value pairs in the dictionary: Example Class Polymorphism Polymorphism…

  • Python Dictionaries

    Dictionary Dictionaries are used to store data values in key:value pairs. A dictionary is a collection which is ordered*, changeable and do not allow duplicates. As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered. Dictionaries are written with curly brackets, and have keys and values: ExampleGet your own Python Server…