User Tools

Site Tools


products:ict:python:dict_and_file_questions

What is a dictionary in Python?

A dictionary is an unordered collection of key-value pairs in Python. How do you create an empty dictionary in Python?

You can create an empty dictionary using curly braces ({}) or by using the dict() constructor. How do you access a value in a dictionary?

You can access a value in a dictionary by using its corresponding key in square brackets ([]). How do you add a key-value pair to a dictionary?

You can add a key-value pair to a dictionary by assigning a value to a new key. How do you update the value of a key in a dictionary?

You can update the value of a key in a dictionary by reassigning a new value to the existing key. How do you check if a key exists in a dictionary?

You can use the in keyword to check if a key exists in a dictionary. How do you remove a key-value pair from a dictionary?

You can use the del keyword followed by the key to remove a key-value pair from a dictionary. What is file handling in Python?

File handling in Python refers to the process of reading from and writing to files. How do you open a file in Python?

You can open a file in Python using the open() function. What are the different modes in which you can open a file?

The different modes for opening a file are: “r” for reading, “w” for writing, “a” for appending, and “x” for creating a new file. How do you read the contents of a file?

You can use the read() method to read the contents of a file. How do you write to a file in Python?

You can use the write() method to write data to a file. How do you append data to an existing file?

You can open the file in append mode (“a”) and use the write() method to append data to an existing file. How do you close a file in Python?

You can use the close() method to close a file after reading or writing. How do you check if a file exists in Python?

You can use the exists() function from the os.path module to check if a file exists. How do you create a new file in Python?

You can open a file in write mode (“w”) or append mode (“a”), and if the file doesn't exist, it will be created. How do you read a file line by line?

You can use the readline() method to read a file line by line. How do you count the number of lines in a file?

You can use a loop to iterate over the lines of a file and maintain a counter to count the number of lines. How do you check the current position of the file cursor?

You can use the tell() method to check the current position of the file cursor. How do you move the file cursor to a specific position in the file?

You can use the seek() method to move the file cursor to a specific position in the file.

products/ict/python/dict_and_file_questions.txt · Last modified: 2023/05/06 15:56 by wikiadmin