What is Python 3?
Python 3 is an interpreted language, which means that anyone can read and execute the code. Python is used to create websites, perform scientific research, data analysis etc. Python 3.9 is the latest version of Python.
Why Learn Python 3?
Python is one of the fastest growing and in-demand programming languages. It has a very easy learning curve, due in large part to its simple, user-friendly syntax. Stack overflow’s annual survey placed Python as the number one programming language of 2020.
Python comes with a rich support of libraries with crisp documentation. This, along with the fact that it is open source, has made it popular among many organizations who have used it to create their programs. Python has a very dedicated community who work to keep it updated all the time. Python is a must-learn for those seeking jobs in the software industry. There are many applications of Python, and the most popular ones are in the realms of website deployment and data science, which includes machine learning, data analysis and visualization and scripting.
Changes in Python 3
- Print statement has been changed to print func and syntax in Python requires brackets around the string that you want to print. You can also use multiple arguments to concatenate the objects together.
- Second main change is that all strings are unicode strings and you do not have to specify unicode strings with lower case "u" before the strings.
- raw_input() has been changed to input function input()
- Also some data structures objects will no longer return lists. Rather they will return views in python3 like dict.items() and dict.values() now returns views instead of lists.
- A lot of new library modules have been added in python3 and many improvements have been made to existing modules.
- Some improvements have been done in exception handling as well.
Variables and Data Structures
Variables:
Variables allow us to store data. We can store diff values like integers, floats, booleans, string, lists or dictionaries. Python reserves space on your machine for a value when a var is declared. So, variable can be declared as below.
varName = value
Example
- x = 1 that means we have assigned number 1 which is an integer to variable x
- y = 3.14 floating point
- mystring = “Hello World” means assigning this sentence which is a string in mystring variable
Data Structures:
Data structures are a way to store more data than variables within the same variable, for example let’s say we have a variable list_demo below
list_demo = [1, 2, 3, 4]
This is a data structure of type list. It may also be known as an array in other programming languages but in Python it's called list, so in this case, we have multiple numbers stored within the same variable which is what the list is. We can store floating point or strings or mix of numbers and strings within the same variable.
The syntax for accessing each element of the list is variable_name[index]. Index within the square brackets in list starts from number zero ,for example in above example, 1 is at index 0,2 is at index 1 etc.
print(list_demo[0]) will yield 1.
Lists are a more convenient way to store multiple numbers or string within the single variable. The second important data structure is dictionaries which store data in the form of key value pairs, for example:
dict_demo = {‘fname’ : ’fname1’,’lname’ : ‘lname1’}
The above dictionary can be interpreted as the first name is name1 and we can separate different key value pairs by a comma, as shown above. The way we access the dictionary is variable_name[index] and the way indexing works in dictionary is by key pairs or rather keys like below
print(dict_demo[‘fname’]) will yield fname1.
Python Functions:
Functions are basically some instructions packaged together that perform a specific task. Let's create a function using the def keyword and see why these are so helpful:
def world_func():
pass
It's possible to write a function and not have any code in it. This allows you to add the code later on by having a pass keyword inside the function. This will let the compiler know that it should not throw any error leaving the function empty. If you want to run the function, just call the function name and put the parentheses after the func name, as shown below:
def world_func():
pass
world_func()
def world_func():
<some code>
world_func()
Python OOP:
Object oriented programming allows us to logically group data and functions in a way that is easy to reuse and also easy to build upon. When I say data and functions that are associated with a specific class, they are called attributes and methods. When I say methods, I mean a function that is associated with a class.
Let’s understand this by taking a simple example.
Let’s suppose we have to build an application for a company which will represent the details of the employees, where each employee is going to have specific attributes and methods. For example, each employee is going to have a name, email id, pay and also actions that they can perform, so it would be nice if we had a class that we could use as a blueprint to create employee details, so that we did not have to do this manually each time from scratch.
Let’s go ahead and create a simple employee class.
class Employee:
pass
A class is basically a blueprint for creating instances and each unique employee that we create using our employee class will be an instance of that class. Let’s see an example for this:
class Employee:
pass
ep1 = Employee()
ep2 = Employee()
ep1 and ep1 are the unique instances of the employee class, so if you print both these instances, you will have different objects with different locations in the memory.
Top Cities Where KnowledgeHut Conduct Python Certification Course Online
Python File I/O:
Let’s see how we can read and write to files in Python. To play around with file object, Python has a built-in open command.
Consider a file called file1.txt. In order to open this file, we may use the open method. But the open method is not recommended. Rather, it is recommended to use a context manager.
f = open(‘file1.txt’)
If your file is located in a specific path, you can pass the path to that file into the open command as above. So the open method allows us to specify whether we want to open this file for reading, writing, appending or reading and writing. If you do not specify anything apart from the file name then it reverts to the default setting, which is to open the file for reading. But it's better to be explicit, as shown below:
f = open(‘file1.txt’, ‘r’) -- reading a file
f = open(‘file1.txt’, ‘w’) -- writing to file
f = open(‘file1.txt’, ‘a’) -- appending to file
f = open(‘file1.txt’, r+) -- reading and writing both to file
Now with these commands, the file is actually open and using this method you have to explicitly close the file to avoid leaks once you are done with the file operations, like below:
print(f.name) -- will print the file name
f.close() --- will close the file
Now the better way to perform file operation is to use context manager by using with keyword which implicitly takes care of closing the file after we exit the block of the code as shown below.
with open(‘file1.txt’,’r’) as f:
pass
Conclusion:
Hope this blog has help you understand some common features of Python, as well as the important updates and core concepts of python3.