1.1introduction To Python
1.1introduction To Python
What is Python:
Python is a high-level object oriented programming language created by
Guido Van Rossum. It is used in most of the domains and hence called as
general purpose language too. The domains where it is used:
• Artificial Intelligence
• Machine Learning
• Deep Learning
• Data Analytics
• Game Development
• Software Development
• Web Development
Why Python?
• Python is very simple to understand
• Python is scalable making development
speed fast
• Simple English like syntax
• Allow developers to write programs with
fewer lines of code
• It is open-source
• Many libraries are available to make work
easy
• Has power to handle large datasets
• Python has been listed as top programming
language in IEEE spectrum list in 2021 and
2022. Python is open-source and its source-
code is available publically at GNU general
public license.
Advantages of Python
• Python is interpreted.
• Python is interactive.
• Python is a beginner’s language.
• Python supports structural, functional and object-oriented
programming methods.
• Provides high level dynamic datatypes and supports type-checking
dynamically.
• It can easily be integrated with C, C++, Matlab, Java etc.
• It has advantage of automatic garbage collection
Applications of Python
• Python has few keywords, clearly defined syntax and simple structure. This
makes Python an easy-to-learn language.
• Python is written in very simple way making its code more readable and visible
to eyes.
• The source code of Python is easy to maintain.
• Python comes with bulk of libraries which are portable and compatible with
most platforms like Windows, Linux, Macintosh etc.
• Python supports interactive mode to perform interactive testing and
debugging.
• Provides interfaces to almost all major commercial databases.
• It supports GUI applications.
Getting started with Python
• It is important to understand the documentation of Python which is a good
starting point. The official website is www.python.org. You can start coding with
many options available these days. You can use any of the following IDEs to start
with Python coding:
• Google Colab: https://colab.research.google.com/
• Spyder: https://www.spyder-ide.org/
• Pycharm: https://www.jetbrains.com/pycharm/
• Jupyter notebook: https://jupyter.org/
• Anaconda: https://www.anaconda.com/products/distribution
• From the above list, I would suggest you download anaconda in your system. It
includes Jupyter notebook, Jupyter Lab, Spyder, R studio etc. all in the same setup.
Features of Anaconda
Anaconda Navigator
Jupyter Notebook
Once you launch the
Jupyter notebook from
anaconda, you will be able
to see your system
folders. If you have any
code saved there, you can
simply open it up by
navigating to the code
location. Else you can
start by making a new
Python 3 notebook as
shown.
Create Python3 notebook:
Looks like a line by line interpreter
Writing “Hello World”
• Before we start writing our code, we must know that Python is case-
sensitive and the use of spaces and tabs in a proper way is really
important.
• The next step is to write your first hello world program. You simply
have to use the print() function and write in double quotes the
statement you want to print. In our case, it is simply print (“Hello
World”). Then you have to run the code by clicking on “Run” button
Figure 7. The “+” button is used to insert new cell.
Writing “Hello World”
Comments and Headings
• If you want to write any comment, you can simply write your
comment and just run it as markdown (not as code)
• or you can use # followed by comment within the code.
• If you use double ## followed by statement and run it as markdown or
simply write the statement and run it as heading, it will show you
comment as heading.
Comments and Headings
Comments and Headings
Comments and Headings
Comments and Headings
Python as a calculator
• For numbers:
• It is worth noting here
that the number should
be type compatible
otherwise it will throw
error. For e.g., if you
divide two numbers, it
will be a float value which
can’t be used with int
operands for performing
calculations. You will have
to convert it to
compatible type first.
Python as a calculator
Output :
Elif statement
The elif statement allows you to check multiple
expressions for TRUE and execute a block of code as
soon as one of the conditions evaluates to TRUE.
Example:
Lists
P1:
P2:
Concatenation and Repetition
P3:
P4:
Sample Program
P5:
String Slicing
● A segment of a string is called a slice.
● [n:m] returns the part of the string from the nth character to the mth character, including the
first but excluding the last.
P6:
String Slicing
P8:
P9:
Immutable Strings
● You cannot change an existing string.
● String objects does not support the item assignment.
greeting = ‘Hello, World!’
greeting[0] = ‘J’ #This will give TypeError
Immutable String
● The best way you can do is create a new string that is a variation on the original string.
P10:
String Methods
● A method is similar to a function - it takes arguments and returns a value - but the syntax is
different.
P11:
P12:
String Methods
P13:
P14:
In Operator
● The word in is a boolean operator that takes two strings and return True if first appears as a
substring in the second.
P15:
P16:
Question:
* What do we use to define a block of code in Python language?
a. Key
b. Brackets
c. Indentation
d. None of these
Another one →
*Which character is used in Python to make a single line comment?
a. /
b. //
c. #
d. !
*What will be the value of x when code executes successfully
#code:
x=0 a) 0
a=5
b=5
if a > 0:
if b < 0: b) 2
x=x+5
elif a > 5:
x=x+4
else: c) 5
x=x+3
else:
x=x+2
print(x) d) 3