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

CSE1021- Python

Uploaded by

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

CSE1021- Python

Uploaded by

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

CSE1021- Problem Solving

and Programming
UNIT II
PYTHON
DATA, EXPRESSIONS, STATEMENTS

Python interpreter and interactive mode; values and types: int,


float, Boolean, string, and list; variables, expressions, statements,
tuple assignment, precedence of operators, comments; Modules
and functions, function definition and use, flow of execution,
parameters and arguments.
PYTHON - INTRODUCTION

Python is a general-purpose interpreted, interactive,


object-oriented, and high-level programming language.

It was created by Guido van Rossum during 1985- 1990.

Python got its name from “Monty Python’s flying circus”.

Python was released in the year 2000.


PYTHON - INTRODUCTION

Python is interpreted: Python is processed at runtime by the interpreter. You do


not need to compile your program before executing it.

Python is Interactive: You can actually sit at a Python prompt and interact with the
interpreter directly to write your programs.

Python is Object-Oriented: Python supports Object-Oriented style or technique of


programming that encapsulates code within objects.

Python is a Beginner's Language: Python is a great language for the beginner-level


programmers and supports the development of a wide range of applications
PYTHON - INTRODUCTION
INTERPRETER VS COMPILER

Interpreter: To execute a program in a high-level language by translating it one line at a time.

PYTHON

Compiler: To translate a program written in a high-level language into a low-level language all at once,
in preparation for later execution.

C, C++, Java
DOWNLOAD PYTHON
• https://www.python.org/downloads/
• https://www.jetbrains.com/pycharm/download/#section=windows
• Online Python Compiler
Basic Programming
C CODE: Hello.c

#include <stdio.h>
int main() {
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}

PYTHON CODE: Hello.py

print(‘Hello, World!’)
Basic Programming
C CODE: Addition.c

#include <stdio.h>
int main() {
PYTHON CODE: Addition.py
int number1=2, number2=3, sum;
num1 = 1.5
// calculating sum num2 = 6.3
sum = number1 + number2;
sum = num1 + num2
printf(“Total", sum);
return 0; # Display the sum
} print(‘total’, sum))
PYTHON INTERPRETER

Python Interpreter is a program that reads and executes Python code. It uses 2 modes of Execution.

1. Interactive mode

2. Script mode
INTERACTIVE MODE
Interactive Mode, as the name suggests, allows us to interact with OS. When we type Python statement,

interpreter displays the result(s) immediately.

Advantages:
Python, in interactive mode, is good enough to learn, experiment or explore.
Working in interactive mode is convenient for beginners and for testing small pieces of code.

Drawback:
We cannot save the statements and have to retype all the statements once again to re-run them.

In interactive mode, you type Python programs and the interpreter displays the result:
>>>1 + 1
>>> 2
INTERACTIVE MODE
• The chevron, >>>, is the prompt the interpreter uses to indicate that it
is ready for you to enter code.
• If you type 1 + 1, the interpreter replies 2.

>>> print ('Hello, World!')


Hello, World!
INTERACTIVE MODE
SCRIPT MODE
• In script mode, we type python program in a file and then use
interpreter to execute the content of the file.

• Scripts can be saved to disk for future use. Python scripts have the
extension .py, meaning that the filename ends with .py

• Save the code with filename.py and run the interpreter in script
mode to execute the script.
SCRIPT MODE

Interactive mode Script mode


A way of using the Python interpreter by A way of using the Python interpreter to
typing commands and expressions at the read and execute statements in a script.
prompt.
Cant save and edit the code Can save and edit the code
If we want to experiment with the code, If we are very clear about the code, we can
we can use interactive mode. use script mode.
we cannot save the statements for further we can save the statements for further use
use and we have to retype and we no need to retype
all the statements to re-run them. all the statements to re-run them.
We can see the results immediately. We cant see the code immediately.
VALUES AND DATA TYPES
Value
Value can be any letter ,number or string.
EX, Values are 2, 42.0, and 'Hello, World!'. (These values belong to different datatypes.)

Data type
Every value in Python has a data type. It is a set of values, and the allowable operations on those
values.
VALUES AND DATA TYPES
VALUES AND DATA TYPES
NUMBERS
 Number data type stores Numerical Values.

 This data type is immutable [i.e. values/items cannot be changed].

 Python supports integers, floating point numbers and complex numbers. They are defined as,
STRINGS
 A String in Python consists of a series or sequence of characters - letters,
numbers, and special characters.
 Strings are marked by quotes:
· single quotes (' '), Eg, 'This a string in single quotes'
· double quotes (" "), Eg, "This a string in double quotes“
· triple quotes (""“ """), Eg, This is a paragraph. It is made up of
multiple lines and sentences."""
 Individual character in a string is accessed using a subscript (index).
 Characters can be accessed using indexing and slicing operations
Strings are immutable i.e. the contents of the string cannot be changed
after it is created.
STRINGS

 Strings and Operations

Indexing
Concatenation
Slicing
Repetition
Membership

Live Examples …….


LISTS
 List is an ordered sequence of items. Values in the list are called
elements / items.
 It can be written as a list of terms (values) between square brackets[ ].
comma-separated i
 Items in the lists can be of different data types.
SEQUENCE
 A sequence is an ordered collection of items, indexed by positive integers.
 It is a combination of mutable (value can be changed) and immutable (values cannot be changed) data
types.
 There are three types of sequence data type available in Python, they are

• Strings
• Lists
• Tuples
LISTS

 Lists and Operations

Indexing
Concatenation
Slicing
Repetition
Membership
Updation-Deletion and Insertion

Live Examples …….


TUPLES
 A tuple is same as list, except that the set of elements is enclosed in
parentheses instead of square brackets.
 Tuple is an immutable list. i.e. once a tuple has been created, you can't add
elements to a tuple or remove elements from the tuple.

Benefit of Tuple:
 Tuples are faster than lists.
 If the user wants to protect the data from accidental changes, tuple can be
used.
 Tuples can be used as keys in dictionaries, while lists can't.
TUPLES

 Tuples and Operations

Indexing
Concatenation
Slicing
Repetition

Live Examples …….


Mapping and Dictionaries
 Unordered and Mutable data type

Dictionaries

 Lists are ordered sets of objects, whereas dictionaries are unordered sets.
 Dictionary is created by using curly brackets. i,e. {}
 Dictionaries are accessed via keys and not via their position.
 A dictionary is an associative array (also known as hashes). Any key of the dictionary is
associated (or mapped) to a value.
 The values of a dictionary can be any Python data type. So dictionaries are unordered key-
value-pairs (The association of a key and a value is called a key-value pair )

Dictionaries don't support the sequence operation of the sequence data types like strings, tuples
and lists.
DICTIONARIES

 Operations

Indexing
Slicing
Creating a Dictionary

Live Examples …….

You might also like