Mapping
Mapping Data Type – Dictionary:
{}.: sign.| python |
dict1 = {‘Fruit’: ‘Apple’, ‘Climate’: ‘Cold’, ‘Price(kg)’: 120} print(dict1) # Output: {‘Fruit’: ‘Apple’, ‘Climate’: ‘Cold’, ‘Price(kg)’: 120} print(dict1[‘Price(kg)’]) # Output: 120 |
Mutable and Immutable Data Types:
Deciding Usage of Python Data Types
Lists:
Tuples:
Sets:
Dictionaries:
OPERATORS
Arithmetic Operators:
Relational Operators:
Assignment Operators:
= assigns a value to a variable.Logical Operators:
Identity Operators:
Membership Operators:
EXPRESSIONS
In Python, expressions are combinations of constants, variables, and operators that always evaluate to a value. Here are some examples of valid expressions:
100numnum - 20.43.0 + 3.1423 / 3 - 5 * 7 * (14 - 2)"Global" + "Citizen"Precedence of operators determines the order in which operators are evaluated within an expression. Unary operators, which operate on a single operand, typically have higher precedence than binary operators, which operate on two operands. Both the minus (-) and plus (+) operators can act as unary or binary operators. The not operator is a unary logical operator.
For example, let’s evaluate the expression 15.0 / 4 + (8 + 3.0):
15.0 / 4 + (8.0 + 3.0) (Step 1: Parentheses evaluated first)15.0 / 4.0 + 11.0 (Step 2: Division and addition within parentheses)3.75 + 11.0 (Step 3: Addition)14.75 (Step 4: Final result)STATEMENT
In Python, a statement is a unit of code that the Python interpreter can execute. Here’s an example illustrating different types of statements:
| python |
x = 4 # Assignment statement cube = x ** 3 # Assignment statement print(x, cube) # Print statement |
In this example:
x = 4 and cube = x ** 3 are assignment statements, where values are assigned to variables.print(x, cube) is a print statement, which outputs the values of variables x and cube.INPUT AND OUTPUT
To receive input from the user, Python provides the input() function. It prompts the user to enter data and accepts it as a string.
| python |
fname = input(“Enter your first name: “) age = input(“Enter your age: “) |
In this example, fname and age variables will contain the strings entered by the user. It’s important to note that all input received using input() is treated as strings.
Python offers the print() function for displaying output to the user.
| python |
print(“Hello”) print(10 * 2.5) print(“I” + ” love “ + “my “ + “country”) print(“I’m”, 16, “years old”) |
In the above example:
print() statement displays the string “Hello”.print() statement evaluates the expression 10 * 2.5 and displays the result.print() statement concatenates multiple strings using the + operator.print() statement demonstrates how multiple arguments can be passed to the print() function, separated by commas.If necessary, you can convert the input received from the user to the desired data type using typecasting.
| python |
age = int(input(“Enter your age: “)) |
Here, the int() function converts the input string to an integer. However, if the input cannot be converted to the specified data type, it will result in an error.
TYPE CONVERSION
Type conversion is a crucial aspect of programming, especially when dealing with different data types. Let’s break down the concept and how it works:
| python |
| # Explicit type casting priceIcecream = 25 priceBrownie = 45 totalPrice = priceIcecream + priceBrownie # Converting totalPrice to a string before printing print(“The total in Rs.” + str(totalPrice)) |
In this example:
totalPrice is calculated as the sum of priceIcecream and priceBrownie.totalPrice with a string for printing, we use str(totalPrice) to explicitly convert it to a string.| python |
| # Explicit type conversion – string to integer price = int(icecream) + int(brownie) print(“Total Price Rs.” + str(price)) |
Here:
icecream and brownie are initially strings representing prices.int() to explicitly convert them to integers before performing addition.DEBUGGING
Debugging is an essential skill for programmers to master, as it helps identify and resolve issues in code. Let’s delve into the different types of errors and how to handle them effectively:
| python |
| # Example of a program that may encounter runtime errors num1 = 10.0 num2 = int(input(“num2 = “)) # Handle potential division by zero error try: result = num1 / num2 print(“Result:”, result) except ZeroDivisionError: print(“Error: Division by zero is not allowed.”) except ValueError: print(“Error: Please enter a valid integer value for num2.”) |
By understanding and addressing different types of errors, programmers can write more reliable and robust code, ensuring smooth program execution and minimizing disruptions.