搜尋此網誌

2026年4月23日星期四

Practice in Python

In Python, the expression int(hexNum, 16) means “convert a hexadecimal string into a decimal integer.”

Curly brackets: dictionaries

In Python, def is the keyword used to define a function.

In Python, a for loop is used to repeat a block of code for each item in a sequence (like a list, tuple, string, or range).

for variable in sequence:

    # code block

variable → takes each value from the sequence one by one.

sequence → the collection you want to loop through.

The code block runs once for each item.

In Python, char isn’t a separate data type like in some other languages (for example, C or Java).

def check_number(n):

    if n > 0:

        return "Positive"

    elif n == 0:

        return "Zero"

    else:

        return "Negative"

print(check_number(5))   # Positive

print(check_number(0))   # Zero

print(check_number(-3))  # Negative

In Python, == is the equality operator. It’s used to check whether two values are equal.

print(2 ** 3)   # 8   (2 raised to the power of 3)
print(5 ** 2)   # 25  (5 squared)
print(10 ** 0)  # 1   (any number to the power of 0 is 1)

沒有留言:

發佈留言