Online Computer Courses Classes and Training Program

Python Basics: Numbers, Strings, Lists, Tuples, and Dictionary Explained with Examples



1. Numbers

Python supports different types of numbers:

  • Integers: Whole numbers, e.g., 5, -10
  • Floating-point numbers: Decimal numbers, e.g., 3.14, -0.001
  • Complex numbers: Represented as a + bj, e.g., 2 + 3j

Examples:

# Integers
a = 10
b = -5

# Floats
c = 3.14
d = -0.76

# Complex numbers
e = 2 + 3j
f = -1j

2. Strings

Strings are sequences of characters enclosed in single (') or double (") quotes.

Examples:

# Single or double quotes
name = 'Python'
greeting = "Hello, World!"

# Multiline strings
description = '''This is a 
multiline string.'''

# String operations
length = len(name)  # Length of the string
uppercase = name.upper()  # Convert to uppercase

3. Lists

Lists are ordered collections of items, which can be of mixed data types. They are mutable (can be changed).

Examples:

# Creating a list
numbers = [1, 2, 3, 4, 5]
mixed = [1, "Python", 3.14, True]

# Accessing elements
first = numbers[0]  # 1
last = numbers[-1]  # 5

# Modifying a list
numbers[1] = 10  # Change the second element

# List operations
numbers.append(6)  # Add element
numbers.remove(3)  # Remove element

4. Tuples

Tuples are similar to lists but are immutable (cannot be changed). They are often used for fixed collections of items.

Examples:

# Creating a tuple
coordinates = (10, 20)
names = ("Alice", "Bob", "Charlie")

# Accessing elements
first = coordinates[0]  # 10

# Tuples are immutable
# coordinates[0] = 15  # This will raise an error

5. Dictionary

Dictionaries are collections of key-value pairs. They are unordered and allow for fast lookups by key.

Examples:

# Creating a dictionary
person = {
    "name": "John",
    "age": 30,
    "is_student": False
}

# Accessing values
name = person["name"]  # John

# Adding/Updating a key-value pair
person["city"] = "New York"
person["age"] = 31

# Removing a key-value pair
del person["is_student"]

# Iterating through a dictionary
for key, value in person.items():
    print(key, value)

These data types and structures provide flexibility in handling various programming tasks effectively in Python.

Code Examples with Line-by-Line Explanation


1. Example of Numbers

Code:

# Store the price of a product (float)
price = 99.99  
print("Price of the product is:", price)

# Store the age of a person (integer)
age = 25  
print("Age of the person is:", age)

# Perform addition of two numbers
sum = age + 10  
print("Age after 10 years will be:", sum)

Explanation (Line by Line):

  1. price = 99.99:

    • एक float value को price में स्टोर किया गया।
    • यह किसी product की कीमत को दर्शाता है।
  2. print("Price of the product is:", price):

    • print statement price को स्क्रीन पर दिखाएगा।
  3. age = 25:

    • age variable में integer value 25 को स्टोर किया गया।
  4. sum = age + 10:

    • age में 10 जोड़ा गया और इसका result sum में स्टोर हुआ।
  5. print("Age after 10 years will be:", sum):

    • अगले 10 सालों के बाद की उम्र sum को स्क्रीन पर प्रिंट किया गया।

Output:

Price of the product is: 99.99  
Age of the person is: 25  
Age after 10 years will be: 35  

2. Example of Strings

Code:

# Store a name
name = "Ajay Kumar"  
print("Name is:", name)

# Convert the name to uppercase
uppercase_name = name.upper()  
print("Uppercase Name:", uppercase_name)

# Find the length of the name
name_length = len(name)  
print("Length of Name:", name_length)

Explanation (Line by Line):

  1. name = "Ajay Kumar":

    • name variable में एक string value को स्टोर किया गया।
  2. print("Name is:", name):

    • name को स्क्रीन पर दिखाने के लिए print का उपयोग किया गया।
  3. uppercase_name = name.upper():

    • name.upper() method का उपयोग करके string को uppercase (बड़े अक्षरों) में बदला।
  4. name_length = len(name):

    • len function से string की लंबाई (length) निकाली।
  5. print("Length of Name:", name_length):

    • name_length को स्क्रीन पर प्रिंट किया।

Output:

Name is: Ajay Kumar  
Uppercase Name: AJAY KUMAR  
Length of Name: 10  

3. Example of Lists

Code:

# Create a list of fruits
fruits = ["Apple", "Banana", "Cherry"]  
print("Original List:", fruits)

# Add a fruit to the list
fruits.append("Orange")  
print("After Adding Orange:", fruits)

# Remove a fruit from the list
fruits.remove("Banana")  
print("After Removing Banana:", fruits)

Explanation (Line by Line):

  1. fruits = ["Apple", "Banana", "Cherry"]:

    • एक list बनाई जिसमें 3 फलों के नाम स्टोर किए गए।
  2. print("Original List:", fruits):

    • Original list को स्क्रीन पर प्रिंट किया।
  3. fruits.append("Orange"):

    • append method से list में "Orange" जोड़ा गया।
  4. fruits.remove("Banana"):

    • remove method से list में "Banana" हटाया गया।
  5. print("After Removing Banana:", fruits):

    • Updated list को स्क्रीन पर प्रिंट किया।

Output:

Original List: ['Apple', 'Banana', 'Cherry']  
After Adding Orange: ['Apple', 'Banana', 'Cherry', 'Orange']  
After Removing Banana: ['Apple', 'Cherry', 'Orange']  

4. Example of Tuples

Code:

# Create a tuple for coordinates
coordinates = (10, 20, 30)  
print("Coordinates are:", coordinates)

# Access the first item in the tuple
first_item = coordinates[0]  
print("First item in the tuple:", first_item)

Explanation (Line by Line):

  1. coordinates = (10, 20, 30):

    • एक tuple बनाया, जिसमें तीन नंबर स्टोर किए गए।
  2. print("Coordinates are:", coordinates):

    • coordinates tuple को स्क्रीन पर प्रिंट किया।
  3. first_item = coordinates[0]:

    • Tuple के पहले item (index 0) को access करके first_item में स्टोर किया।
  4. print("First item in the tuple:", first_item):

    • पहले item को स्क्रीन पर प्रिंट किया।

Output:

Coordinates are: (10, 20, 30)  
First item in the tuple: 10  

5. Example of Dictionary

Code:

# Create a dictionary for a student
student = {"name": "Ajay", "age": 16, "grade": "10th"}  
print("Student Details:", student)

# Access the student's name
print("Student's Name:", student["name"])

# Add a new key-value pair
student["hobby"] = "Cricket"  
print("After Adding Hobby:", student)

# Update the grade
student["grade"] = "12th"  
print("After Updating Grade:", student)

Explanation (Line by Line):

  1. student = {"name": "Ajay", "age": 16, "grade": "10th"}:

    • एक Dictionary बनाई जिसमें छात्र का विवरण (name, age, grade) स्टोर किया।
  2. print("Student Details:", student):

    • Dictionary को स्क्रीन पर प्रिंट किया।
  3. print("Student's Name:", student["name"]):

    • Dictionary से name key की value को access किया और प्रिंट किया।
  4. student["hobby"] = "Cricket":

    • नई key "hobby" को value "Cricket" के साथ जोड़ा।
  5. student["grade"] = "12th":

    • grade key की value को update किया।

Output:

Student Details: {'name': 'Ajay', 'age': 16, 'grade': '10th'}  
Student's Name: Ajay  
After Adding Hobby: {'name': 'Ajay', 'age': 16, 'grade': '10th', 'hobby': 'Cricket'}  
After Updating Grade: {'name': 'Ajay', 'age': 16, 'grade': '12th', 'hobby': 'Cricket'}  

Conclusion

Code को Line-by-Line Explain करने से छात्रों को हर एक लाइन का उपयोग और Logic समझने में आसानी होगी। साथ ही Practical Examples उन्हें Coding का Confidence देंगे।

Post a Comment

0 Comments