0% found this document useful (0 votes)
3 views5 pages

Python Notes Set 3

The document covers several programming concepts including iterators and generators, regular expressions, date and time manipulation, JSON handling, and OS and sys modules in Python. It provides code examples for each topic, illustrating how to use iterators with the yield statement, perform pattern matching with regex, manage dates, handle JSON data, and interact with the operating system. Each section highlights key functions and methods relevant to the respective topics.

Uploaded by

fake786king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Python Notes Set 3

The document covers several programming concepts including iterators and generators, regular expressions, date and time manipulation, JSON handling, and OS and sys modules in Python. It provides code examples for each topic, illustrating how to use iterators with the yield statement, perform pattern matching with regex, manage dates, handle JSON data, and interact with the operating system. Each section highlights key functions and methods relevant to the respective topics.

Uploaded by

fake786king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Page 1: Iterators and Generators

Iterators:

- __iter__() and __next__()

Generators:

def count_up_to(n):

i=1

while i <= n:

yield i

i += 1

for num in count_up_to(5):

print(num)
Page 2: Regular Expressions

import re

Pattern matching:

pattern = r'\d+'

text = "123 and 456"

matches = re.findall(pattern, text)

Useful Functions:

- re.match()

- re.search()

- re.findall()

- re.sub()
Page 3: Date and Time

import datetime

now = datetime.datetime.now()

print(now)

today = datetime.date.today()

formatted = today.strftime("%Y-%m-%d")

timedelta = datetime.timedelta(days=5)

future = today + timedelta


Page 4: JSON Handling

import json

To JSON:

data = {"name": "Alice", "age": 30}

json_str = json.dumps(data)

From JSON:

parsed = json.loads(json_str)

Read/Write files:

with open('data.json', 'w') as f:

json.dump(data, f)
Page 5: OS and Sys Modules

import os, sys

OS:

- os.getcwd(), os.listdir(), os.rename()

- os.path.exists(), os.remove()

SYS:

- sys.argv

- sys.path

- sys.exit()

Used for interacting with the system

You might also like