Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
80 views

1.python Tutorial

This Python tutorial provides an overview of Python programming and covers topics such as Python basics, installation, variables, data types, conditional statements, loops, functions, OOP concepts, and Python cheat sheets. It discusses Python's history and characteristics, compares Python 2 and 3, and explains how to install Python and work with variables, data types, conditional statements, loops, functions, and OOP concepts in Python. It also discusses career opportunities in Python and its large open source community.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

1.python Tutorial

This Python tutorial provides an overview of Python programming and covers topics such as Python basics, installation, variables, data types, conditional statements, loops, functions, OOP concepts, and Python cheat sheets. It discusses Python's history and characteristics, compares Python 2 and 3, and explains how to install Python and work with variables, data types, conditional statements, loops, functions, and OOP concepts in Python. It also discusses career opportunities in Python and its large open source community.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Courses

Free Courses Interview Questions Tutorials Community

Home /
Tutorial /
Python Tutorial

Python Tutorial
By Manoj 3 K Views 34  min read Updated on April 20, 2022

This Python programming tutorial will help you learn Python programming and build a career in it. This tutorial contains Python
basics, its installation, variables, data types, conditional statements, loops, user-defined functions, Oops Concepts, and Python
Cheat Sheets.

Become a Certified Professional

Python Tutorial

Python Tutorial

Fundamentals of Python

Python Introduction

What is Python?

Python Version

Python Frameworks

How to Download and


Install Python?

Python Syntax

Python Input and


Output

Comments in Python

Data Types in Python

Python Variables -
Constant, Global & Static
Variables

Numbers in Python

String in Python

Python Lists

Tuple in Python

Python Sets

Python Dictionary
Python Operators

Type conversion in
Python

Python If Else
Statements

Python While Loop

For Loop in Python

Python Functions -
Define & Call a Functions
in Python

Lambda Function in
Python

Python Built in
Functions with Examples

In this Python tutorial, we will comprehensively learn all the concepts of Python. The following are the topics we are going
to cover in this tutorial:

Introduction to Python
Why Learn Python?
Python History
Characteristics of Python
Career Opportunities
Python Installation
Python 2 Vs. Python 3
Variables in Python
Data Types in Python
OOPs Concepts in Python
Numbers in Python
Conditional Statements
Types of Loops in Python
User-Defined Functions
Exception Handling
Applications of Python

Watch this Python Tutorial Video for Beginners:

Python Course | Python Tutorial for Beginners | Int…


Int…
Introduction to Python
Python is a free, open-source programming language. Therefore, all you have to do is install Python once, and you can start
working with it. Not to mention that you can contribute your own code to the community.

Python is also a cross-platform compatible language. So, what does this mean? Well, you can install and run Python on
several operating systems. Whether you have a Windows, Mac, or Linux, you can rest assured that Python will work on all
these operating systems.

Python is also a great visualization tool. It provides libraries such as Matplotlib, Seaborn, and bokeh to create stunning
visualizations.

In addition, Python is the most popular language for Machine learning and Deep Learning. Today, all top organizations are
investing in Python to implement Machine Learning in the back-end.

Why learn Python?


Learning a python programming language is fun. If you compare Python with any other language, for example, Java or C++,
then you will find that its syntax is a way lot easier. You also don’t have to worry about the missing semicolons (;) in the end!

Suppose we want to print “Welcome to the world of programming” on our screen. Let’s compare the syntax for Python and
Java:

Python Syntax:

print(“Welcome to the world of programming”)

Java Syntax:

class Simple{

public static void main(String args[]){

System.out.println("Welcome to the world of programming");

So here we see that Python code consists of only one line, but for Java, there are multiple lines of code just for printing a
statement.

Interested in learning Python? Enroll in our Python Course in London now!

Python History
Python’s roots are traced back to the late 1980s. Guido Van Rossum of CWI in the Netherlands began implementing Python
in December 1989. Python labeled version was first published in February 199,1 whereas, Python 1.0 was launched in 1994,
and it included new capabilities such as lambda, map, filter, and reduce. List comprehensions and garbage collection
mechanisms were included in Python 2.0. Python 3.0 (commonly known as “Py3K”) was released on December 3, 2008. It
was created to correct language’s underlying weakness. The ABC programming language, which was capable of Exception
Handling and connecting with the Amoeba Operating System, is claimed to be the forerunner of Python.

Characteristics of Python
Easy and simple to learn and understand.
Python enables object-oriented programming and concepts such as classes, encapsulation, and so on.
Python is a dynamically typed language
It turns the code into bytecode, which can be run on any platform.
It is an extensible programming language which means we can write and compile code like any other programming
language.
Python is also an integrated language since it can easily be used with other programming languages such as C, C++,
Java, and others.

Career Opportunities:
Python has huge career opportunities in the IT industry. Almost every other IT company, be it a startup or a Multi-National
Company uses python for varied applications. So, if you have good expertise in python, you will be in demand for a wide
range of jobs in different domains such as machine learning, cloud infrastructure, website designing, testing, and many
more.

Large Open Source Community:


Let’s say you are working on a python project and you get stuck somewhere, you don’t have to worry at all because python
has a huge community for help. So, if you have any queries, you can directly seek help from millions of python community
members.

Now, in this tutorial, we will look at the procedure to install python.

Learn how to use Machine Learning in Python with this Machine Learning Tutorial.

Python Installation
If you are new to programming, then installing a programming language itself could be a herculean task. So, now we are
going to look at the step-by-step process to install python.

1. Start off by going to this website -> Downloads Python

2. Click on the downloads tab and choose the operating system and python version. So, here I am downloading python
version 3.7.4 for  Windows operating system.

Now that we have installed python, let’s go ahead in this tutorial and start off with programming in Python

For the best of career growth, check out Intellipaat’s Python Course in Sydney and get certified!

Python 2 Vs. Python 3


The two versions of Python- Python 2  and Python 3 are the most widely used Python versions and there are many
differences between these versions which are as follows:

Python 2 Python 3

The release year of Python 2 is 2000 The release year of Python 3 is 2008
The syntax is more complex in this version than in Python The syntax is easy and simple
3

By default, strings are saved in ASCII format in version 2 By default, strings are saved in UNICODE format in this
of Python version.

In Python 2, Print is a statement. So, the syntax is print In Python 3, Print is a function. So, the syntax is print
“hello” (“hello”).

Exceptions should be enclosed in notations in Python 2. Exceptions should be enclosed in parentheses in Python
3.

In python 2, while using variables inside a for-loop, their In Python 3, the value of variables stays constant.
values do change.

Python 2 is not that popular after 2020 compared to Python 3 is a more popular version of Python and is being
Python 3 used for many purposes

To perform iterations in Python 2, xrange() is used To perform iterations in Python 3, Range() is used

Variables in Python:
You can consider a variable to be a temporary storage space where you can keep changing values. Let’s take this example
to understand variables:

So, let’s say, we have this cart and initially we store an apple in it.

After a while, we take out this apple and replace it with a banana.

Again, after some time, we replace this banana with a mango.


So, here this cart acts like a variable, where the values stored in it keep on changing.

Now, that we have understood what a variable is, let’s go ahead and see how can we assign values to a variable in python.

Assigning values to a variable:

To assign values to a variable in Python, we will use the assignment (=) operator.

Here, initially, we have stored a numeric value -> 10 in the variable ‘a’.  After a while, we have stored a string value ->
“sparta” in the same variable. And then, we have stored the logical value True.

Now, let’s implement the same thing in Jupyter Notebook and look at the result:

Assigning a value 10 to a:

Allocating “sparta” to a:

Assigning True to a:

Going ahead in this tutorial, we will learn about data types in Python.

Watch Learn Python Course for Beginners

Python Tutorial | Python Course | Intellipaat


Data Types in Python
Every variable is associated with a data type and these are the different types of   data types available in python:

Now, let’s understand these individual data types by their implementation in the Jupyter notebook.

OOPS Concepts in Python


From its early beginning, Python has been an object-oriented language. Object-oriented programming (OOPs) is a
programming technique that emphasizes the usage of classes and objects. Its goal is to use programming to create real-
world concepts like inheritance, polymorphisms, and encapsulation. The basic idea behind OOPs is to combine data and
the functions that interact with it into a single entity so that no other parts of the code may touch it. It also emphasizes the
creation of reusable code. It is a common method of resolving an issue by generating objects.

The key concepts of Oops are as follows:

Objects
Classes
Inheritance
Polymorphisms
Encapsulation

Numbers in Python
Numbers in python could be integers, floating-point numbers or complex numbers.

Let’s start off with an example on integer:


Here, we have assigned the value 100 to num1 and when we check the type of the variable, we see that it is an integer.

Next, we have an example on floating-point number:

This time, we have assigned the value 13.4 to num2 and checking the type of the variable, tells us that it is float.

Finally, let’s look at an example of a complex number:

Here, we have assigned the value 10-10j to num3. Now 10-10j comprises two parts->  the real part and the imaginary part
and combining these two gives us the complex number.

Now, let’s start with Python Strings.

Intellipaat is providing free Python Interview Questions and Answers, which will help you excel in your career!

Python Strings
Anything written in single or double quotes is treated as a string in Python.

          

Now, let’s see how can we extract individual characters from a string.

So, I’d want to extract the first two characters from ‘str1’ which I have created above:

Now, similarly, let’s extract the last two characters from str1:

Now, let’s head onto tuples in Python:

Python Tuples
A python tuple is a collection of immutable Python objects enclosed within parentheses(). Elements in a tuple could be of
the same data type or of the different data types.

Let’s create a tuple where elements are of the same data type:
Now, let’s access the first element from this tuple:

Extracting the last element from this tuple:

Now, we will go ahead and learn about Python lists.

We have the perfect professional Python Course in Bangalore for you!

Python Lists
Python Lists is an ordered collection of elements.

_blank

It can contain elements of different data types, unlike arrays.

Now, let’s create a list with different data types:

Now, let’s do some operation on the list we created:

Fetching the first element from the list:

Adding an element while removing the other:

The below line of code will return the length of the list:

This will return the list in reversed order.

Now, we will further look at Python Sets.

Get 100% Hike!


Master Most in Demand Skills Now !

Email Address Phone Number


Submit

Python Sets
Python sets are a collection of unordered and unindexed items.

Every element in a set is unique and it does contain duplicate values.

Sets can be used to perform mathematical calculations such as union, intersection, and differences.

Creating a set:

Here, in set ‘Age’, value “22” is appearing twice. Since every element in set is unique, it will remove the duplicate value.

Operations on Sets:

1. Add:  This method adds an element to the set if it is not present in it.

2. Union: It returns the union of two sets.

3. Intersection: This method returns the intersection of two sets.

4. Difference: The difference of two sets(set1, set2) will return the elements which are present only in set1.

Now, we will look at Python Dictionary.

Python Dictionary
Python Dictionaries is an unordered collection of data. The data in the dictionary is stored as a key:value pair where the
key should not be mutable and value can be of any type.

Creating a Dictionary:

Accessing elements from a dictionary:

Removing elements from a dictionary:

Replacing elements in a dictionary:

Going ahead in this tutorial, we will learn about conditional statements.

Career Transition

Conditional Statements
We use a conditional statement to run a single line of code or a set of codes if it satisfies certain conditions. If a condition is
true, the code executes, otherwise, control passes to the next control statement.
There are three types of conditional statements as illustrated in the above example:

1. If statement: Firstly, “if” condition is checked and if it is true the statements under “if” statements will be executed. If it is
false, then the control will be passed on to the next conditional statements.
2. Elif statement: If the previous condition is false, either it could be “if” condition or “elif” after “if”, then the control is
passed on to the “elif” statements. If it is true then the statements after the “elif” condition will execute. There can be
more than one “elif” statement.
3. Else statement: When “if” and “elif” conditions are false, then the control is passed on to the “else” statement and it will
execute.

Now, let’s go ahead and learn about loops in this tutorial.

Types of Loops in Python


If we have a block of code then statements in it will be executed sequentially. But, when we want a statement or a set of
statements to be executed multiple times then we use loops.

Types of loops:

1. While loop:
loop: We use this loop when we want a statement or a set of statement to execute as long as the Boolean condition
associated with it satisfies.

In the while loop, the number of iterations depends on the condition which is applied to the while loop.

2. for loop:
Here, we know the number of iterations unlike while loop. This for loop is also used for iterations of statements or a set of
statements multiple times.

3. Nested loop:
This type of loop consists of a loop inside a loop. It can be for loop or can be a combination of for and while loop.

Now, we will learn about user-defined functions in this tutorial.

User-Defined Function
In any programming language, functions are a better and systematic way of writing. Functions provide us the liberty to use
the code inside it whenever it is needed just by calling the function by its name.

Syntax: def function()

Going ahead in this tutorial, we will learn about Exception Handling.

Exception Handling
Basically, an exception is an abnormal condition or error that occurs during the execution of a program. Whenever an
exception occurs in a program, the execution of the program halts and the further instruction of the programs are not
executed.

We need to handle these exceptions in order to ensure the normal execution of the program.

Some of the common exceptions that occur in Python programs while executing are:

1. NameError: This error occurs when a name is not found.


2. ZeroDivisionError: When a number is divided by zero, ZeroDivisionError occurs.
3. IndentationError: This error occurs due to the wrong indentation.
4. IOError: When Input-Output operation fails, IOError occurs.

So, in Python we use the try, catch, except, and finally to handle the exceptions.

try-except block
In the Python program, the line of code that may throw exceptions is placed in the try block.

The try block should have an except block with it to handle the exception. As an illustration – If any exception occurs in the
try block then the statements in the except block will be executed.
Syntax:

try:

#line of code

except Exception:

#line of code

#rest of the code

Example:

“else” block
In this case, the else block will be executed if no exception occurs.

Syntax:

try:

#line of code

except Exception:

#line of code

else:

#If there is no exception then this piece of code will be executed

Example:

“finally” block
If we want a piece of executable code, which we cannot skip under any circumstances, then we need to put this code in the
finally block.

Syntax:
try:

#line of code

finally:

#This piece of code will definitely be executed.

Example:

Applications of Python  
These are some applications of Python:

Python may be used to create web apps


Python can be used as a support language and hence, the best use for software development
It is also used in scientific and numeric applications.
To build e-commerce websites or ERP applications, Python will be of great use.
Fandango, AnyCAD, and RCAM are all functions in Python that can be used to construct a 3D CAD application.
Many libraries for working with image processing applications are available in Python.
Python is versatile enough to execute a variety of tasks, therefore it can also be utilized to construct multimedia apps.

Here, in this tutorial, we learned all the basics of Python which are variables, string, numbers, data types, tuples, lists, sets,
dictionaries, conditional statements, loops, user-defined functions, and exception handling. If you want to go through more
concepts of Python in-depth, this Tutorial consists of all the modules which will help you throughout your learning.

If you have any doubts or queries related to Python concepts, do a post on Python Community.

Frequently Asked Questions

What is Python used for?

Python is used to develop different types of applications such as web applications, GUI-based applications, software
development applications, scientific and numeric applications, network programming, gaming and 3D applications, and
business applications. Also, Python has widely used in the field of data analytics, and data science.

How do I start learning Python?

Is Python easy to learn?

Can a beginner learn Python?

How fast can I learn Python?

You might also like