Professional Documents
Culture Documents
Sure
Sure
Sure
```python
file.write(data)
def read_from_file(filename):
return file.read()
write_to_file("output.txt", user_input)
file_data = read_from_file("output.txt")
```
```python
import csv
writer = csv.writer(file)
writer.writerows(data)
def read_csv(filename):
with open(filename, 'r') as file:
reader = csv.reader(file)
return list(reader)
write_csv("data.csv", data_to_write)
data_from_csv = read_csv("data.csv")
```
```python
if user_greeting:
else:
result = greet(user_name)
print(result)
```
```python
class Stack:
def __init__(self):
self.items = []
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
def is_empty(self):
return len(self.items) == 0
stack = Stack()
stack.push(5)
stack.push(10)
stack.push(15)
print("Popped:", stack.pop())
print("Popped:", stack.pop())
```
```python
import pickle
pickle.dump(data, file)
def read_binary_file(filename):
write_binary_file("data.bin", data_to_write)
data_from_binary = read_binary_file("data.bin")
```
You can continue building on these examples by combining different concepts and adding more
functionality to them.
Of course, here are five more programs that continue to cover various topics from your list:
```python
def double_numbers(numbers):
file.write(str(item) + '\n')
numbers = [1, 2, 3, 4, 5]
doubled_numbers = double_numbers(numbers)
write_list_to_file("doubled_numbers.txt", doubled_numbers)
```
**Program 7: File Reading and Manipulation**
```python
lines = infile.readlines()
outfile.write(line + '\n')
reverse_lines("input.txt", "reversed_output.txt")
```
```python
display_info("Alice", 28)
```
```python
import csv
writer = csv.writer(file)
writer.writerow(data)
user_data = input("Enter name and age (comma-separated): ").split(',')
append_to_csv("data.csv", user_data)
```
```python
global_variable = 10
def modify_global():
global global_variable
global_variable += 5
def local_scope_demo():
local_variable = 20
modify_global()
local_scope_demo()
# Uncomment the next line to see an error since local_variable is not defined here
```
Feel free to experiment with these programs, combine concepts, and explore further. Each program
provides a unique perspective on the topics you've mentioned, and you can continue building your
understanding by adapting and expanding these examples.
Certainly! Here are five more programs that continue to cover the topics you've mentioned:
```python
def write_lines_to_file(filename, lines):
file.write(line + '\n')
def read_and_reverse_lines(filename):
lines = file.readlines()
return reversed_lines
write_lines_to_file("lines.txt", lines_to_write)
reversed_lines = read_and_reverse_lines("lines.txt")
```
```python
class Stack:
def __init__(self):
self.items = []
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
def is_empty(self):
return len(self.items) == 0
stack = Stack()
stack.push("apple")
stack.push("banana")
stack.push("cherry")
print("Popped:", stack.pop())
item = stack.pop()
file.write(item + '\n')
```
```python
import csv
rows = list(csv.reader(file))
rows[row_index] = new_data
writer = csv.writer(file)
writer.writerows(rows)
```
```python
return a + b
file.write(str(result))
```
```python
def calculate_sum(*args):
return sum(args)
total = calculate_sum(*nums)
```
These additional programs should help you further explore the topics you've mentioned. You can
continue to mix and match these concepts and create more complex programs as you become more
comfortable with the concepts and their interactions.
Certainly! Here's a program that includes a mix of various concepts you mentioned within
approximately 30 lines of code:
```python
import csv
import pickle
def calculate_average(numbers):
total = sum(numbers)
def read_csv_and_calculate_average(filename):
reader = csv.reader(file)
avg = calculate_average(data)
return avg
pickle.dump(data, file)
def read_binary_data(filename):
data = pickle.load(file)
return data
# Main program
if __name__ == "__main__":
csv_filename = "data.csv"
writer = csv.writer(file)
avg = read_csv_and_calculate_average(csv_filename)
binary_filename = "binary_data.bin"
write_binary_data(binary_filename, binary_data)
loaded_data = read_binary_data(binary_filename)
```
This program incorporates user-defined functions, CSV file handling, binary file handling using the
pickle module, and calculations. It demonstrates how these concepts can be combined to create a
functional program within the specified line count.
Certainly! Here are the programs for each of the tasks you mentioned:
**1. Read a text file line by line and display each word separated by a #**
```python
def process_text_file(file_name):
with open(file_name, 'r') as file:
words = line.strip().split()
formatted_line = '#'.join(words)
print(formatted_line)
text_file_name = 'sample.txt'
process_text_file(text_file_name)
```
```python
def count_characters(file_name):
vowels = "AEIOUaeiou"
consonants = "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz"
content = file.read()
if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
if char in vowels:
vowel_count += 1
consonant_count += 1
print("Uppercase:", upper_count)
print("Lowercase:", lower_count)
print("Vowels:", vowel_count)
print("Consonants:", consonant_count)
text_file_name = 'sample.txt'
count_characters(text_file_name)
```
```python
lines = file.readlines()
new_file.writelines(filtered_lines)
input_file_name = 'input.txt'
output_file_name = 'output.txt'
```
**4. Create and search in a binary file with name and roll number**
```python
import pickle
pickle.dump(data, file)
def search_roll_number(file_name, target_roll):
data = pickle.load(file)
if roll == target_roll:
break
else:
binary_file_name = 'students.bin'
create_binary_file(students, binary_file_name)
search_roll_number(binary_file_name, 102)
```
**5. Create and update a binary file with roll number, name, and marks**
```python
import pickle
pickle.dump(data, file)
data = pickle.load(file)
if target_roll in data:
data[target_roll]['marks'] = new_marks
with open(file_name, 'wb') as file:
pickle.dump(data, file)
print("Marks updated.")
else:
students = {
binary_file_name = 'students_marks.bin'
create_binary_file(students, binary_file_name)
```
```python
import random
def roll_dice():
return random.randint(1, 6)
print("Rolled:", roll_dice())
```
```python
class Stack:
def __init__(self):
self.items = []
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
def is_empty(self):
return len(self.items) == 0
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print("Popped:", stack.pop())
print("Popped:", stack.pop())
```
**8. Create a CSV file for user-id and password, and search for password by user-id**
```python
import csv
writer = csv.writer(file)
writer.writerows(data)
reader = csv.reader(file)
if row[0] == target_user_id:
break
else:
csv_file_name = 'user_data.csv'
create_csv_file(user_data, csv_file_name)
search_password(csv_file_name, 'user2')
```
These programs cover a range of tasks as per your request. Feel free to modify and combine them to
meet your specific requirements.
**9. Read a text file line by line and display each word separated by a #**
```python
def process_text_file(file_name):
words = line.strip().split()
formatted_line = '#'.join(words)
print(formatted_line)
text_file_name = 'sample.txt'
process_text_file(text_file_name)
```
```python
def count_characters(file_name):
vowels = "AEIOUaeiou"
consonants = "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz"
content = file.read()
if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
if char in vowels:
vowel_count += 1
consonant_count += 1
print("Uppercase:", upper_count)
print("Lowercase:", lower_count)
print("Vowels:", vowel_count)
print("Consonants:", consonant_count)
text_file_name = 'sample.txt'
count_characters(text_file_name)
```
**11. Remove lines containing 'a' and write to another file**
```python
lines = file.readlines()
new_file.writelines(filtered_lines)
input_file_name = 'input.txt'
output_file_name = 'output.txt'
```
**12. Create and search in a binary file with name and roll number**
```python
import pickle
pickle.dump(data, file)
data = pickle.load(file)
if target_roll in data:
else:
binary_file_name = 'students.bin'
create_binary_file(students, binary_file_name)
search_roll_number(binary_file_name, 102)
```
**13. Create and update a binary file with roll number, name, and marks**
```python
import pickle
pickle.dump(data, file)
data = pickle.load(file)
if target_roll in data:
data[target_roll]['marks'] = new_marks
pickle.dump(data, file)
print("Marks updated.")
else:
students = {
binary_file_name = 'students_marks.bin'
create_binary_file(students, binary_file_name)
```
```python
import random
def roll_dice():
return random.randint(1, 6)
print("Rolled:", roll_dice())
```
```python
class Stack:
def __init__(self):
self.items = []
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
def is_empty(self):
return len(self.items) == 0
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print("Popped:", stack.pop())
print("Popped:", stack.pop())
```
Feel free to adapt and use these programs for your needs, or combine them to create more
comprehensive applications. Each program demonstrates a specific concept or task as outlined in
your original request.