[go: up one dir, main page]

0% found this document useful (0 votes)
16 views38 pages

Unit 1

Uploaded by

nikhilgruru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views38 pages

Unit 1

Uploaded by

nikhilgruru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Python

Programming
Basic Python to All

Presented By: Ms. Kumud Alok


Assistant Professor, CSE Dept.
WELCOME
TO THE
ND
2 YEAR
KUMUD ALOK, CSE DEPT. 2
COURSE OUTCOME
COs COURSE OUTCOME
CO1 Interpret the fundamental Python syntax and semantics and be fluent in the use
of Python control flow statements.
CO2 Express proficiency in the handling of strings and functions

CO3 Determine the methods to create and manipulate Python programs by utilizing
the data structures like lists, dictionaries, tuples and sets.
CO4 Identify the commonly used operations involving file systems and regular
expressions.
CO5 Articulate the Object-Oriented Programming concepts such as encapsulation,
inheritance and polymorphism as used in Python

KUMUD ALOK, CSE DEPT. 3


SYLLABUS
UNIT TOPICS

1 Introduction to Python: Python variables, Python basic Operators, Understanding


python blocks. Python Data Types, Declaring and using Numeric data types: int, float etc.

2 Python Program Flow Control Conditional blocks: if, else and else if, Simple for loops
in python, For loop using ranges, string, list and dictionaries. Use of while loops in python,
Loop manipulation using pass, continue, break and else. Programming using Python
conditional and loop blocks.

3 Python Complex data types: Using string data type and string operations, Defining list
and list slicing, Use of Tuple data type. String, List and Dictionary, Manipulations Building
blocks of python programs, string manipulation methods, List manipulation. Dictionary
manipulation, Programming using string, list and dictionary in-built functions. Python
Functions, Organizing python codes using functions.

KUMUD ALOK, CSE DEPT. 4


SYLLABUS

UNIT TOPICS

4 Python File Operations: Reading files, Writing files in python, Understanding read
functions, read(), readline(), readlines(). Understanding write functions, write() and
writelines() Manipulating file pointer using seek Programming, using file operations.

5 Python packages: Simple programs using the built-in functions of packages matplotlib,
numpy, pandas etc. GUI Programming: Tkinter introduction, Tkinter and
PythonProgramming, Tk Widgets, Tkinter examples. Python programming with IDE.

KUMUD ALOK, CSE DEPT. 5


INTRODUCTION

KUMUD ALOK, CSE DEPT. 6


PROGRAM VS PROGRAMMING

A sequence of instructions that designate how to execute a


PROGRAM
computation

Taking a task and writing it down in a programming language


PROGRAMMING
that the computer can understand and execute

KUMUD ALOK, CSE DEPT. 7


WHY PYTHON?
Open-source means it is free. Python has a large and active
scientific community with access to the software’s source code
OPEN SOURCE
and contributes to its continuous development and upgrading,
depending on users’ needs.

There is a broad set of fields where Python could be


GENERAL-
applied – web programming, analysis of financial data,
PURPOSE
analysis of big data, and more.

High-level languages employ syntax a lot closer to human


HIGH LEVEL logic, which makes the language easier to learn and
implement

KUMUD ALOK, CSE DEPT. 8


WHY PYTHON IS POPULAR?

Easy-to-learn programming language designed to be highly


readable, with a syntax quite clear and intuitive.

Its user-friendliness does not take away from its strength. Python can
execute a variety of complex computations and is one of the most
powerful programming languages preferred by specialists.

KUMUD ALOK, CSE DEPT. 9


FEATURES OF PYTHON

1. High-level powerful programming language


2. Interpreted language
3. Object-oriented
4. Portable
5. Easy to learn & use
6. Open source
7. Derived from many other programming languages

KUMUD ALOK, CSE DEPT. 10


PYTHON EDITORS

KUMUD ALOK, CSE DEPT. 11


WHY JUPYTER?
The Jupyter Notebook App is a server-client application that allows you to edit your code through
a web browser
➢ Language kernels are programs designed to read and execute code
in a specific programming language, like Python, R, or Julia. The
Jupyter installation always comes with an installed Python kernel, and
the other kernels can be installed additionally
➢ The Interfaces, where you can write code, represent the clients. An
example of such a client is the web browser

➢ The Jupyter server provides the environment where a client is


matched with a corresponding languages kernel. In our case, we will
focus on Python, and a web browser as a client

KUMUD ALOK, CSE DEPT. 12


JUPYTER INTERFACE- THE DASHBOARD
▪ As soon as you load the notebook, the Jupyter dashboard opens. Each file and directory has a check box next to it.
By ticking and unticking an item, you could manipulate the respective object – that means you can duplicate or
shutdown a running file.

▪ From the Upload button in the top-right corner, you can upload a notebook into the directory you are in. You can
expand the New button. From the list that falls, you will most likely need to create a new text file, a new folder, or a
new notebook file

KUMUD ALOK, CSE DEPT. 13


COMPILER VS INTERPRETER
• Many languages compile (translate) ▪ Python is directly interpreted into
the program into a machine machine instructions.
understandable form.

compile execute interpret


----------> ----------> >

Source code Intermediate code Output Source code Output

KUMUD ALOK, CSE DEPT. 14


COMPILING AND INTERPRETING (2)
Compiler Interpreter
Scans the entire program and translates it Translates program one statement at a time.
as a whole into machine code.
It takes large amount of time to analyze the It takes less amount of time to analyze the source
source code but the overall execution time is code but the overall execution time is slower.
comparatively faster.
Generates intermediate object code which No intermediate object code is generated,
further requires linking, hence requires hence are memory efficient.
more memory.
It generates the error message only after Continues translating the program until the first
scanning the whole program. Hence error is met, in which case it stops. Hence
debugging is comparatively hard. debugging is easy.
Programming language like C, C++ use Programming language like Python, Ruby use
compilers. interpreters.

KUMUD ALOK, CSE DEPT. 15


------------

BASIC SYNTAX

KUMUD ALOK, CSE DEPT. 16


Interactive Mode Programming VS Script
Mode Programming
Interactive Mode Programming Script Mode Programming
Invoking the interpreter without passing a script Invoking the interpreter with a script parameter, &
file as a parameter brings up the Python prompt. begins execution of the script and continues until
Type a python statement at the prompt and press the script is finished. When the script is finished,
Enter. the interpreter is no longer active (assuming that
user have Python interpreter set in PATH
variable).
C:\Users\moitra>python G:\2.
Python 2.7.13 (v2.7.13:a06454b1afa1, DOCUMENTS\PythonPrograms>pytho
Dec 17 2016, 20:42:59) [MSC v.1500 n test.py
32 bit ( Hello World
Intel)] on win32
Type "help", "copyright", "credits" or "license"
for more information.
>>> print("Hello World")
Hello World

KUMUD ALOK, CSE DEPT. 17


Identifiers
• A Python identifier is a name used to identify a variable,
function, class, module or other object.
• An identifier starts with a letter A to Z or a to z or an
underscore (_) followed by zero or more letters, underscores
and digits (0 to 9).
• Python does not allow punctuation characters such as @, $,
and % within identifiers.
• Python is a case sensitive programming language.
Manpower and manpower are two different identifiers in
Python.

KUMUD ALOK, CSE DEPT. 18


RESERVED WORDS

KUMUD ALOK, CSE DEPT. 19


LINES AND INDENTATION
• Python provides no braces to indicate blocks of code for class and
function definitions or flow control. Blocks of code are denoted by line
indentation.
• The number of spaces in the indentation is variable, but all
statements within the block must be indented the same amount.

Correct Erroneous
if True: if True:
print "True" else: print "Answer" print
print "False" "True"
else:
print "Answer" print
"False"

KUMUD ALOK, CSE DEPT. 20


COMMENTS
Example

Multi-Line Statements total = item_one + \


item_two + \
item_three

Comments in Python print "Hello World" # comment

Multiple Statements on a Single Line x = 'foo'; print(x + '\n')

KUMUD ALOK, CSE DEPT. 21


VARIABLES

One of the main concepts in programming is variables. They are your best friends. You will deal
with them all the time. You will use them to store information. They will represent your data input.

Variable names can be as long as


you like. They can contain both
letters and numbers, but they
can’t begin with a number. It is
legal to use uppercase letters, but it
is conventional to use only lower
case for variables names

Standard Data Types


Other Operations
Some Type Conversions

KUMUD ALOK, CSE DEPT. 22


STANDARD DATA TYPE

Standard Data Type Example

counter = 100
Numbers
miles = 1000.0

String name = "John"

List tinylist = [123, 'john']

Tuple tinytuple = (123, 'john')

tinydict = {'name': 'john','code':6734, 'dept':


Dictionary
'sales'}

KUMUD ALOK, CSE DEPT. 23


OTHER OPERATIONS
Operation Example
a=b=c=1
Multiple Assignment
a,b,c = 1,2,"john"
Delete del var1[,var2[,var3[....,varN]]]]

Some Type Conversions


Function Description
int(x) Converts x to an integer number
str(x) Converts x to a string representation
list(s) Converts s to a list
tuple(s) Converts s to a tuple
dict(d) Creates a dictionary d
…..

KUMUD ALOK, CSE DEPT. 24


BASIC OPERATORS
Type Operators

+, -, *, /, %, ** (exponent), // (floor
Arithmetic Operators
division)

Relational Operators ==, !=, >, <, >=, <=

Assignment Operators =, +=, -=, *=, /=, %=, **=, //=

Logical Operators and, or, not

Bitwise Operators &, |, ~, ^, <<, >>

Membership Operators in, not in

Identity Operators is, not is

KUMUD ALOK, CSE DEPT. 25


PYTHON NUMBERS

Number data types store numeric values. Number objects are


created when you assign a value to them. For example:

KUMUD ALOK, CSE DEPT. 26


Del statement

You can also delete the reference to a number object


by using the del statement. The syntax of the del
statement is −

KUMUD ALOK, CSE DEPT. 27


BASIC DATA TYPES
Python supports three different numerical types −

A. int (signed integers)


B. float (floating point real values)
C. complex (complex numbers)

KUMUD ALOK, CSE DEPT. 28


PYTHON STRINGS

❖ Strings in Python are identified as a contiguous set of characters represented in


the quotation marks.

❖ Python allows either pair of single or double quotes.

❖ Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes
starting at 0 in the beginning of the string and working their way from -1 to the end

KUMUD ALOK, CSE DEPT. 29


STRING OPERATORS

The plus (+) sign is the string concatenation operator


The asterisk (*) is the repetition operator.

KUMUD ALOK, CSE DEPT. 30


LIST

❖ Lists are the most versatile of Python's compound data types. A list contains items
separated by commas and enclosed within square brackets ([]).

❖ To some extent, lists are similar to arrays in C. One of the differences between
them is that all the items belonging to a list can be of different data type.

❖ The values stored in a list can be accessed using the slice operator ([ ] and [:]) with
indexes starting at 0 in the beginning of the list and working their way to end - 1.

❖ The plus(+) sign is the list concatenation operator, and the asterisk (*) is the
repetition operator.

KUMUD ALOK, CSE DEPT. 31


KUMUD ALOK, CSE DEPT. 32
TUPLE

❖ A tuple is another sequence data type that is similar to the list.

❖ A tuple consists of a number of values separated by commas. Unlike


lists, however, tuples are enclosed within parenthesis.

❖ The main difference between lists and tuples is- Lists are enclosed
in brackets ( [ ] ) and their elements and size can be changed, while
tuples are enclosed in parentheses ( ( ) )and cannot be updated.
Tuples can be thought of as read-only lists.

KUMUD ALOK, CSE DEPT. 33


KUMUD ALOK, CSE DEPT. 34
DICTIONARY

Python's dictionaries are kind of hash-table type. They work like associative
arrays or hashes found in Perl and consist of key-value pairs.

A dictionary key can be almost any Python type, but are usually numbers or
strings. Values, on the other hand, can be any arbitrary Python object.

Dictionaries are enclosed by curly braces ({ }) and values can be assigned


and accessed using square braces ([]).

Dictionaries have no concept of order among the elements. It is incorrect to


say that the elements are "out of order"; they are simply unordered.

KUMUD ALOK, CSE DEPT. 35


KUMUD ALOK, CSE DEPT. 36
SETS

A set is a well-defined collection of distinct objects,


typically called elements or members.
Python’s built-in set type has the following
characteristics:
❑Sets are unordered.
❑Set elements are unique. Duplicate elements are
not allowed.
❑A set itself may be modified, but the elements
contained in the set must be of an immutable type.
KUMUD ALOK, CSE DEPT. 37
KUMUD ALOK, CSE DEPT. 38

You might also like