KEMBAR78
L1 - Intro To Sequential Prog - Part2 | PDF | Boolean Data Type | Data Type
0% found this document useful (0 votes)
19 views29 pages

L1 - Intro To Sequential Prog - Part2

The document provides an overview of built-in data types in Python, including numeric types, sequence types, mapping types, set types, boolean types, binary types, and the None type. It also discusses arithmetic operators, the input() command, and steps for writing a computer program, along with examples of Python code for calculating area and volume. Additionally, it covers advanced topics such as f-string notation, integer division, and various operators like comparison, identity, and logical operators.

Uploaded by

mukhtam082
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)
19 views29 pages

L1 - Intro To Sequential Prog - Part2

The document provides an overview of built-in data types in Python, including numeric types, sequence types, mapping types, set types, boolean types, binary types, and the None type. It also discusses arithmetic operators, the input() command, and steps for writing a computer program, along with examples of Python code for calculating area and volume. Additionally, it covers advanced topics such as f-string notation, integer division, and various operators like comparison, identity, and logical operators.

Uploaded by

mukhtam082
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/ 29

Data Types in Python

❖ Python has the following built-in data types:

Numeric Types

1. Integers (int): whole numbers, e.g., 1, 2, 3, etc.

2. Floating Point Numbers (float): decimal numbers, e.g., 3.14, -0.5, etc.

3. Complex Numbers (complex): numbers with real and imaginary parts, e.g., 3 + 4j, -2 - 3j, etc.

Sequence Types

1. Strings (str): sequences of characters, e.g., "hello", 'hello', etc. Strings can be enclosed in single quotes or
double quotes.

2. Lists (list): ordered collections of items, e.g., [1, 2, 3], ["a", "b", "c"], etc.

3. Tuples (tuple): ordered, immutable collections of items, e.g., (1, 2, 3), ("a", "b", "c"), etc.

1
Mapping Type

1. Dictionaries (dict): unordered collections of key-value pairs, e.g., {"name": "John", "age": 30}, etc.

Set Types

1. Sets (set):is mutable and a unordered collections of unique items, e.g., {1, 2, 3}, {"a", "b", "c"}, etc.

2. Frozensets (frozenset): is an immutable version of sets in Python, called frozenset. A frozenset is created using the
frozenset() function, and it cannot be modified after it's created. e.g., frozenset([1, 2, 3]), etc.

Boolean Type

1. Boolean (bool): a logical value that can be either True or False.

Binary Types

1. Bytes (bytes): sequences of integers in the range 0 <= x < 256, e.g., b'hello', etc.

2. Bytearray (bytearray): mutable sequences of integers in the range 0 <= x < 256, e.g., bytearray(b'hello'), etc.

3. Memoryview (memoryview): a buffer interface for Python objects, e.g., memoryview(b'hello'), etc.

None Type

1. NoneType (NoneType): a special type that has only one value, None, which represents the absence of a value. 2
Note
The string, list, tuple, dictionary, and set data types
are called data structure in python. We will look at them
in more details in the subsequent lectures.

For now, let us look at Arithmetic operators

3
Python Arithmetic Operators

• Arithmetic operators are used to perform mathematical operations like addition,


subtraction, multiplication and division.

• There are 7 arithmetic operators in Python :


• Addition

• Subtraction

• Multiplication

• Division

• Modulus

• Exponentiation

• Floor division

4
5
6
7
8
9
The input() command
• The input() command in Python is a built-in function that allows the user to input
data, which can then be stored in a variable for further processing.

• Syntax:

The basic syntax of the input() function is as follows:

variable = input(prompt)

Where:

- variable is the name of the variable that will store the user's input.
- prompt is the message that will be displayed to the user, prompting them to enter input.

• We will study variables in our subsequent lectures:

10
Steps for writing a computer program

• The followings are five steps for writing a program


❖ Analyzing problems: Identifying the requirements and specifications of a
program.
❖ Designing solutions: Creating a detailed design of the program, including its
architecture, algorithms, and data structures.
❖ Writing code: Translating the design into a programming language, such as Java,
Python, or C++.
❖ Testing and debugging: Verifying that the program works correctly and fixing any
errors or bugs.
❖ Maintaining and updating: Ensuring that the program continues to work correctly
over time and making any necessary updates or modifications.

11
Example 1
• Write a program that calculate the area and the volume of a rectangular room.
• Step 1: Data requirements
✓ i. Determine what the program will do
computes and displays area & volume: area & volume.

✓ ii. Determine what the program requires in order to compute: area & volume?
To compute the area & volume of a rectangular, the program needs:
• Length of the rectangular room -- length
• Height of the rectangular room – height
• Breadth of the rectangular room- breadth
• iii. What is the relationship b/w output & input? The relationships are
• area = length * breadth
• volume = length * breadth* height

• Step 2: Design an algorithm for the problem


– input length
– input breadth
– Input height
– Calculate area
– Calculate volume.
– Print area
– Print volume

12
• Step 3: Implement the algorithm using a programing Language .
• The python implementation of the above algorithm is:
– length = float(input (‘Enter length : '))
– breadth = float(input ('Enter breadth : '))
– height = float(input ('Enter height: '))

– # computes the area


– area = length * breadth
– # computes the amount due the seller
– volume = area * height
– print ('The area is', area)
– print ('The volume is', volume )

13
The f string notation
• In Python, you can use the f string notation to format output in the print() function.
This is also known as f-strings.
• For example, consider the ff program:
name = "John"

age = 30
print(f"My name is {name} and I am {age} years old.")

• This will output:


My name is John and I am 30 years old.

• In this example, the f string notation is used to insert the values of the name and age
variables into the string.

14
f string notation continue
• Here are some more advanced examples:
• Float formatting: You can use the :.2f format specifier to format a float to two
decimal places.
float_num = 3.14159

print(f"The value of pi is {float_num:.2f}")

• This will output:


• The value of pi is 3.14

15
– Therefore, our Example 1 prints statements can
be written using the ff f-notation

– print ('The area is', area)


– print ('The volume is', volume )

– print (f“The area is {area:.2f}”)


– print (f”The volume is volume:.2f”)

16
Example 2

➢ After selling a house, an estate broker receives certain percentage of the sale
price as a commission. The seller receives the sale price minus the broker’s
commission and any expenses. Develop a program that computes and displays
the amount due the seller after settlement.
➢ Solution
➢ Step 1: Data requirements
✓ 1. Determine what the program will do
computes and displays exact amount due the seller: sell_amnt.

✓ 2. Determine what the program requires in order to compute sell_amnt?


To compute the sell_amnt, the program needs:
• How much the broker sold the house -- price
• Percentage of the sale price the broker will be given -- perCom
• The expenses incur while trying to sell the house -- expenses

17
• Below is the code

18
EXAMPLE4

• The area of a pentagon can be computed


using the following formula:


• Write a program that prompts the user to
enter the side (i.e s) of a pentagon and
displays its area.

19
Integer Division: //
• This operator returns an integer if the operands are integers. If they are floats, the result
will be a float with .0 as the decimal part because it truncates the decimal part.

• In Python, integer division, also referred to as floor division, involves discarding the
remainder and returning the whole number part of the quotient. To perform division, in
Python, you use two slashes (//).

• For instance, if you wish to divide two numbers and get the part of the outcome, you can
employ this syntax; quotient = dividend // divisor. In this case, the dividend represents the
number being divided, while the divisor is what it is being divided by.

• The resulting quotient will be an integer representing the whole number part of the
division.

Example

result = 7 // 3
In this case, the result of the integer division is 2, as 2 is the largest integer less than or equal
to the division of 7 by 3. The remainder of this division, which would normally be 1, is
discarded.

Example
result = 10 // 3
The result would be 3. In regular division, the result would be 3.333333, but with integer
division, only the whole number part, which is 3, is returned. 20
ADDITIONAL OPERATORS
▪ Comparison Operators

▪ Identity Operator

▪ Logical Operators

▪ Assignment Operators

▪ Conditional Statements in Python

21
Comparison Operator
In Python, we can use something called "comparison operator chaining" in
which we chain the comparison operators to make more than one
comparison more concisely.

22
Example 1
x=5
y=3

print(x == y)

# returns False because 5 is not equal to 3

Example 2
x=5
y=3

print(x >= y)

# returns True because five is greater, or equal, to 3

23
Identity Operator
Identity operators are used to compare the objects, not if they are equal,
but if they are actually the same object, with the same memory location:

Example 1
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x
print(x is z)
# returns True because z is the same object as x
print(x is y)
# returns False because x is not the same object as y, even if they have the same content
print(x == y)
# to demonstrate the difference between "is" and "==": this comparison returns True because x is equal to y
24
Example 2:
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x

print(x is not z)

# returns False because z is the same object as x

print(x is not y)

# returns True because x is not the same object as y, even if they have the same content

print(x != y)

# to demonstrate the difference between "is not" and "!=": this comparison returns False
because x is equal to y

25
Logical Operators
Logical operators are used to combine conditional statements allowing you to
perform operations based on multiple conditions:
There are three logical operators in Python: and, or, and not. Each one of these
operators has its own truth table and they are essential to work with conditionals.

26
27
Example 2:

The code checks if all variables a, b, and c


evaluate to True, printing a message
accordingly.

28
Logical AND operator Examples

29

You might also like