Day 3: Python Essentials - Strings, File Handling, Decorators & Generators ??

Today, I'm diving into powerful Python concepts essential for writing clean and efficient code: Strings, File Handling, Decorators, and Generators. Let's explore! ??


1?? Python Strings - More than Just Text

Strings in Python are immutable, meaning they cannot be modified in place

? Common String Operations

text = "Hello, Python!"

print(text.upper())            # HELLO PYTHON!
print(text.lower())            #hello, python!
print(text.split())              # ['Hello',' Python!']
print(text.replace("Python","World"]  # Hello, World! 
print(len(text))                #14        

?? Use f-strings for better readability in string formatting:

name = "Jyothsna"
print(f"Hello, {name}!")    # Hello, Jyothsna!         

2?? File Handling - Reading & Writing Files

Python makes working with files simple! ??

? Reading a File

with open("sample.txt", "r") as file:
        content = file.read()
        print(content)        

? Writing to a File

with open("output.txt","a") as file:
      file.write("\nAppending new content!")        

3?? Decorators - Adding Functionality to Functions

Decorators are used to modify functions without changing their code

? Basic Decorator Example

def my_decorator(func):
     def wrapper():
            print("Before the function call")
            func()
            print("After the function call")
       return wrapper

@my_decorator
def say_hello():
   print("Hello, World!")

say_hello()

        

?? Output:

Before the function call
Hello, World!
After the function call        

? Use Cases: logging, Authentication, Execution Timing


4?? Generators - Efficient Iteration with Yield

A generator is a special function that produces values on demand instead of storing them in memory

? Creating a Generator with yield

def my_generator():
      yield 1
      yield 2
      yield 3

gen=my_generator()
print(next(gen))   #1
print(next(gen))   #2
print(next(gen))   #3        

? Why to Use Generators?

?? Saves memory - No need to store large data in RAM

?? Improves performance - Generates value only when needed

?? Example: Generating an Infinite Sequence

def infinite_numbers():
      num=0
      while True:
             yield num
             num+=1

gen=infinite_numbers()
for i in range(10):
         print(next(gen))        

Summary of Today's Concepts

?? Strings - Use f-strings for formatting

?? File Handling - Use "with-open()" to manage files safely

?? Decorators - Modify functions without changing their code

?? Strings - Use yield for memory-efficient loops


?? Which concept do you use the most? Let me know in the comments! ??

#100DaysOfCode #Python #Programming #Coding #Learning


要查看或添加评论,请登录

Sarasa Jyothsna Kamireddi的更多文章

  • Day 28: Filtering Data with LIKE in MySQL

    Day 28: Filtering Data with LIKE in MySQL

    Today, let us explore the LIKE operator in MySQL, which is used for pattern matching in text-based searches. It's super…

  • Day 27: Filtering Data with WHERE

    Day 27: Filtering Data with WHERE

    Today, let us explore the WHERE clause in MySQL, which helps filter records based on specific conditions The WHERE…

  • Day 26: Basic MySQL Commands

    Day 26: Basic MySQL Commands

    Today, let us explore some fundamental MySQL commands that are essential for database management 1. Creating a Database…

  • Day 25: MySQL Installation

    Day 25: MySQL Installation

    Steps to Install MySQL: ? Download MySQL: Go to MySQL Official Website and choose the appropriate version for your OS…

  • Day 24: Introduction to MySQL

    Day 24: Introduction to MySQL

    Today, let us dive into MySQL, a powerful Relational Database Management System (RDBMS) that helps store and manage…

  • Day 23: Error Handling Best Practice in Python

    Day 23: Error Handling Best Practice in Python

    Proper error handling makes our python code more robust, readable, and maintainable. Here are the best practices to…

  • Day 22: Python Shortcuts to write Cleaner and more Efficient Code

    Day 22: Python Shortcuts to write Cleaner and more Efficient Code

    1. List Comprehensions 2.

  • Day 21: Different Types of Sorting Algorithms in Python

    Day 21: Different Types of Sorting Algorithms in Python

    Sorting is a fundamental concept in programming, and python supports multiple sorting techniques. Today, let's explore…

  • Day 20: Sorting in Python

    Day 20: Sorting in Python

    Sorting is a fundamental operation in programming. Python provides multiple ways to sort data efficiently.

  • Day 19: Python Coding Challenges

    Day 19: Python Coding Challenges

    Today, let's solve some interesting Python problems! 1?? Find Pairs with Given Sum in an Array Problem: Given an array…

社区洞察