Python round() Function

Python round() Function

The Python round() function is another useful built-in Python function that can be used when doing computational work. The first parameter for the round() function is a numerical value and is required. The round() function can also accept an integer as an optional second input that specifies the number of digits after the decimal point to round to. When the function runs, the rounded value of the first input is returned.…

Python abs() Function

Python abs() Function

A built-in Python function that gets used a lot in computational work is the abs() function. The abs() function accepts a numeric value as input and returns the absolute value of that input. The absolute value refers to the non-negative value of a number. So for example the absolute value of -10 is 10. The absolute value of 10 is also 10. In this tutorial, we will look at examples of how to use the abs() function in some small Python programs.…

Python input() Function

Python Input Function

The input() function in Python makes it very easy to get input from users of your application. The input function is built in just like the print function and its use is quite common. When you call the input function(), you can think of it as an expression that evaluates to whatever the user typed in at the prompt. This always gets returned as a string so if you want to use numbers as input, you will need to handle that by using the int() function.…

Python print() Function

Python print function

There are several key functions in the Python programming language that are super useful. The Python print function is definitely one of the first ones that you should know. The print function accepts data as an input and then displays that data to the screen for the user to view. The print() function will be used all the time since it allows you to output the contents of a variable to the screen, allowing you to understand how your program is processing data.…

Python Data Class

Python Data Class

Part of what classes are used for in Python is to represent data. It is by using the __init__ function that we can accept data into an object as it is created, and assign those data values to the object using the self keyword. You might notice that sometimes this code setup has a lot of boilerplate to it. In other words, you have to write the same structure of code to simply store simple values on an object.…

What Are Python Magic Methods

What Are Python Magic Methods

Magic Methods in Python are a collection of methods that are automatically associated with every class definition in the language. If you create your own classes, you can override one or more standard magic methods to customize their behavior. There are many magic methods in Python, and we include a table of them at the end of this tutorial. For now, we want to take a look at some of the more commonly used magic methods which can be useful in day to day programming.…

Composition Over Inheritance

Composition Over Inheritance

Inheritance in Python is nice since it facilitates a great deal of code reuse. We put common data and behavior into a Base class and then implement any extra data and behavior needed in derived classes. Therefore when using inheritance, you are making use of an “is a” relationship. For example, a Car is a Vehicle. Another approach to code reuse when using the object-oriented style of programming in Python is Composition.…

Python Interface Design Pattern

Python Interface Design Pattern

By using a combination of multiple inheritance and abstract classes, you can use a design pattern in Python called an Interface. Interfaces are a common technique in object-oriented programming and programming languages like C# and Java implement this feature natively. Interfaces aren’t actually part of the Python language itself, but you can still use this type of design pattern with Python. In interface is a promise, or contract, that forces a given class to implement a given behavior.…