Sets,
re module(findall,search,split,match),
if,
elif
Getting input from user,
Identity Operators
Python Basics and Concepts
1. Sets in Python
A set is an unordered collection of unique elements. It does not allow duplicate
values and is defined using {} or the set() function.
Example 1: Creating a Set
numbers = {1, 2, 3, 4, 5}
print(numbers)
Output:
{1, 2, 3, 4, 5}
Example 2: Adding Elements
[Link](6)
print(numbers)
Output:
{1, 2, 3, 4, 5, 6}
Example 3: Removing Elements
[Link](3)
print(numbers)
Output:
{1, 2, 4, 5, 6}
Example 4: Set Operations
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print([Link](set2)) # {1, 2, 3, 4, 5}
print([Link](set2)) # {3}
print([Link](set2)) # {1, 2}
2. re Module (Regular Expressions)
The re module is used for pattern matching in strings.
Example 1: findall()
import re
text = "Python is fun. Python is powerful."
matches = [Link]("Python", text)
print(matches)
Output:
['Python', 'Python']
Example 2: search()
result = [Link]("fun", text)
print([Link]())
Output:
10
Example 3: split()
words = [Link]("\s", text)
print(words)
Output:
['Python', 'is', 'fun.', 'Python', 'is', 'powerful.']
Example 4: match()
result = [Link]("Python", text)
print(bool(result))
Output:
True
3. if Statement
The if statement executes a block of code if a condition is True.
x = 10
if x > 5:
print("x is greater than 5")
Output:
x is greater than 5
4. elif Statement
The elif (else-if) statement allows multiple conditions.
age = 20
if age < 18:
print("You are a minor")
elif age >= 18 and age < 60:
print("You are an adult")
else:
print("You are a senior citizen")
Output:
You are an adult
5. Getting Input from User
The input() function allows the user to enter data.
name = input("Enter your name: ")
print("Hello", name)
Input:
John
Output:
Hello John
6. Identity Operators (is and is not)
Identity operators check if two objects refer to the same memory location.
a = [1, 2, 3]
b=a
print(a is b) # True
Output:
True
x = [10, 20, 30]
y = [10, 20, 30]
print(x is not y) # True
Output:
True
Summary
Sets: Collections of unique elements, supporting operations like add(),
remove(), union(), etc.
re Module: Provides functions like findall(), search(), split(), and match()
for pattern matching.
if Statement: Executes a block of code if a condition is True.
elif Statement: Allows checking multiple conditions.
Getting Input: input() is used to get user input as a string, which can be
converted to int or float.
Identity Operators: is checks if two objects share the same memory
location, while is not checks if they don’t.