Posts

Showing posts from August, 2024

Control Structures in Python

Image
Control Structures in Python Control structures determine the flow of execution of the program. They include: Sequential Control : The default mode where statements are executed one after another. Decision-Making Statements : These allow the program to take different paths based on conditions. Looping Statements : These allow the program to execute a block of code multiple times. Decision-Making Statements if Statement : Definition:  In Python, an  if  statement is used to execute a block of code only if a specified condition is true. It’s a fundamental control structure that allows you to make decisions in your code. Here’s a basic overview  Syntax : if condition: # block of code Flowchart : Example : number = 10 if number > 5 : print ( "Number is greater than 5" ) if-else Statement : Definition:  An  if-else   statement in Python allows you to execute one block of code if a condition is true and another block if the condition is fal...

Precedence, Associativity of Operators, Non-Associative Operators

Precedence Operator Description Precedence Associativity () Parentheses Highest N/A ** Exponentiation High Right-to-left +x , -x , ~x Unary plus, Unary minus, Bitwise NOT High Right-to-left * , / , // , % Multiplication, Division, Floor division, Modulus Medium Left-to-right + , - Addition, Subtraction Medium Left-to-right << , >> Bitwise shift operators Medium Left-to-right & Bitwise AND Medium Left-to-right ^ Bitwise XOR Medium Left-to-right ` ` Bitwise OR Medium == , != , > , < , >= , <= , is , is not , in , not in Comparisons, Identity, Membership operators Low Left-to-right not Logical NOT Low Right-to-left and Logical AND Low Left-to-right or Logical OR Lowest Left-to-right = , += , -= , *= , /= , etc. Assignment operators N/A Non-associative Associativity of Operators Associativity determines the order in which operators of the same precedence are evaluated. Most operators in Python have left-to-right associativity, meaning they are evaluated from le...

Python Operators

Python Operators Here’s a comprehensive table of Python operators, along with examples and explanations for each type: Arithmetic Operators Operator Name Example Explanation + Addition 5 + 3 Adds two numbers - Subtraction 5 - 3 Subtracts second number from first * Multiplication 5 * 3 Multiplies two numbers / Division 5 / 3 Divides first number by second // Floor Division 5 // 3 Divides and returns the integer part % Modulus 5 % 3 Returns the remainder of division ** Exponentiation 5 ** 3 Raises first number to the power of second Assignment Operators Operator Name Example Explanation = Assignment x = 5 Assigns value to variable += Addition Assignment x += 3 Adds and assigns -= Subtraction Assignment x -= 3 Subtracts and assigns *= Multiplication Assignment x *= 3 Multiplies and assigns /= Division Assignment x /= 3 Divides and assigns //= Floor Division Assignment x //= 3 Floor divides and assigns %= Modulus Assignment x %= 3 Modulus and assigns **= Exponentiation Assignment x **= 3 E...

Input & Output Function, Import Function

 Sure! Let’s dive deeper into the input, output, and import functions in Python. Input Function The input() function is used to take input from the user. It reads a line from the input and returns it as a string. Here are some detailed examples: Basic Input : name = input ( "Enter your name: " ) print ( "Hello, " + name + "!" ) Input with Type Conversion : You can convert the input to other data types using functions like int() , float() , etc. age = int ( input ( "Enter your age: " )) print ( "You are" , age, "years old." ) Handling Multiple Inputs : You can take multiple inputs in a single line and split them. x, y = input ( "Enter two numbers separated by space: " ).split() print ( "First number:" , x) print ( "Second number:" , y) Output Function The print() function is used to display output to the console. It can print strings, variables, and other data types. Here are some detailed examp...