Python - Input & Output Operations
Input from Command Line
1
2
user_input = input("Enter the value to save in the variable:")
print("Output is:", user_input)
- Changing the data types:
1 2 3 4
user_input1 = int(input("Enter the number 1:")) user_input2 = int(input("Enter the number 2:")) sum = user_input1 + user_input2 print("Sum is", sum)
Input from a storage/file
- File Operations
1 2
fileobj = open(filename, mode) fileobj.close()
- Modes:
- r : read
- w : write, create if file does not exist and overwrite if it exists
- x : write only if file does not exist
- a : append
- Second letter of mode:
- t : text
- b : binary
- Modes:
Example:
1 2 3
fileobj = open("example.txt", "wt") fileobj.write("The is the content of the text file") fileobj.close()
This post is licensed under CC BY 4.0 by the author.