Python Day- 3
Python Day- 3
Expressions in Python
Definition:
An expression is a combination of values, variables, operators, and function calls that the
Python interpreter can evaluate to produce a result.
Types of Expressions:
Examples of Expressions:
# Constant Expression
result = 10 + 20
print(result) # Output: 30
# Arithmetic Expression
x = 15
y=5
result = (x + y) * 2
print(result) # Output: 40
# Logical Expression
is_valid = True
is_complete = False
print(is_valid and not is_complete) # Output: True
# Relational Expression
a = 10
b = 20
print(a < b) # Output: True
# Bitwise Expression
x = 5 # Binary: 0101
y = 3 # Binary: 0011
print(x & y) # Output: 1 (Binary: 0001)
# String Expression
greeting = "Hello" + " World"
print(greeting) # Output: Hello World
Strings in Python
● Strings in Python are sequences of characters enclosed in single ('), double ("), or triple
quotes (''' or """).
● Strings are immutable, meaning they cannot be changed after creation.
Examples of Strings:
# Single-quoted string
greeting = 'Hello'
# Double-quoted string
name = "Alice"
● Strings are indexed from 0 to n-1 (forward indexing) and -1 to -n (reverse indexing).
# Forward indexing
print(text[0]) # Output: P (1st character)
print(text[3]) # Output: h (4th character)
# Reverse indexing
print(text[-1]) # Output: n (last character)
print(text[-3]) # Output: t (3rd last character)
String Slicing:
# Basic slicing
print(text[0:6]) # Output: Python (characters from index 0 to 5)
print(text[:6]) # Output: Python (same as above)
print(text[7:]) # Output: Programming (from index 7 to end)
# Using step
print(text[0 : 12 : 2]) # Output: Pto rg (every 2nd character from index 0 to 11)
# Reverse slicing
print(text[ : : -1]) # Output: gnimmargorP nohtyP (reverses the string)
String Formatting:
name = "Alice"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
# Output: My name is Alice and I am 25 years old.
name = "Bob"
age = 30
print(f"My name is {name} and I am {age} years old.")
# Output: My name is Bob and I am 30 years old.
name = "Eve"
age = 22
print("My name is %s and I am %d years old." % (name, age))
# Output: My name is Eve and I am 22 years old.
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # Output: Hello World
1. Repetition: Repeating a string using the * operator.
Examples:
text = " Hello Python "
words = text.split()
print(words) # Output: ['Hello', 'Python']
joined = "-".join(words)
print(joined) # Output: Hello-Python
Introduction to List
Definition
Key Properties
Syntax
# Creating a list
my_list = [1, 2, 3, "hello", 4.5]
Introduction to Tuple
Definition
● A tuple is an ordered, immutable (non-modifiable) collection of elements.
● Tuples can contain elements of different data types.
● Tuples are defined using parentheses ( ).
Key Properties
● Ordered: The elements have a specific order.
● Immutable: Once defined, elements cannot be changed.
● Allows duplicates: Multiple occurrences of the same value are allowed.
# Creating a tuple
my_tuple = (1, 2, 3, "hello", 4.5)
Introduction to Set
Definition
Key Properties
Syntax
# Creating a set
my_set = {1, 2, 3, 4}
Introduction to Dictionary
Definition
Key Properties
Syntax
# Creating a dictionary
my_dict = {"name": "Alice", "age": 25, "city": "New York"}
Practice Problems
1. Convert a string to uppercase and lowercase
text = "banana"
print(text.count("a")) # Output: 3
def get_initials(full_name):
names = full_name.split()
initials = "".join([name[0].upper() for name in names])
return initials
def count_vowels(text):
vowels = "aeiouAEIOU"
return sum(1 for char in text if char in vowels)
def reverse_words(sentence):
words = sentence.split()
reversed_sentence = " ".join(reversed(words))
return reversed_sentence
def is_palindrome(text):
text = text.replace(" ", "").lower()
return text == text[::-1]
word = "madam"
print(is_palindrome(word)) # Output: True
List of programs to practice (Home work)
9. Write a program to convert the string from upper case to lower case.
10. Write a program to convert the string from lower case to upper case.
11. Write a program to delete the all consonants from given string.
12. Write a program to count the different types of characters in given string.
13. Write a program to count number of words in a multi word string.
14. Write a program to sort the characters of a string.
15. Write a program for concatenation two strings.
16. Write a program to find the length of a string.
17. Write a program to find the length of a string without using string function.
18. Write a program which prints initial of any name (print RAM for RAM KUMAR).
19. Write a program to check whether a string is palindrome or not.
20. Write a program to sort given names in Lexicographical sorting (Dictionary order).