0% found this document useful (0 votes)
16 views13 pages

Day 1

The document discusses various Python programming concepts including variables and data types, casting variables, assigning values to variables, printing variables, conditional statements, loops, arrays and string methods. It also covers defining functions, including recursive functions to calculate factorials. Key concepts are explained with examples.

Uploaded by

Hein Khant Thu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views13 pages

Day 1

The document discusses various Python programming concepts including variables and data types, casting variables, assigning values to variables, printing variables, conditional statements, loops, arrays and string methods. It also covers defining functions, including recursive functions to calculate factorials. Key concepts are explained with examples.

Uploaded by

Hein Khant Thu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

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(a.format(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
cars.append("Honda") # add new item into the array
Array and its functions - cont

chinese_cars = [ "GAC", "FAW" ]


cars.extend(chinese_cars) # combine two arrays
cars.pop() # remove the last element
cars.pop(0) # remove the element by index
cars.remove("GAC") # remove the first occurance of the
element by value
cars.index("Honda") # get the index of the first occurance of
the element by value
cars.clear() # remove all elements
String and its methods

s = "Welcome to Coding Challenge! "


s = s.capitalize() # capitalize the first character
s = s.upper() # convert string to uppercase
s = s.casefold() # convert string to lowercase
s = s.lower() # convert string to lowercase
s = s.replace("!", " 2023!") # replace an old value with new one
s = s.strip() # return the trimmed version of the string
c = s.count("c") # get the number of occurrences of a value
x = s.find("come") # return the index of a value
x = s.index("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

You might also like