Read more
MegaTech- AZILAL
Useful Python commands:
- Print
Statement:
- Description:
Used to display output on the console. It accepts one or more expressions
as arguments and prints their values.
|
print("Hello,
MEGATECH AZILAL!") |
- Input
Function:
- Description:
Used to take input from the user. It prompts the user with a message and
waits for the user to enter some input, which is then returned as a
string.
|
name = input("Enter F4T name: ") |
- Conditional
Statements (if, elif, else):
- Description:
Used to execute different blocks of code based on certain conditions. The
if statement checks a condition, and if it's true, it executes a
block of code. Optionally, elif and else can be used for
additional conditions and fallbacks, respectively.
|
x = 10 if x
> 0: print("x is positive") elif x == 0: print("x is zero") else: print("x is negative") |
- Loops
(for loop):
- Description:
Used to iterate over a sequence (such as a list or range) or other
iterable objects. It executes a block of code repeatedly for each item in
the sequence.
|
for i in range(5):
print(i) |
- Loops
(while loop):
- Description:
Used to repeatedly execute a block of code as long as the condition is
true. It continues execution until the condition becomes false.
|
x = 0 while x
< 5: print(x) x += 1 |
- Functions:
- Description:
Used to group code into reusable blocks. Functions take inputs
(arguments), perform some computation, and optionally return a result.
|
def greet(name):
print("Hello,", name) |
- Lists:
- Description:
Used to store multiple items in a single variable. Lists are ordered and
mutable, meaning their elements can be changed.
|
my_list = [1,
2, 3, 4, 5] |
- Tuples:
- Description:
Similar to lists but immutable, meaning their elements cannot be changed
after creation. Tuples are often used to store related pieces of data
together.
|
my_tuple = (1,
2, 3) |
- Dictionaries:
- Description: Used to store key-value pairs. Each key is unique and associated with a value. Dictionaries are unordered and mutable.(F4TSARLCOM)
Example: |
my_dict = {"name": "F4T", "age": 3} |
- Sets:
- Description:
Used to store unique elements. Sets are unordered and mutable, and they
don't allow duplicate values.
|
my_set = {1, 2,
3, 4, 5} |
- List
Comprehensions:
- Description:
Provides a concise way to create lists by applying an expression to each
item in an iterable. It's often used as a more readable alternative to
traditional loops.
|
squares =
[x**2 for x in range(5)] |
- Exception
Handling (try, except):
- Description:
Used to handle errors gracefully. The try block contains the code
that might raise an exception, and the except block catches and
handles the exception if it occurs.
|
try: result
= 10 / 0 except
ZeroDivisionError: print("Error:
Division by zero") |
0 Reviews