CSE1021- Python
CSE1021- Python
and Programming
UNIT II
PYTHON
DATA, EXPRESSIONS, STATEMENTS
Python is Interactive: You can actually sit at a Python prompt and interact with the
interpreter directly to write your programs.
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;
}
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,
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.
• 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
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.
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
Indexing
Concatenation
Slicing
Repetition
Membership
• Strings
• Lists
• Tuples
LISTS
Indexing
Concatenation
Slicing
Repetition
Membership
Updation-Deletion and Insertion
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
Indexing
Concatenation
Slicing
Repetition
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