Week 6 Assignment
- Get link
- X
- Other Apps
Chapter 5: Operators
Operators in Python are symbols used to perform various operations on variables and values. They can be categorized into several types:
Arithmetic Operators:
- Addition (+): Adds two values.
- Subtraction (-): Subtracts the right operand from the left operand.
- Multiplication (*): Multiplies two values.
- Division (/): Divides the left operand by the right operand.
Comparison Operators:
- Equal (==): Returns True if the two operands are equal.
- Not equal (!=): Returns True if the two operands are not equal.
- Greater than (>): Returns True if the left operand is greater than the right operand.
- Less than (<): Returns True if the left operand is less than the right operand.
- Greater than or equal to (>=): Returns True if the left operand is greater than or equal to the right operand.
- Less than or equal to (<=): Returns True if the left operand is less than or equal to the right operand.
Logical Operators:
- and: Returns True if both operands are true.
- or: Returns True if either of the operands is true.
- not: Returns True if the operand is false.
Bitwise Operators:
- Bitwise AND (&): Performs bitwise AND operation on the corresponding bits of the operands.
- Bitwise OR (|): Performs bitwise OR operation on the corresponding bits of the operands.
Additional operators include:
- Modulus (%): Returns the remainder of the division.
- Exponentiation (**): Raises the left operand to the power of the right operand.
- Floor Division (//): Performs division and returns the largest whole number less than or equal to the result.
Example code demonstrates the use of arithmetic operators like addition, subtraction, multiplication, and division. The output displays the results of these operations.
Chapter 6: Functions in Python
Functions in Python are blocks of reusable code designed to perform specific tasks. They aid in breaking down complex tasks into smaller, manageable parts and facilitate code reuse. Functions are defined using the def
keyword followed by the function name, parentheses, and a colon. Input arguments can be specified within the parentheses, and the function body follows, indented by 4 spaces or a tab. Functions can return a value using the return
statement.
Example functions include:
- A function that adds two numbers and returns the sum.
- A function that greets a person with a customizable greeting message.
Functions can have default argument values, allowing the function to be called with fewer arguments.
Chapter 7: Modules
Python uses a modular approach to organizing code, where modules are files containing Python code, usually with a .py
extension. Modules can include functions, classes, and variables, which can be imported and used in other Python scripts.
Built-in modules include math
, os
, sys
, re
, datetime
, random
, json
, and collections
, among others. Modules can be imported using the import
statement, and specific functions, classes, or variables can be imported using the from ... import ...
statement. Aliases can be used to shorten module names or avoid naming conflicts.
Example code demonstrates the use of the re
module for working with regular expressions to find email addresses in a given text.
- Get link
- X
- Other Apps
Comments
Post a Comment