YECC
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 -
Integer - is a whole number (e.g. 1, 2, 3)
Float - is a number with decimal format to represent a fraction (e.g. 0.0, 1.23, 50.01)
String - is a serie of characters (text) (e.g. “Hello World”)
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
There are multiple ways to assign value to a variable.
x = "Hello World"
print(x)
x, y, z = "Hello", "World", "!" (or) x, y, z = [ "Hello", "World", "!" ]
print(x)
print(y)
print(z)
x = y = z = "Hello World !"
print(x)
print(y)
print(z)
Printing variables
We can use “print” function to print out one or more variables.
x, y, z = "Hello", "World", "!"
print(x)
print(x, y, z)
print(x + " " + y + " " + z)
print(f"{x} {y} {z}")
a = "We can also use format function to add variables in the predefined
places. {num} {} {} {}"
print([Link](x, y, z, num=10))
Condition (if - elif - else)
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)
Loop is used when we want program to go through each item of a list.
for x in range(10):
print(x)
for x in [ "A", "B", "C", "D" ]:
print(x)
Loop (while)
Loop is used when we want program to go through each item of a list.
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.
cars = ["Ford", "Volvo", "BMW"]
print(cars) # print out the whole array
print(cars[0]) # print out the first index of the array
cars[0] = "Toyota" # modify the first index of the array
len(cars) # get the length of the array
[Link]("Honda") # add new item into the array
Array and its functions - cont
chinese_cars = [ "GAC", "FAW" ]
[Link](chinese_cars) # combine two arrays
[Link]() # remove the last element
[Link](0) # remove the element by index
[Link]("GAC") # remove the first occurance of the
element by value
[Link]("Honda") # get the index of the first occurance of
the element by value
[Link]() # remove all elements
String and its methods
s = "Welcome to Coding Challenge! "
s = [Link]() # capitalize the first character
s = [Link]() # convert string to uppercase
s = [Link]() # convert string to lowercase
s = [Link]() # convert string to lowercase
s = [Link]("!", " 2023!") # replace an old value with new one
s = [Link]() # return the trimmed version of the string
c = [Link]("c") # get the number of occurrences of a value
x = [Link]("come") # return the index of a value
x = [Link]("come") # return the index of a value
Function
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