Party On With Python 2: Party Harder
Basic Concepts of Python
Welcome back to my introduction to Python! In this blog post I will be discussing the basic concepts of Python. As I discussed in my previous blog post, Python’s design goals were to make the language easy to read as to increase clarity and cut down on user errors. What I didn’t really get into was how it does this. Python is a dynamically and strongly typed language. It’s dynamic in the sense that values of variables are given a specific type as opposed to the variables themselves so naturally the programmer doesn’t need to specify the type of a variable as the type is already given based on what value the variable contains, for example my_variable = 5.0 will naturally be of a floating-point number data type due to the value itself. It’s also strongly typed as that the types of those values cannot change, the “String” type cannot be assumed to be a “Number” type unless explicitly converted to. This makes values effectively immutable except for a few types such as lists and when a new value is given to a variable that already contains a value, it is simply given that new value and the old one is thrown away. This in turn means that the memory location is actually specified in the value of variable as opposed to the variable itself. These two concepts allow for python to be the high-level language that it is as one doesn’t have to add additional type declaration at the beginning of each variable, cutting down on the overall word count of your code and increasing the speed at which one can iterate on a project.
Data Types in Python are, unsurprisingly, few as there is only three principal data types:
- Numbers which include whole integers numbers, floating point numbers and complex numbers.
- Strings which are a series of characters wrapped by either a single quote or double quote.
- Lists include Tuples as well as Dictionaries are simply arrays of values. Lists use indexes to control elements while Tuples are read only and Dictionaries are associative arrays that use keys to control elements.
As I explained in my previous paragraph, data types are specified by the value so to declare an integer one must simply declare a namespace such as my_integer and give it a whole number with no decimal while Floating-point numbers require a decimal. Complex numbers on the other hand require a “j” character at the end of the number regardless of decimal or not. The “j” specifies an imaginary number.
Strings can be thought of as a list or array of characters, even though there is no actual character data type in Python. To obtain a character from a string the slice operator must be used. The slice operator is represented by square brackets at the end of the namespace of the string similar to java.
Python supports a variety of packed in functions for the string data type such as length() which will return the length of the string as an integer value. Checks can be made against the string using isalpha() or isdigit() to see if it contains only digits or only alphabetic characters. Strings can be converted to uppercase or lowercase using upper() or lower(). These are just a few of the functions that allow strings to be versatile within Python.
Lists are just that, lists of elements. Each element is separated by a comma with each element being accessed by its own index in the form of an integer beginning at zero and ascending with the number of elements contained in the list. Elements are accessed using the same splice operator that the string uses. Tuples cannot be modified once created but elements can still be accessed using the slice operator. Dictionaries are the same as lists but instead of using integer indices, it uses keys in the form of strings.
Functions & Methods
Functions and methods are blocks of encapsulated code created to be modulated and be reused. Methods are considered functions that are created inside of a class while functions are held outside of a class. Functions are defined by the def keyword followed by the namespace of the function and parentheses, ending in a colon. The parentheses will contain any input parameters to be passed to the function. Methods are required to have the “self” keyword as one of the parameters as this represents the object itself.
Calling a function is the same as other programming languages, simply call the namespace of the function, adding any input parameters required.
If you have any understanding of Java classes, then you’ll be happy as Python’s classes are incredibly similar outside of some syntax differences. For those unfamiliar with the term, Classes are blueprints for the creation of objects. They allow the programmer to specify the contents, inputs/outputs, inheritance, methods, of the object. Python classes are defined by the “Class” keyword followed by the namespace defined by the user and ends with a colon to signify the beginning of the class block.
Within the class we can define a constructor for the object which can be used to create the object. The constructor is defined by a standardized initialization method which is represented by the “def” keyword followed by a standardized namespace of __init__ and parenthesis containing the input parameters and the keyword “self”.
The “self” keyword represents the object itself and allows the user to specify the attributes of its own object to avoid variable shadowing. It can also be used to define attributes within the object itself, so long as you give that attribute a value.
Exceptions are errors that can be caused in a variety of ways but typically occur in your code either from invalid user input or invalid arguments. In Python we handle these with a “try-except” statements. These statements are used to change the flow of a program so that if an error does occur, it’s caught and dealt with dealt with before continuing with the program.
The try except block is defined by the “try” keyword followed by a colon to specify the start of the block that we will be checking for exceptions. The block is terminated by the “except” keyword followed by the name of the exception that we’re trying to catch in that block and another colon. In this new block we can define what action should be taken should this exception be caught.
Python is ultimately a very rewarding language and I’ve only scratched the surface when it comes to its feature set. I hope this little intro has helped you and I encourage you to party on with Python!