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

Chapter 5

Uploaded by

BHARGAVI SHETTY
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)
18 views38 pages

Chapter 5

Uploaded by

BHARGAVI SHETTY
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

Chapter 5:

Getting Started with


Python
Features of Python
1. High-Level Language: Easy to understand and manage.
2. Free and Open Source: Accessible to everyone.
3. Interpreted: Executes code line by line.
4. Clear Syntax: Simple structure.
5. Case-Sensitive: Differentiates between uppercase and lowercase.
6. Portable and Platform-Independent: Runs on various operating systems.
7. Rich Library: Extensive predefined functions.
8. Web Development: Useful in building web services and applications.
9. Indentation: Uses indentation for code blocks.
Working with Python
DESCRIPTION PYTHON IDLE

•Requires a Python interpreter, available


on the official website.
•The interpreter shows a prompt (>>>),
ready for commands.
Execution Modes of Python
•Interactive Mode:
•Executes individual statements immediately. Useful for testing.
Execution Modes of Python
Script Mode: Allows writing multiple instructions in a file (with .py extension). Programs
can be executed by running the script file.
F5 function key is the shortcut to run the script.
Keywords

Reserved words with specific meanings to the Python Interpreter,


e.g., False, class, return, True, def, import.
Identifiers

•Names used to identify variables, functions, etc.

•Rules for Naming Identifier:


Start with a letter or underscore,
followed by letters, digits, or underscores.
Case-sensitive and
cannot use special symbols.
Variables
A variable in a program is uniquely identified by a #To find the area of a
name(identifier). rectangle
length = 10
Variable in Python refers to an object — an item or breadth = 20
element that is stored in the memory.
Value of a variable can be a string (e.g., ‘b’, ‘Global area = length * breadth
Citizen’),numeric (e.g., 345) or any combination of print(area)
alphanumeric characters (CD67).
We can use an assignment statement to create new Output:
variables and assign specific values to them.
200
Variable declaration is implicit in Python.
Variables must be assigned values before use.
Comments
#To find the sum of two numbers
num1 = 10
num2 = 20
result = num1 + num2

Used to add notes in the code, print(result)


ignored by the interpreter. Begin with #.
Every thing is an OBJECT in Python
•Python treats every value as an >>> num1 = 20
object, each with a unique identity.
>>> id(num1)
id() function returns the identity of an object.
1433920576 #identity of num1
>>> num2 = 30 - 10
• INTERNING: Python’s behaviour to reuse the
same object already available in the memory. >>> id(num2)

This is done for numeric values of range -5 to 256. 1433920576


#identity of num2 and num1 are same as both
Larger/Smaller numeric values are not interned.
# refers to object 20
String values are interned depending on the Context.
Data Type
Data Types
•Number: Stores numerical values, classified as int, float, complex.

•Boolean: Subtype of integer, consisting of True and False.

•Sequence: Ordered collection of items (Strings, Lists, Tuples).

•String: Group of characters enclosed in quotes.

•List: Sequence of items in square brackets.

•Tuple: Sequence of items in parentheses, immutable.

•Set: Unordered collection of unique items in curly brackets.


Number Type
String Type
String is a group of characters.

These characters may be alphabets, digits or special characters including spaces.

String values are enclosed either in single quotation marks (e.g., ‘Hello’) or in double quotation marks (e.g.,
“Hello”).

The quotes are not a part of the string, they are used to mark the beginning and end of the string for the
Interpreter.
List Type
List is a sequence of items separated by commas and the items are enclosed in square brackets [ ].
Tuple Type
Tuple is a sequence of items separated by commas
and items are enclosed in parenthesis ( ).
This is unlike list, where values are enclosed in
brackets [ ].
Once created, we cannot change the tuple.
Set Type
Set is an unordered collection of items separated by
commas and the items are enclosed in curly
brackets { }.
A set is similar to list, except that it cannot have
duplicate entries.
Once created, elements of a set cannot be changed.
None Type
None is a special data type with a single value.
It is used to signify the absence of value in a
situation.
None supports no special operations, and it is
neither same as False nor 0 (zero).
Mapping:
Dictionary
Mapping: Dictionary in Python holds data items in key-value
pairs.
Mapping is an unordered data type in Python.
Items in a dictionary are enclosed in curly brackets { }.
Currently, there is only one standard mapping data
type in Python called dictionary. Dictionaries permit faster access to data.
Every key is separated from its value using a colon (:)
sign.
The key : value pairs of a dictionary can be accessed
using the key.
The keys are usually strings and their values can
be any data type.

In order to access any value in the dictionary, we have


to specify its key in square brackets [ ].
Dictionary Type
Mutable and Immutable Data Types
•Mutable Types: Can be changed after creation
(e.g., lists, dictionaries, sets).
•Immutable Types: Cannot be changed after creation
(e.g., strings, tuples, ).
Mutability
Demo
Deciding usage of Python Data Type
Use lists when we need a simple iterable collection of data that may go for frequent modifications.
For example, if we store the names of students of a class in a list, then it is easy to update the list when some new
students join or some leave the course.

Tuples are used when we do not need any change in the data.
For example, names of months in a year.

When we need uniqueness of elements and to avoid duplicacy it is preferable to use sets,
for example, list of artefacts in a museum.

If our data is being constantly modified or we need a fast lookup based on a custom key or we need a logical
association between the key : value pair, it is advised to use dictionaries. A mobile phone book is a good
application of dictionary.
Operator
An operator is used to perform specific mathematical or logical operation on values.
The values that the operators work on are called operands.
Types of Operators:
1. Arithmetic Operators
2. Relational Operators
3. Assignment Operators
4. Logical Operators
5. Identity Operator
6. Membership Operators
Arithmetic
Operators
Python supports arithmetic operators that are
used to perform the four basic arithmetic
operations as well as modular division, floor
division and exponentiation.
Relational
Operators
Relational operator compares the
values of the operands on its either
side and determines the
relationship among them.
Assume the Python variables num1
= 10, num2
= 0, num3 = 10, str1 =
"Good", str2 =
"Afternoon" for the following
examples:
Assignment
Operators
Assignment operator assigns or changes the value of
the variable on its left.
Logical
Operators
There are three logical operators
supported by Python.
These operators (and, or, not)
are to be written in lower case only. The
logical operator evaluates to either True
or False based on the logical operands on
either side. Every value is logically either
True or False. By default, all values are
True except None, False, 0 (zero), empty
collections "", (), [], {}, and few other
special values. So if we say num1 =
10, num2 = -20, then both num1
and num2 are logically True.
Identity Operator
Identity operators are used to
determine whether the value of a
variable is of a certain type or
not. Identity operators can also
be used to determine whether
two variables are referring to the
same object or not. There
are two identity operators.
Membership Operators
Membership operators are
used to check if a value is a
member of the given
sequence or not.
Expression
DESCRIPTION EXAMPLES

An expression is defined as a combination of


constants, variables, and operators.
An expression always evaluates to a value.
A value or a standalone variable is also considered
as an expression but a standalone operator is not
an expression.
Precedence of Operator
Evaluation of the expression is based on precedence of operators.
When an expression contains different kinds of operators, precedence determines which operator should
be applied first.
Higher precedence operator is evaluated before the lower precedence operator.

Most of the operators studied till now are binary operators.


Binary operators are operators with two operands.
The unary operators need only one operand, and they have a higher precedence than the binary operators.
The minus (-) as well as + (plus) operators can act as both unary and binary operators,
not is a unary logical operator.
Precedence of Operators
Statement
In Python, a statement is a unit of code that the Python interpreter can execute.
Input & Output

•Input: input() function is used to take input from the user.

•Output: print() function is used to display output to the user.


Type Conversion

•Implicit Conversion: Automatic conversion of one data type to another.

•Explicit Conversion: Manual conversion using functions like int(), float(), str(), etc.
Debugging
The process of identifying and removing mistakes, also known as bugs or errors, from a program is called
debugging.

Types of Errors:
Syntax Errors: Errors in the structure of the code, such as missing colons, parentheses, or incorrect
indentation.
Runtime Errors: Errors that occur during program execution, such as division by zero or accessing a variable
that has not been defined.
Logical Errors: Errors in the logic of the code that produce incorrect results, even though the code runs
without crashing.
Chapter Blueprint

VSA SA LA E TOTAL

1 MARK 2 MARKS 3 MARKS 5 MARKS

2 MCQ 1 1 2 18 MARKS

1 FIB (1 HOT)

You might also like