0% found this document useful (0 votes)
19 views17 pages

Python Basic Data Types and Operators

Uploaded by

priyanka62928
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views17 pages

Python Basic Data Types and Operators

Uploaded by

priyanka62928
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Basic data types in Python:

Python has several basic (or built-in) data types


that are used to store values. Here are the most
common ones:
1. Numeric Types
int: Integer values (e.g., 10, -3)

float: Floating-point (decimal) numbers (e.g.,


3.14, -0.5)

complex: Complex numbers (e.g., 2 + 3j)


2. Text Type
str: String (text) values (e.g., "hello", 'Python')

3. Boolean Type
bool: Boolean values (True or False)

4. Sequence Types
list: Ordered, mutable sequence (e.g., [1, 2, 3])
tuple: Ordered, immutable sequence (e.g., (1, 2,
3))
range: Sequence of numbers (e.g., range(5))
5. Set Types
set: Unordered collection of unique items (e.g.,
{1, 2, 3})

frozenset: Immutable version of a set


6. Mapping Type
dict: Key-value pairs (e.g., {"name": "Alice",
"age": 25})
7. None Type
NoneType: Represents the absence of a value
(None)

Example Data Type Try it


x = "Hello World" str
x = 20 int
x = 20.5 float
x = 1j complex
x = ["apple", "banana", "cherry"]list
x = ("apple", "banana", "cherry")tuple
x = range(6) range
x = {"name" : "John", "age" : 36} dict
x = {"apple", "banana", "cherry"} set
x = frozenset({"apple", "banana", "cherry"})
frozenset
x = True bool
x = None

Relational operators:
Relational operators in Python, also known as
comparison operators, are used to compare two
values and return a boolean result (True or
False).

Here’s a list of relational operators in Python


along with simple examples:

1. == (Equal to)

Checks if two values are equal.


x=5
print(x == 5) # True

2. != (Not equal to)

Checks if two values are not equal.

x=5
print(x != 3) # True

3. > (Greater than)

Checks if the left value is greater than the right.

x = 10
print(x > 7) # True

4. < (Less than)

Checks if the left value is less than the right.

x=4
print(x < 10) # True

5. >= (Greater than or equal to)

x=5
print(x >= 5) # True

6. <= (Less than or equal to)


x=3
print(x <= 4) # True

These operators return True or False and are


commonly used in conditional statements like if,
while, and for.

Expressions in python:
In Python, expressions are combinations of values,
variables, operators, and function calls that are
evaluated to produce a result. and they can be
broadly categorized into arithmetic, relational,
logical, and assignment expressions.

1. Arithmetic Expressions:
These expressions involve mathematical
operations and operators like +, -, *, /, **, and
%.
Example: 2 + 3 * 4 (evaluates to 14).
Example: 10 / 2 (evaluates to 5.0).
Example: 5 ** 2 (evaluates to 25).
2. Relational Expressions:
These expressions compare values using
operators like ==, !=, >, <, >=, and <=.
Example: 5 == 5 (evaluates to True).
Example: x > y (evaluates to True or False,
depending on the values of x and y).
Example: a != b (evaluates to True or False,
depending on the values of a and b).
3. Logical Expressions:
These expressions combine conditions using
logical operators like and, or, and not.
Example: x > 5 and y < 10 (evaluates to True if
both conditions are true).
Example: a == b or c != d (evaluates to True if at
least one condition is true).
Example: not (x > y) (evaluates to the opposite
of the condition x > y).
4. Assignment Expressions:
These expressions assign values to variables
using the = operator.
Example: x = 5 (assigns the value 5 to the
variable x).
Example: y = x + 2 (assigns the value of x + 2 to
the variable y).
5. Membership Expressions:
These expressions check if a value is present in a
sequence (like a list, tuple, or string) using the in
and not in operators.
Example: 5 in [1, 2, 3, 4, 5] (evaluates to True).
Example: "a" not in "banana" (evaluates to
False).
6. Conditional Expressions (Ternary Operator):
These expressions evaluate one of two values
based on a condition.
Example: result = "Even" if number % 2 == 0 else
"Odd" (assigns "Even" to result if number is
even, otherwise assigns "Odd").
7. Function Call Expressions:
Call functions or methods. These expressions
invoke a function and evaluate to the return
value of that function.
Example: len("Hello") (evaluates to 5, the length
of the string).
Example: print("Hello, world!") (prints "Hello,
world!" to the console).

Conditional statements in Python:


Conditional statements in Python allow us to
make decisions based on conditions. They
control the flow of our program by executing
different blocks of code depending on whether
a condition is True or False.
1. if Statement
Executes a block of code if the condition is true.

x = 10
if x > 5:
print("x is greater than 5")
2. if-else Statement
Executes one block if the condition is true, and
another if it's false.

x=3
if x > 5:
print("x is greater than 5")
else:
print("x is 5 or less")
3. if-elif-else Statement
Checks multiple conditions in sequence.

x=5

if x > 10:
print("x is greater than 10")
elif x == 5:
print("x is exactly 5")
else:
print("x is less than 10 and not 5")

4. Nested if Statements
A nested if statement is an if statement placed
inside another if (or elif) block. It allows you to
check a second condition only if the first
condition is true. This is useful when decisions
depend on multiple layers of conditions.
Syntax:

if condition1:
# Executes if condition1 is true
if condition2:
# Executes if both condition1 and
condition2 are true

Example:

num = int(input("Enter a number: "))


if num > 0:
if num % 2 == 0:
print("The number is positive and even.")
else:
print("The number is positive but odd.")
else:
print("The number is not positive.")

Here are some conditional statement examples


in Python that involve user input:

1. Check if a number is positive or negative

num = int(input("Enter a number: "))


if num > 0:
print("The number is positive")
elif num == 0:
print("The number is zero")
else:
print("The number is negative")

2. Check if a person is eligible to vote

age = int(input("Enter your age: "))

if age >= 18:


print("You are eligible to vote.")
else:
print("Sorry, you must be at least 18 to vote.")
3. Simple grade checker

marks = int(input("Enter your marks: "))

if marks >= 90:


print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 60:
print("Grade: C")
elif marks >= 40:
print("Grade: D")
else:
print("Grade: F (Fail)")

You might also like