44  Useful python code snippets

44.1 take a number input from user

Built-in function input could be used to take input from a user in command line. The return value of input function could be anything, such as qerwrrqrr, the user could press any key from the keyboard as an input. If we would like to use the return value as a number, we need to convert it from string value to other numbers, float or int. See below code snippet

str_n = input("please enter a number")
N = int(str_N)

44.2 divisible by some number

“%” remainder operator could be used to find if any number is divisible by another number. See below example

if n % 3 == 0: # Disible by 3 # do something here

a = 1
total = 0
while a <= 10:
    total = total + a
    a = a + 1
print(total)
N = int(str_n)
if n % 3 == 0: # Disible by 3
    # do something here