Day 1
Day 1
Coding Challenge - 1
Variable and variable types
Variable is a memory location to store a value. Below are the types of variables which
are mostly used -
Float - is a number with decimal format to represent a fraction (e.g. 0.0, 1.23, 50.01)
Boolean - is a data with two possible values: true or false (e.g. True)
Array - is a data structure that can store one or more objects or values (e.g. [ “Hello”, 2,
True ]
Casting variables and get type
Casting can be used to specify the data type of a variable. “type” function gives the type
of a variable.
x = str(3)
y = float(3)
z = str(3)
x = 3
y = float(x)
print(type(y))
Assigning value to a variable
Condition is used when we need the program to operates differently to the various
inputs.
x = 5
if x == 0:
print("x is zero")
elif x < 3:
print("x is less than 3")
else:
print("x is greater than 3")
Loop (for - in)
for x in range(10):
print(x)
x = 0
while(x < 10):
print(x)
x += 1
x = 0
a = [ "A", "B", "C", "D" ]
while(x < len(a)):
print(a[x])
x += 1
Array and its functions
Array is a data structure that can store one or more objects or values.
Function is a block of code which only runs when it is called. It is usually used for
reusability and readability.
def message():
print("Hello")
def message_with_param(msg):
print(msg)
def message_with_param_default_value(msg="Hello World !"):
print(msg)
message()
message_with_param("Hello World !")
message_with_param_default_value() # default value will be used
Recursive Function
Recursive function is called when a function calls itself. Below function is to calculate
factorial.
def factorials(x):
if(x > 1):
return x * factorials(x - 1)
else:
return x
print(factorials(6))
print(6 * 5 * 4 * 3 * 2 * 1) # result is the same as above