We previously saw about functions, and how we pass a single argument. In reality, functions usually accept multiple arguments.
Here's an example:
Python also allows us to declare functions which receive a variable number of arguments.
The syntax for that is as follows:
Quiz: Can you guess what the above code snippet will print?
Here is an excellent video to understand OOP in Python better. Just sharing because I found it very helpful!
Python is an Object Oriented Programming language. Objects are an encapsulation of variables and functions into a single entity. Objects get their variables and functions from classes.
You can think of Classes as a template to create your objects.
Here's a simple class. Once we create the class, we can create an instance of it.
You can now access the variables inside the class (in our example, "name") using "." on the object
This will print "Kavin" in the console.
As I mentioned, a class is a template by which you can create multiple objects. Think of class as a cookie-cutter. Using a cookie-cutter, you can make any number of cookies. Same way, using a class, we can create any number of objects, each with their copy of the variables. See the modified example below:
This will print:
A dictionary is a data type similar to arrays, but works as a key-value store.
We access it with keys and values instead of indexes.
The values stored in a dictionary can be accessed using a key, which is any type of object (a string, a number,) instead of using its index to address it.
Note: In Java world, this is called a HashMap.
Here are some Python dictionary examples:
Iterating through the dictionary:
Now, to remove an item, we can either use pop or del notation. Note: you will be accessing the dict by the key.
The output only shows Steve, because both the items (Jake and Liz) have been removed.
Dictionaries are simple, but powerful. A lot of problems can be solved using this.
If you have ever looked at C code, you would be familiar with the string formatting that uses % to create new, formatted strings.
Python uses the same C-style string formatting.
The "%" operator is used to format a set of variables enclosed in a fixed list, together with a format string, which contains normal text together with "argument specifiers", special symbols like "%s" and "%d".
Let's say we have a variable called "name", and we would like to print a "message" to that user.
If we need to pass more than one argument, then we use the paranthesis
Here are some specifiers we need to be aware of:
Here are some basic argument specifiers you should know:
Through Functions, we have an easy way to have modular code, they allow us to to divide our code into useful blocks, making it more readable, reusable and saves us time.
Here are some examples:
Python is not "statically typed" language. This means we don't need to declare variables before using them, or declare their type. Here are some examples of declaring numbers (integers, float) and strings.
Python Intro - Hello World!
Python is a simple programming language. It has a very straightforward syntax.
The simplest directive in Python is the "print" directive - it simply prints out a line There are two major Python versions, Python 2 and Python 3. Python 2 and 3 are very different.
Python is a favorite language for Data Science.
As with any programming language, the first thing we need to do is Hello World!
Look how easy it is to do that in Python: