1. Data Types in Python
Data types are used to identify what kind of value a variable stores. In Python we can store different types of data such as numbers text and True or False values. Python automatically identifies the data type based on the value assigned to a variable.
Example:
name = "Rajasekar"
age = 25
price = 99.50
is_student = True
print(type(name))
print(type(age))
print(type(price))
print(type(is_student))
Output:
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
Here str represents text int represents whole numbers float represents decimal numbers and bool represents True or False values.
2. Integer (int)
An integer is a data type used to store whole numbers without decimal points. It can contain positive numbers, negative numbers and zero.
Example:
age = 25
temperature = -10
number = 0
print(age)
print(temperature)
print(number)
Output:
25
-10
0
Here 25 is a positive integer -10 is a negative integer and 0 is also an integer.
3. Float
A float is used to store numbers that contain decimal points. It is useful when working with prices, measurements and percentages.
Example:
price = 99.50
height = 5.8
percentage = 85.5
print(price)
print(height)
print(percentage)
Output:
99.5
5.8
85.5
4. String (str)
A string is used to store text, characters and words. In Python we can create strings using single quotes or double quotes.
Example:
name = "Rajasekar"
city = 'Chennai'
print(name)
print(city)
Output:
Rajasekar
Chennai
Strings are commonly used to store names, addresses, messages and other text information.
5. Boolean (bool)
Boolean is a data type that stores only two values: True or False. It is mainly used in conditions and decision making.
Example:
is_logged_in = True
is_admin = False
print(is_logged_in)
print(is_admin)
Output:
True
False
When a user enters the correct password a program can return True. If the password is incorrect it can return False.
6. Operators in Python
Operators are special symbols or keywords used to perform operations on values and variables. Python provides different types of operators, such as arithmetic, comparison, logical, assignment and membership operators.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication and division.
Example:
a = 10
b = 3
print(a + b) # Addition
print(a - b) # Subtraction
print(a * b) # Multiplication
print(a / b) # Division
print(a // b) # Floor Division
print(a % b) # Modulus
print(a ** b) # Exponentiation
Output:
13
7
30
3.3333333333333335
3
1
1000
The / operator performs normal division and returns a float. The // operator performs floor division and returns the quotient rounded down. The % operator returns the remainder after division while ** is used for exponentiation.
Comparison Operators
Comparison operators are used to compare two values. They return either True or False.
Example:
a = 10
b = 5
print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)
Output:
False
True
True
False
True
False
Logical Operators
Logical operators are used to combine multiple conditions. Python provides three main logical operators: and or and not.
Example:
age = 25
print(age > 18 and age < 30)
print(age > 30 or age == 25)
print(not age > 18)
Output:
True
True
False
Assignment Operators
Assignment operators are used to assign values to variables and update their values.
Example:
number = 10
number += 5
print(number)
number -= 2
print(number)
number *= 2
print(number)
Output:
15
13
26
Membership Operators
Membership operators are used to check whether a value exists inside a sequence, such as a string, list or tuple. Python provides in and not in.
Example:
name = "Python"
print("P" in name)
print("Java" not in name)
Output:
True
True
Negative Indexing
Indexing is used to access individual elements from a string or sequence. Python supports both positive and negative indexing.
Positive indexing starts from 0 on the left side while negative indexing starts from -1 on the right side.
Example:
name = "Python"
print(name[0])
print(name[1])
print(name[-1])
print(name[-2])
Output:
P
y
n
o
Here name[0] accesses the first character while name[-1] accesses the last character. Negative indexing is useful when we want to access elements from the end without calculating their positions.
Slicing in Python
Slicing is used to extract a portion of a string or sequence. Instead of accessing only one character we can access multiple characters using slicing.
The syntax is:
variable[start:end:step]
Here start represents the starting position end represents the stopping position (the end position is not included) and step determines how many positions to move each time.
Example:
name = "Python"
print(name[0:3])
print(name[1:4])
print(name[:4])
print(name[::2])
print(name[::-1])
Output:
Pyt
yth
Pyth
Pto
nohtyP
Slicing is useful when we want to extract specific parts of a string skip characters or reverse a sequence.
Length (len) in Python
The len() function is used to find the total number of elements in a sequence such as a string, list or tuple. When used with a string it returns the total number of characters including spaces.
Example:
name = "Python"
print(len(name))
Output:
6
Here the word Python contains six characters so the len() function returns 6.
Step in Python
The step value is used in slicing to control how many positions Python moves when accessing elements. A positive step moves from left to right while a negative step moves from right to left.
Example:
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
print(numbers[::2])
print(numbers[::3])
print(numbers[::-1])
Output:
[1, 3, 5, 7]
[1, 4, 7]
[8, 7, 6, 5, 4, 3, 2, 1]
Here 2 skips one element between selected elements 3 moves three positions at a time and -1 reverses the sequence.
Top comments (0)