Uploaded by sidnerlikar

introduction to python

advertisement
History of Python and Its
Importance
Python is a general-purpose programming language that was named after Monty
Python. It is simple and incredibly readable since it closely resembles the English
language. But still, why should you use Python?
Python is a language that finds use in nearly every domain possible. Its
official website will give you an overview of this. In addition, its simplicity and the
way it ensures tasks can be performed using fewer lines of code are encouraging
many developers across the world to take it up.
Play Video
4803892
Currently, there are two common versions of Python: Version 2 and 3 and later.
Apart from some syntactical differences, they are pretty similar. As support for
version 2 would fade over time, our course supports version 3.
It is preferred to uninstall Python 2 before installing Python 3. In case one does not
want to uninstall Python 2, to avoid confusion, setup virtual python environments
using pip on the prompt: sudo apt install python-pip.
Play Video
4803892
A high level language called a source code is used to write a computer program.
Source codes have to be converted into machine language with the help of
compilers and interpreters.
Compiler vs Interpreter
To install Python 3 on your own system, follow the steps in the document provided
at the end of the next segment.
You will need various Python packages (or synonymously, libraries) for specific
purposes.
Anaconda is an open-source distribution that simplifies package management and
deployment. The package management system 'Conda' manages package versions.
We strongly recommend using Anaconda to install Python as well as the packages
since it comes preloaded with most of the packages you will need.
Advantages of using Anaconda
1. It is easy to manage and supports most of the libraries required for Machine
learning/Artificial Intelligence problems.
2. Anaconda comes with many libraries such as NumPy, OpenCV, SciPy, PyQt,
the Spyder IDE, etc.
Anaconda can be downloaded from anaconda.org and can be installed like any other
normal software. There is no need to download Python separately; the Anaconda
installer will do this for you. Make sure you select Python 3.x while downloading
Anaconda.
Note for experienced Python programmers: In case you're already using Python
along with an existing package manager such as pip or easy_install, you can continue
to do so. However, make sure you're using Python 3.x.
Jupyter Notebook
You’ll use the Jupyter IPython Notebook as the main environment for writing
Python code throughout this program. The key advantage of using Jupyter Notebook
is that you can write both code and normal text (using the Markdown format in
Jupyter) in the notebooks. These notebooks are easy to read and share, and can even
be used to present your work to others. Here’s a brief overview of Jupyter Notebook.
The document given below provides instructions for installing Python and the
Jupyter Notebook using Anaconda.
Installation_documentation
Download
Also, refer to the document for help with installing Anaconda successfully. It is
important to note that you need to install Python 3.x (the latest 3.x version available),
not 2.x.
Please proceed to the next segment only after installing Anaconda and the Jupyter
notebook.
Additional Reading
If you want to know about the Jupyter notebook in brief check out this link:
Jupyter Notebook quick starter guide
Writing Your First Program
in Python
You can download the Python notebooks used in the lecture from the link below. It
is recommended that you keep executing the commands on your computer at pace
with the lecture on your jupyter notebook. Also, from the coding perspective for
practice questions and out of video questions, you are expected to use jupyter
notebook.
Jupyter notebook is an interactive computational environment in which one
can combine code execution, rich text, mathematics, plots and rich media.
Python notebook
Download
Now that you have installed Python notebook on your computer, let us start our
journey in Python by writing a small code and see how it executes.
Play Video
4803892
You have come across certain shortcuts involved in the execution of the demo
code. But there are other shortcuts that may come in handy as well while using the
Jupyter notebook. For these shortcuts, you can download and explore the notebook
given below. It provides a brief description of different shortcuts, and you will gain
expertise by using them regularly during hands-on practice.
Jupyter Notebook Introduction
Download
Note: Some of the questions here may include concepts from the shared
notebook.
Question 5/5
Mandatory
Question 1Correct
Question 2Incorrect
Question 3Correct
Question 4Correct
Question 5Correct
Additional Reading
If you want to know about the shortcuts for mac users and magic commands existing
in jupyter notebook, the links are provided below:
Jupyter mac shortcuts
Jupyter magic commands
In the next segment, we will learn about the different data types present in python
and how to convert one data type into another with the help of type casting.
Data Types in Python
Now that you know how to use Jupyter notebooks and have learned how to write the
basic “hello world!” program in Python, it’s time to understand how to declare a
variable in Python. We will also learn about the different data types available in
Python.
The following video will offer you an introduction to the basic syntax of declaring a
variable in Python.
Play Video
4803892
You have learned that variables are nothing but a memory location to store values
assigned to them. Some of the properties of these variables are:
1. There is no need to declare the data type of the variable as done in other
programming languages, such as C, C++, and JAVA
int c = 5
string name = 'Rahul'
2. The variable name (or identifier) cannot start with a number, i.e., declaring
something like 2name = 7 throws an error.
3. Python is case sensitive or in other words, these variables are case sensitive.
This means that declaring name= 2 & Name = 4 would create two different
variables.
Play Video
4803892
Let us look into the concept of converting one datatype to another in the following
video.
Play Video
4803892
In the previous video, you learned about the various data types supported in
Python. Now, let us move on and see how you can change one data type to
another in Python called typecasting from the video given below.
In order to change the data type of a particular variable, you can simply call the
following inbuilt methods in python.
In the above snapshot of code, you are assigning a value to a variable (x) and then
using typecasting, converting it into different data types. So for example, when
you convert x into an integer it converts the floating-point value into an integer
value.
Based on your learning about various data types and typecasting in Python from the
previous videos, attempt the quiz given below.
Question 2/2
Mandatory
Question 1Correct
Question 2Correct
In the next segment we will learn about the various arithmetic operations
performed in python programming and most importantly the precedence that we
follow for these operations.
Arithmetic Operations
In the previous segment, you learned about various data types in Python. Arithmetic
operations are an integral part of every programming language. Let's understand how
you perform one in Python from the following video.
Play Video
4803892
To do some mathematical operation which involves one or more than one operator,
we did follow some rules. The same is the case in Python where if multiple
operations have to be done in a single problem, then we make use of operator
precedence rule.
Let us understand Operator precedence using an example.
a = 4+(8**2)-3**2%1
To find the value of the variable 'a', the following steps have to be followed.
Step 1: The first step is to deal with brackets as it holds the highest precedence
among all operators in the given expression. The expression inside these brackets
[(8**2)] will get executed first to return 64.
Updated Expression: 4+64-3**2%1
Step 2: Moving on, you deal with the exponentiation operator [3**2] as it has the
next highest precedence when compared to other operators in the expression.
Updated Expression: 4+64-9%1
Step 3: Now you deal with the remainder operator as it has higher precedence
over subtraction and addition. This means the value 9%1 gets evaluated to return 0.
Updated Expression: 4+64-0
Step 4: In the next step, the subtraction operator gets executed as it holds higher
precedence over addition.
Updated Expression: 4+64
Step 5: The final step would be to perform addition
Answer: 68
Operator Precedence (from highest to lowest) :
Preference Operator
Type (respectively)
1
**
Exponential
2
~, +, -
Complement, Unary plus & minus
3
/, *, %, //
4
+,-
Add, Subtract
5
>>, <<
Bitwise shift right & left
6
&
Bitwise AND
7
^, |
Bitwise or regular OR
8
<, =, <, >, >, =
Comparisons
9
<, >, ==, !=
Equality
10
=, %=, /=, //=, -=, +=, *=,
**=
Division, Multiplication, Modulus, Floor
Division
Assignment
11
is, is not
Identity
12
in, not in
Membership
13
not, or, and
Logical
And this is how operator precedence rule plays an important part while doing
arithmetic operations in Python. Based on the concept you learned through the
given above example and video attempt the quiz given below.
Question 1/2
Mandatory
Operator Precedence
What would be the output of the following code?
x=3
y=2.5
z=5
print(X+y/z)
5.5
1.1
3.5
Error
Attempt 0 of 1
Submit
In the next segment we will learn about the various string operations performed
and methods associated with it in python programming.
String Operations
In the next set of videos, you will learn about various operations and manipulations
done by a string. So, let's have a look at each one of them.
String Indexing
Play Video
4803892
In the previous video, you learned about Indexing in strings. Always remember that
forward indexing starts with 0 and reverse indexing starts with -1, strings are
immutable which means they cannot be changed once created and strings can be
modified by slicing a part of it and concatenating.
String Concatenation
Play Video
4803892
Now, let's move on to the next video and understand another important
concept string slicing.
String Slicing
Play Video
4803892
Question 1/2
Mandatory
String Slicing
How will you extract Python from the string word "I love Python programming"?
(More than one answers may be correct.)
word[7:13]
word[7:12]
word[-12:-18]
word[-18:-12]
Attempt 0 of 2
Submit
In the previous videos, you saw how to do indexing and slicing in strings. But let us
say you want to change the character case or count the number of occurrences of a
substring in a string, they can be performed using the inbuilt string methods.
In our next video, let's look at these methods in detail.
String Methods
Play Video
4803892
Some more string methods:
1. str.len() , str.find() , str.format()
2. str.replace() , str.join() , str.split()
3. str.upper() , str.lower()
4. String boolean methods : str.isalnum() , str.isalpha() , str.isnumeric() ,
str.isspace() , str.islower() , str.isupper() , str.istitle()
In the previous video, you saw slicing in a string through several examples. Now,
based on your learning attempt the quiz given below.
Download