0% found this document useful (0 votes)
2 views20 pages

Module 2 PythonIntroduction Trunc

The document outlines the fundamental actions in Python, emphasizing the creation and modification of data, as well as interaction with the environment. It details types of actions, including expressions and statements, and introduces basic data types such as numbers, booleans, and container types like strings, lists, and dictionaries. Additionally, it covers mutability, accessing elements in sequential containers, and string representation in Python.

Uploaded by

sivaprasathb603
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)
2 views20 pages

Module 2 PythonIntroduction Trunc

The document outlines the fundamental actions in Python, emphasizing the creation and modification of data, as well as interaction with the environment. It details types of actions, including expressions and statements, and introduces basic data types such as numbers, booleans, and container types like strings, lists, and dictionaries. Additionally, it covers mutability, accessing elements in sequential containers, and string representation in Python.

Uploaded by

sivaprasathb603
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

Python

Actions in Python:

An algorithm must be expressed in Python as a sequence of


“actions”

Purpose of actions:

• Creating or modifying data: These actions take in data, perform


sequential, conditional, or repetitive execution on the data and
produce other data.

• Interacting with the environment: Our solutions will usually


involve interacting with the user or the peripherals of the
computer to take in (input) data or take out (output) data.

1
Python
Types of actions:

Expression: An expression (e.g., 3 + 4 * 5) specifies a calculation


which, when evaluated (performed), yields some data as a result.

An expression can consist of:

• basic data (integer, floating point, Boolean, etc.) or


container data (e.g., string, list, set, etc.).

• expressions involving operations among data and other


expressions.

• functions acting on expressions.

2
Python
Types of actions:

Statement: Unlike an expression, a statement does not return data


as a result. It can either be basic or compound.

• Basic statement: A basic statement can be, e.g., for


storing the result of an expression in a memory location
(an assignment statement for further use in subsequent
actions), deleting an item from a collection of data, etc.
Each statement has its special syntax that generally
involves a special keyword.

• Compound statement: Compound statements are


composed of statements, and control the execution of
those statements in some way.

3
Python
Naming and Printing Data

Variables: Name to data and use that name to access it

Printing data: Python provides the print() function to display data


items on the screen:

4
Python
Basic data in Python: Numbers
 Integers (e.g., 73)
 Floating point numbers (e.g., 73., 6.8e-6)
 Complex numbers (e.g., 2-5j)

From Python version 3, integers do not have fixed-size representation,


and their size is only limited by available memory

5
Python
Basic data in Python: Numbers

As for the float type, Python uses the 64-bit IEEE 754 standard which
can represent numbers in the range
[2.2250738585072014E-308, 1.7976931348623157E+308].

6
Python
Basic data in Python: Numbers

abs(<Number>): Absolute value of the number

pow(<Number1>, <Number2>): Power

7
Python
Basic data in Python: Numbers

round(<FloatNumber>): Rounds the floating point number to the


closest integer

Functions from the math library: sqrt(), sin(), cos(), log(), etc

8
Python
Basic data in Python: Booleans

Python provides the bool data type which allows only two values:
True and False
 Comparison operations
 Logical operations
• 0 (the integer zero)
• 0.0 (the floating-point number zero)
• "" (the empty string)
• [] (the empty list)
• {} (the empty dictionary)
are interpreted as False.

9
Python
Basic data in Python: Booleans

With Boolean values, we can use the “not” (negation or inverse),


“and”, and “or” operations.

10
Python
Container data in Python: (str, tuple, list, dict, set)

String (str): A string can hold a sequence of characters or only a


single character. A string cannot be modified after creation.

List (list): A list can hold ordered sets of all data types in Python
(including another list). The elements of a list can be modified after
creation.

Tuple (tuple): The tuple type is very similar to the list type, but the
elements cannot be modified after creation (similar to strings).

Dictionary (dict): A very efficient data type that implements a


mapping from a set of numbers, Booleans, strings, and tuples to
any set of data in Python. Dictionaries are easily modifiable and
extendable

11
Python
Container data in Python: (str, tuple, list, dict, set)

Mutability Versus Immutability

 Some container types are created as “frozen”. After creating


them, you can completely destroy them, but you cannot change
or delete their individual elements. This is called immutability.

 Strings and tuples are immutable, whereas lists, dictionaries,


and sets are mutable. With a mutable container, it is possible to
add new elements and change or delete existing ones.

12
Python
Accessing Elements in Sequential Containers

string, list, and tuple, are called sequential containers

Index starts from 0 in Python

13
Python
Accessing Elements in Sequential Containers

string, list, and tuple, are called sequential containers

14
Python
Accessing Elements in Sequential Containers

Number of elements: For all containers, len() is a built-in function


that returns the count of elements in the container that is given as
argument to it

Concatenation: String, tuple, and list data types can be combined


using the “+” operation

15
Python
Accessing Elements in Sequential Containers

Repetition: String, tuple, and list data types can be repeated using
the “*” operation

Membership: All containers can be checked for whether they


contain a certain item as an element using in and not in operations

16
Python
String in Python
 A string is denoted by enclosing the character sequence
between a pair of quotes (’Example String’) or double quotes
("Example String").

 A string surrounded with triple double quotes ("""Example


String""") allows you to have any combination of quotes and line
breaks within a sequence and Python will still view it as a single
entity.

"Hello World!"
’Hello World!’
’He said: "Hello World!" and walked towards the house.’
"A"
"""
Andrew said:
"Come here, doggy".
The dog barked in reply: ’woof’
17
"""
Python
String in Python

 The backslash (\) is a special character in Python strings, also


known as the escape character. It is used in representing
certain, the so-called, unprintable characters: \t is a tab, \n is a
newline,etc

 For example, \’ is the single quote character. ’It\’s raining’


therefore is a valid string and equivalent to "It’s raining".

18
Python
String in Python

19
Python
Examples with string in Python

20

You might also like