Posts

ILLUSTRATIVE PROGRAMS IN PYTHON

1  Hello, World! Program: print ( "Hello, World!" ) This program simply prints the message "Hello, World!" to the console. 2 Simple Calculator: def add ( a, b ): return a + b def subtract ( a, b ): return a - b def multiply ( a, b ): return a * b def divide ( a, b ): return a / b num1 = float ( input ( "Enter the first number: " )) num2 = float ( input ( "Enter the second number: " )) print ( "Sum:" , add(num1, num2)) print ( "Difference:" , subtract(num1, num2)) print ( "Product:" , multiply(num1, num2)) print ( "Quotient:" , divide(num1, num2)) This program performs basic arithmetic operations (addition, subtraction, multiplication, and division) on two numbers provided by the user.

PRECEDENCE EXAMPLE

PRECEDENCE EXAMPLE x = 2 + 3 * 4  # Multiplication has higher precedence than addition print(x)  # Output: 14 y = (2 + 3) * 4  # Parentheses enforce addition to be evaluated first print(y)  # Output: 20 z = 2 ** 3 ** 2  # Exponentiation is right associative print(z)  # Output: 512 a = 9 % 4 + 2 / 3  # Modulo has higher precedence than division print(a)  # Output: 2.6666666666666665 b = (9 % 4) + (2 / 3)  # Parentheses enforce separate evaluations print(b)  # Output: 2.6666666666666665 c = 10 / 2 * 3  # Multiplication and division have the same precedence print(c)  # Output: 15.0 d = 10 / (2 * 3)  # Parentheses enforce multiplication to be evaluated first print(d)  # Output: 1.6666666666666667 e = not False or True  # Logical NOT has higher precedence than logical OR print(e)  # Output: True f = False or True and True  # Logical AND has higher precedence than logical OR print(f)  # Output: Tru...

PRECEDENCE IN PYTHON

 PRECEDENCE IN PYTHON  In Python, the precedence of operators follows a specific hierarchy. Here is the precedence of operators in Python, from highest to lowest: Parentheses: Operators enclosed in parentheses are evaluated first. Parentheses can also be used to override the default precedence and enforce a specific order of evaluation. Exponentiation: The exponentiation operator ( ** ) is evaluated next. For example, in the expression 2 ** 3 , the exponentiation operator has higher precedence than any other operator, so the result would be 8. Unary Operators : Unary operators, such as negation ( - ) or logical negation ( not ), are evaluated next. These operators operate on a single operand. For example, in the expression -3 * 4 , the unary negation operator has higher precedence than the multiplication operator, so the result would be -12. Multiplication, Division, Floor Division, and Modulo: Multiplication ( * ), division ( / ), floor division ( // ), and modulo ( %...