Python Basics
Python Data Types and Operators
Learning Objectives
By the end of this lesson, you will be able to:
Identify Python data types
Learn data assignment in Python
List the different operators in Python
Work with string functions
Business Scenario
A software development company is organizing a training session on Python data
types, operators, and string functions. The goal is to enhance the team's
understanding of these fundamental concepts and improve their ability to handle
diverse data, perform efficient operations, and manipulate strings effectively.
This training will equip the team with essential Python programming skills,
resulting in higher code quality, optimized performance, and increased
productivity when delivering exceptional software solutions to clients.
In this lesson, we will explore the following:
• Python data types
• Different types of operators and string functions in Python
Data Types and Data Assignment
Discussion
Data Types and Data Assignment
Are data types required in Python?
• What are the different data types and operators in Python?
• What are miscellaneous operators in Python, and how are they different
from other operators?
Data Types
Variables can store various kinds of data, each of which has a specific function.
Objects are used to represent data.
Aggregated Data
Scalar Data
• string: “abc” , ‘xyz’
• integer: 2323, 10 Types of
data • set: {2, 4, 6, ‘abc’}
• float: 32.3, 31.2e5
• list: [23, 9.8, ‘a’, ‘b’, ‘a’]
• boolean: True , False
• tuple: (34.5, ‘a’ , 45, ‘w’)
• complex: 2 + 6j
• dictionary: {‘a’ : 1 , ‘b’ : 3}
Note
The data type of any object can be checked by using the built-in function: type().
Data Assignment
Python variables are references to objects, but actual data is contained in the objects.
Here, x and y are pointing to the same Here, y is pointing to a new integer object with
memory location where 34 (an integer object) 78 as a value, and x is pointing to the previous
is stored. object with 34.
These references can be verified by using id() function
Operators in Python
Operators
The different types of operators are given below:
Arithmetic
operators
Miscellaneous
operator
Assignment
operators
Logical
operators
Comparison
operators
Arithmetic Operators
Different arithmetic operators are given below with an example:
Operator Description Example
>> x = 12 ; y = 50
+ Addition >> x + y
62
>> x = 45 ; y = 24
- Subtraction >> x - y
21
>> x = 50 ; y = 4
* Multiplication >> x * y
200
>> x = 50 ; y = 4
/ Division >> x / y
12.5
Arithmetic Operators
Different arithmetic operators are given below with an example:
Operator Description Example
>> x = 50 ; y = 4
% Modulo >> x % y
2
>> x = 50 ; y = 4
// Integer divide >> x // y
12
>> x = 5 ; y = 4
** Exponent >> x ** y
625
Assignment Operators
The equal to “=“ operator is used for data assignment in Python.
Example:
• Assignment operators can be combined with
arithmetic operators.
• Here, a += 5 is same as a = a + 5.
• Similar operators are: -= , *=, /=, %=, //=, and
**= .
Comparison Operators
Comparison operators are used to compare two values
Operator Description Example
Returns True when >> x = 20 ; y = 20
== the two values are >> x == y
equal. True
Returns True when the >> x = 45 ; y = 24
!= two values are not >> x != y
equal. True
Note
These operators return true or false based on the comparison.
Comparison Operators
Comparison operators are used to compare two values
Operator Description Example
Returns True when >> x = 20 ; y = 30
< first value is less than >> x < y
the second. True
Returns True when >> x = 20 ; y = 30
> first value is greater >> x > y
than the second. False
Note
These operators return true or false based on the comparison.
Comparison Operators
Comparison operators are used to compare two values
Operator Description Example
Returns True when >> x = 40 ; y = 30
first value is less than
<= >> x <= y
or equals to the
second. False
Returns True when >> x = 30 ; y = 30
first value is greater
>= >> x >= y
than or equals to the
second value. True
Note
These operators return true or false based on the comparison.
Logical Operators
Logical operators are used for combining conditional statements.
Operator Description
and Returns True if both statements are true
Returns True if one of the statements is
or
true
Reverses the results, returns False if the
not
result is true
Miscellaneous Operators
There are two kinds of miscellaneous operators, such as
identity and membership operators.
1. Identity Operators
Identity operators compare variables to see
whether they are the same object at the same
memory address.
• is: Returns True if both variables are the same
object
• is not: Returns True if both variables are not
the same object
Miscellaneous Operators
There are two kinds of miscellaneous operators, such as
identity and membership operators.
2. Membership Operators
Membership operators are used to testing if a
sequence is present in an object.
in: Returns True if a value is present in the object
not in: Returns True if a value is not present in the
object
Data Types and Data Assignment
Are data types required in Python?
• What are the different data types and operators in Python?
Answer: Variables can store various kinds of data, and objects are used to
represent the data, each of which has a specific function. The different types
of operators include arithmetic operators, miscellaneous operators,
assignment operators, logical operators, and comparison operators.
• What are miscellaneous operators in Python, and how are they different
from other operators?
Answer: There are two kinds of miscellaneous operators, that is, identity and
membership operators. The identity operators compare variables to see
whether they are the same object at the same memory address.
Strings in Python
Discussion
Strings
You have been given a sentence and have been asked to fetch a particular
word from it.
Take the text “Welcome to the course of Data Science from Simplilearn”. You
have been asked to use the string functions and print only the word
“Simplilearn”.
How will you achieve this? Also, specify if the indexing starts from 0 or 1.
Strings
Strings are a sequence of characters that can be either letters or alphanumeric.
Strings in python are enclosed in either single quotation marks or double quotation marks.
In the above example, the values of message_1 and message_2
are same:
Strings
Triple single quotes (‘’’... ‘’’) or triple double quotes (""" ... ""”) can be used to create multiline strings in Python.
In the above example, the values of message_1 and message_2
are similar.
Accessing Characters in Strings
Strings can be accessed by using subscripts or indexes. Indexing in Python starts with 0.
The characters of the string can be accessed as:
Explanation
0 1 2 3 4 5 6 7 8 9 10 11
H e l l o W o r l d !
The last character or reverse of the string can be -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
accessed as:
String Functions
String functions include:
Methods Description
Returns a string in sentence case, which means that the first letter is upper
capitalize
case, and the rest is lower case
lower Returns a copy of the string converted to lowercase
upper Returns a copy of the string converted to uppercase
strip Returns a copy of the string with leading and trailing whitespace removed
join Concatenate any number of strings
Strings are immutable Python objects.
All string methods return a new value and do not change the original string.
String
You have been given a sentence and have been asked to fetch a particular
word from it.
Take the text “Welcome to the course of Data Science from Simplilearn”. You
have been asked to use the string functions and print only the word
“Simplilearn”. How will you achieve this? Also, specify if the indexing starts
from 0 or 1.
Answer: Strings can be accessed by using subscripts or indexes. Indexing in
Python starts with 0.
File Handling in Python
File Handling in Python
File operations are fundamental actions performed on files, including creating, reading, writing, and
closing files.
These operations are crucial for data manipulation, storage, and transfer in programming and Python
provides built-in functions and methods for handling these tasks efficiently.
Creating and Opening a File
To open a file in Python, use the open() function. If the file does not exist, opening it in write (w) or
append (a) mode can create it:
Syntax:
file = open('example.txt', 'w')
Reading from a File
To read the entire content of a file, use the read() method:
Syntax:
content = file.read()
print(content)
To read one line at a time, use the readline() method, and for reading all lines into a list, use readlines():
Syntax:
line = file.readline()
print(line)
lines = file.readlines()
print(lines)
Writing to a File and Closing a File
Use the write() method to write a string to a file, and writelines() for a list of strings:
Syntax:
file.write('Hello, world!\n')
lines = ['First line\n', 'Second line\n']
file.writelines(lines)
It is crucial to close a file after your operations are complete to free up system resources:
Syntax:
file.close()
Key Takeaways
In Python, data is represented as objects.
Python supports different types of operators, such as
arithmetic, comparison, logical, and miscellaneous operators.
Strings are immutable objects in Python.
Strings can be accessed by using subscripts or indexes.
All string methods return a new value and do not change
the original string.
Knowledge Check
Knowledge
Check
Which of the following is NOT an arithmetic operator?
1
A. “+” Addition
B. “-” Subtraction
C. “\” Division
D. “|” Bitwise OR
Knowledge
Check
Which of the following is NOT an arithmetic operator?
1
A. “+” Addition
B. “-” Subtraction
C. “\” Division
D. “|” Bitwise OR
The correct answer is D
Bitwise “OR” operator is a type of logical operator that is used to compare binary numbers.
Knowledge
Check
__________ operators are used to test if a sequence is present in an object.
2
A. Membership
B. Logical
C. Comparison
D. All of the above
Knowledge
Check
__________ operators are used to test if a sequence is present in an object.
2
A. Membership
B. Logical
C. Comparison
D. All of the above
The correct answer is A
Membership operators are used to test if a sequence is presented in an object.
Knowledge
Check
String indexing in Python starts with _________.
3
A. 2
B. 1
C. 0
D. All of the above
Knowledge
Check
String indexing in Python starts with _________.
3
A. 2
B. 1
C. 0
D. All of the above
The correct answer is C
String indexing in Python starts with 0.
Thank You