Learning Target: I can open, read, write, and append files in Python.
with open('example.txt', 'r') as file:
content = file.read()
print(content)
with open('example.txt', 'w') as file:
file.write('Hello, world!')
with open('example.txt', 'a') as file:
file.write('\nAppended text')
Write a program that:
Try it here:
Learning Target: I can check if a file exists and delete files in Python.
import os
if os.path.exists('example.txt'):
print('File exists')
else:
print('File does not exist')
import os
if os.path.exists('example.txt'):
os.remove('example.txt')
print('File deleted')
Write a program that:
Try it here:
Learning Target: I can use the 'with' statement for file operations in Python.
with open('example.txt', 'r') as file:
content = file.read()
print(content)
Write a program that:
Try it here:
Learning Target: I can read input from the console and print formatted text in Python.
name = input('Enter your name: ')
print('Hello, ' + name)
name = 'Alice'
age = 30
print('Name: {}, Age: {}'.format(name, age))
print(f'Name: {name}, Age: {age}')
Write a program that:
Try it here:
Learning Target: I can use command-line arguments in Python.
import sys
if len(sys.argv) > 1:
print('Arguments:', sys.argv[1:])
else:
print('No arguments provided')
Write a program that:
Try it here: