Python Unit - 3
Python Unit - 3
Mapping type: Dictionaries – Mapping type operators – Mapping type It represents a collection of key-value pairs.
Built-in and Factory Functions - Mapping type built in methods –
Conditionals and loops – if statement – else Statement – elif statement – Each key is unique within the collection.
conditional expression – while statement – for statement – break statement
– continue statement – pass statement – Iterators and the iter( ) function -
The primary mapping types in Python are dictionaries (dict).
Files and Input/output – File objects – File built-in functions – File built- in
methods – File built-in attributes – Standard files – command line
arguments. 1. Creating a Dictionary:
Using curly braces {} to define a dictionary.
MAPPING TYPE Example:
DICTIONARIES my_dict = {'key1': 'value1', 'key2': 'value2'}
It is the mapping type.
It is mutable. 2. Accessing Values:
Syntax: Using square brackets [] to access the value associated with a
variable_name={“Value-1”,”Value_2”,Value_n} key.
Example: Example:
value = my_dict['key1']
3. Adding/Updating Elements:
Using square brackets [] to add or update key-value pairs.
Example:
Output:
my_dict['new_key'] = 'new_value'
7. Getting Items:
Using the items() method to get a list of key-value pairs as
tuples.
Example:
items = my_dict.items()
2. copy() 4. Item()
Returns a shallow copy of the dictionary. Returns a view of the dictionary's key-value pairs as tuples.
Example: Example:
original_dict = {'a': 1, 'b': 2, 'c': 3} my_dict = {'a': 1, 'b': 2, 'c': 3}
copied_dict = original_dict.copy () items = my_dict.items()
print(items)
Output:
dict_items([('a', 1), ('b', 2), ('c', 3)])
Program:
print("String Handling Function") Program:
str1=input("Enter the String 1: ") print("String Handling Function")
if str1.islower(): str1=input("Enter the String 1: ")
print(str1,"The string is lower..") if str1.islower ():
print(str1,"The string is lower..")
else:
print("Try again")
Syntax: name=name+1
While (condition):
//statement-1 Output:
Flowchart
Output:
Output:
Output:
Output:
1. file.name:
2. file.mode
3. file.closed
Example:
f=open("N:/naresh1.txt","r")
print(f.read())
f=open("N:/naresh1.txt","a")
print(f.write("Kumar"))
print(f.name)
print(f.mode)
print(f.closed)
f.close()
5. requirements.txt files:
The line in front of the command prompt C:\> is called as Similar as the C getopt() function.
command-line. The getopt module is useful in parsing command line
The command-line arguments are always stored in string arguments.
variables.
3. Python argparse module
To store in numeric variable, we can use type conversion
functions. It is the preferred way to parse command line arguments.
Command-line arguments are parameters passed to a script or It provides a lot of option such as
program.
1. Positional arguments
There are different ways we can use command line
2. Default value for arguments
arguments.
3. Help message
1. Python sys module
4. Specifying data type of argument etc.
2. Python getopt module
Example:
3. Python argparse module
import argparse
4. Fire
parser = argparse.ArgumentParser()
5. Docopt
parser.parse_args()
1. Python sys module
4. Docopt
It stores command-line arguments into a list.
Docopt is used to create command line interfaces.
It accesses using sys.argv.
It simplifies the process of parsing command-line arguments
Read command-line arguments as String.
and generating help messages.
Example:
5. Fire
import sys
Python Fire automatically generates a command line
print(type(sys.argv)) interface.
print(' The command line arguments are: ') Here, need one line of code.