Relational operators:
Relational operators in Python (also known as comparison operators) compare the values of two operands and return a Boolean value: either True or False.
- == Equal to
- != Not equal to
- > Greater than
- < Less than
- >= Greater than or equal to
- <= Less than or equal to
Example:
mark = 67
if mark >= 90:
print("grade a")
elif mark >= 80:
print("grade b")
elif mark >= 70:
print("grade c")
elif mark >= 40:
print("grade d")
else:
print("fail")
Output:
grade d
Ternary operator:
- The ternary operator in Python (officially called a conditional expression) is a compact, one-line version of a traditional if-else statement.
- It evaluates a condition and returns one value if the condition is true, and another value if it is false.
Syntax:
value_if_true if condition else value_if_false
Example:
num = 5
result = "Even" if num % 2 == 0 else "Odd"
print(result)
Output:
Odd
input() function:
In Python, you accept user input by using the built-in input() function.
By default, the input() function always returns a string (str), even if the user types a number.
Example:
name = input("enter a name:")
print(name)
Output:
enter a name:gokul
gokul
- Convert text to an integer:
Example:
num = int(input("enter a number:"))
result = "Even" if num % 2 == 0 else "Odd"
print(result)
Output:
enter a number:4
Even
- Convert text to a decimal:
Example:
num = float(input("enter a number:"))
result = "Even" if num % 2 == 0 else "Odd"
print(result)
Output:
enter a number:4.5
Odd
Runtime:
runtime (when the computer actually processes the instructions).
Compile time:
compile time (when code is translated into a lower-level format).
System language:
the language settings of the underlying operating system or Python's role as a system scripting language.
Function:
- A Python function is a reusable block of organized code designed to perform a single, focused action.
- Functions help break down large scripts into smaller, manageable pieces, preventing you from repeating the same code multiple times.
How to Create and Call a Function:
- The def keyword marks the start of the function header.
- The function name uniquely identifies it and follows standard variable naming rules.
- Parentheses () hold optional input values called parameters.
- A colon : marks the end of the function header.
- An indented code block describes what the function actually does.
- The return statement optionally exits the function and passes a value back to the caller.
Example:
def square(number):
return number * number
output = square(4)
print(output)
Output:
16
types of functions:
- Built-in Functions: Predefined tools built directly into Python, such as print(), len(), or max().
- User-Defined Functions: Custom blocks of code created by programmers using the def keyword to meet specific project needs.
types of arguments:
- Positional Arguments
- Keyword Arguments
- Default Arguments(tbd)
- Variable-Length Arguments(tbd)
TypeError:
A TypeError in Python occurs when an operation or function is applied to an object of an inappropriate data type.
NameError:
A NameError in Python occurs when the interpreter tries to execute a piece of code that references a variable, function, or module name that has not been defined or recognized.
Top comments (0)