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

1.1introduction To Python

Uploaded by

jerathhrithik
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

1.1introduction To Python

Uploaded by

jerathhrithik
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 85

Introduction 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

• The Figure show the


difference between
simple division which
operates with single
slash and returns a
floating point number.
The floor division using
double slash discards
the decimal points in
the result of division.
Python as a calculator
The use of assigning
any value to a variable
that can be used later
on.
Use of underscore
If a variable is being used and it is not defined
earlier, it will simply throw an error. There is an
option to use the last printed value in python as it
becomes so interactive. The last printed value is
stored in an underscore variable Figure 15 and can be
used further.
Python as a calculator
• For strings:
• Strings can be stored in
either single quotes or
double quotes and a
forward slash “\” can be
used to escape quotes.
Figure shows some use
cases for strings. For
splitting the strings into
multiple lines, the new line
operator “\n” can be used.
The result varies if you
don’t use the print function
than if you use the print.
Python as a calculator
• Strings can be
concatenated with “+”
operator and repeated
with “*”. Two or more
strings literals which are
next to each other are
automatically
concatenated. This is
useful in breaking down
long strings. But it can
only be used with literals
not with variables. For
that we will have to use
“+” as shown in Figure 18
Python as a calculator
• String can be indexed staring
from 0. Indices may be negative
numbers also to strat counting
from the right. Since, -0 is same
as 0, the negative indexing starts
from -1 Figure 19. In addition to
indexing, slicing is also
supported. Indexing is used to
fetch the individual character at
the given index while slicing is
used to find the substrings. Slice
index have default values too.
The omitted first index results in
starting from 0 and omitted
second index results in default
to size of string Figure 21.
Python as a calculator
• Slicing
Python as a calculator
• Changing String
LOOPS
Loops are among the most basic and powerful concepts of programming. A loop in a computer
program is an instruction that repeats until a specified condition is reached.
● In a loop structure, the loop asks a question. If the answer requires action, it is
executed.
● Why to use loop?
Reason:A computer programmer who needs to use the same lines of code many
times in a program can use a loop to save time.
While loop
● A while loop is used to execute a block of statements repeatedly until a given condition is satisfied. And when
the condition becomes false, the line immediately after the loop in the program is executed.
For loop
● For Loops are used for sequential traversal.
● For example: traversing a list or string or array etc.
● In Python, there is no C style for loop, i.e., for (i=0; i<n; i++). There is “for in” loop which is similar to for each loop in
other languages.
For loop example:
Nested loops
● Python programming language allows to use one loop inside another loop.
If else statement
Python If-Else is an extension of Python If
statement where we have an else block that
executes when the condition is false.
Example

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

1. Multiple items in single variable.


2. Created using square brackets.
3. Items are ordered,changeable and allow duplicates.
Tuples

● Tuples are used to store multiple items in a single variable.


● A tuple is a collection which is ordered ,unchangeable and allow duplicate values.
● Tuples are written with round brackets.
1. CURLY
BRACKETS
2. SET
CONSTRUCTOR
LENGTH OF A SET EXAMPLE
HELPS TO SERVE AS KEY IN DICTIONARY
STRINGS
● A string is a sequence of characters.

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

You might also like