100 Days of Code Day 13

Hello! My name is Rick and I am in search for gig as a developer :)

This past December I finished up a year long study with a full-stack school in NYC called Codeimmersives. We explored HTML, CSS, JavaScript, and how to build applications using the MERN stack. I learned about class-based components, passing props around (prop-drilling) and utilizing the component lifecycle(componentWillMount, componentDidMount, etc). After learning class based components, we moved on to functional based components using hooks, custom hooks, and different ways to manage state, such as the useContext and Redux APIs. (This is just a brief overview of the technologies I studied over the past year). Currently I am enrolled at devCodeCamp learning the basics of Python. I am documenting my journey by sharing these blog posts. Please help me out by dropping a comment below on what you think!

Loops in python

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

The above loop works the same for strings as it does lists.

The range() function

# starts at 0 increments by 1 (default) till 6 (not including 6)

for x in range(6):
  print(x)


# starts at 2 and runs till 6 (not including 6)

for x in range(2, 6):
  print(x)


# starts at 2, runs till 30 (not including 30) increments by 3

for x in range(2, 30, 3):
  print(x)

Functions in python

def add_two_numbers(first_number, second_number):
   result_sum = first_number + second_number
   return result_sum

Void function - is a function that does not send anything back (no return)

Pure function - a function whose output value follows solely from its input values, without any observable side effects

modules

files with the “.py” extension containing Python code that can be imported inside another Python Program.

In simple terms, we can consider a module to be the same as a code library or a file that contains a set of functions that you want to include in your application.

Function exercise

I know these may seem basic but this is just meant to be an introduction to functions in python

# Example function:
def display_name(name):
    print(name)
    #  this is the logic of the function

# The above function takes in a variable, known as the parameter.
# In this example, that variable is name.
# The function then prints to the console the value that is passed in


display_name('Mike')
display_name('Ian')
display_name('Nevin')

# Above we are now calling the function. This means using the function that we wrote.
# Here we are passing in an actual value. In this case, the value is 'Mike'

# Example 2


def add_one_to_number(number):
    number_one = 1
    add_one = number + number_one
    return add_one

# The above function takes in a variable, known as the parameter.
# In this example, that variable is number.
# The function then adds one to the parameter and returns the sum


result = add_one_to_number(30)

# Above we are now calling the function. This means using the function that we wrote.
# Here we are passing in an actual value. In this case, the value is 30
# We create and set a variable equal to the function call becuase the function returns a value

# Problem 1
# Write a function that takes in two numbers
# The logic of the function should add those two numbers together and return a sum
# Capture the returned value in a variable and print it to the console

def add_two_nums(num1, num2):
    result = num1 + num2
    return result

add_two = add_two_nums(5,11)

print(add_two)

# Problem 2
# Write a function that takes in two strings
# The logic of the function should concatenate those two strings together and return the concatenated result
# Capture the returned value in a variable and print it to the console

def concat_two_strings(str1, str2):
    result = str1 + ' ' + str2
    return result

concat_two = concat_two_strings('Rick', 'Rieger') 
print(concat_two)   

# Problem 3
# Write a function that takes in a string
# The logic of the function should print each character of the string one at a time to the console
# HINT: for loop is one way to do this

def print_each_char(str):
    for char in str:
        print(char)

print_each_char('devCodeCamp')


# Problem 4
# Write a function that takes in a string
# The logic of the function should print the string to the console but only if that string has three or more characters in it
# If it is less than three characters, then print to the console 'we need a larger string to print!'



def is_less_than_three_chars(str):
    if len(str) >= 3:
        print(str)
    else:
        print('we need a larger string to print!')

string_to_be_evaluated = input('Provide a string to print to console  ')

if string_to_be_evaluated:
    is_less_than_three_chars(string_to_be_evaluated)

Lists

A ordered collection, data structure that can be manipulated

my_list = [1,3,4,3,6,4]

# value can be changed
my_list[3] = 44

# the append() function

adds to end of list

append() method

my_list.append(33)
# [1,3,4,3,6,4,33]

##pop() method

removes from last index or the index passed

my_list.pop()
# [1,3,4,3,6,4]


my_list.pop(2)
# [1,3,3,6,4]

remove() method

removes item passed in

my_list.remove(6)
# [1,3,3,4]

len() method

gets length of string or list

num = len(my_list)

loop over list

for num in my_list:
    print(num)

Ex 1 with lists

# 1.    Write a function that has one parameter. When you call the function, you will be passing in a list of strings. 
# a.    Print to the console the value at the 0 index of the list
# b.    Return the value at the 0 index of the list

def my_func(list_of_strings):
  single_item = list_of_strings[0]
  print(single_item)
  return single_item

print('#####  Exercise1  #####')  
result1 = my_func(['sometimes', 'I', 'like', 'to', 'play', 'music'])
print(result1)

# 2.    Write a function that has one parameter. When you call the function, you will be passing in a list of strings that represent different colors -- create a list and append the following values into the list: “blue”, “red”, “white”, “green”, “yellow”
# a.    Prompt the user to enter a color.
# b.    Iterate over the list. If the user inputted color matches any of the colors in the list, print to the console “You found my chosen color!”

def my_func2(colors):
  list_to_append = ['blue', 'red', 'white', 'green', 'yellow']
  for color in list_to_append:
    colors.append(color)
  users_color = input('Please provide a color:')
  for color in colors:
    if users_color.lower() == color.lower():
      print('You found my chosen color!')


print('#####  Exercise2  #####')  

print(my_func2(['black', 'orange', 'brown', 'pink', 'gray']))

# 3.    Write a function that has one parameter. When you call the function, you will be passing in a list of numbers.
# a.    Iterate over the list and add up all of the numbers inside of it
# b.    If the sum of the numbers is even, return string “Even” from the function
# c.    If the sum of the numbers is odd, return string “Odd” from the function

def add_all_nums_in_list(numbers):
  total = 0
  for num in numbers:
    total += num
  if (total % 2) == 0:
    print('Even')
  else: print('Odd')


print('#####  Exercise3  #####') 
print(add_all_nums_in_list([2, 6, 1, 3, 1]))


# 4.    Write a function that has two parameters. The first parameter will represent a list, the second parameter will represent a number.
# a.    The list that is passed in needs to be a list of numbers
# b.    Iterate over the list and print to the console each value in the list that is greater than the number parameter

def greater_than_this_num(list_of_nums, num_passed):
  for num in list_of_nums:
    if num > num_passed:
      print(num)


print('#####  Exercise4  #####')  
result4 = greater_than_this_num([2, 6, 11, 13, 1], 10)
print(result4)

Ex 2 with lists

# 1.    Write a function that has one parameter. When you call the function, you will be passing in a list of strings. 
# a.    Print to the console the value at the 0 index of the list
# b.    Return the value at the 0 index of the list

def my_func(list_of_strings):
  single_item = list_of_strings[0]
  print(single_item)
  return single_item

print('#####  Exercise1  #####')  
result1 = my_func(['sometimes', 'I', 'like', 'to', 'play', 'music'])
print(result1)

# 2.    Write a function that has one parameter. When you call the function, you will be passing in a list of strings that represent different colors -- create a list and append the following values into the list: “blue”, “red”, “white”, “green”, “yellow”
# a.    Prompt the user to enter a color.
# b.    Iterate over the list. If the user inputted color matches any of the colors in the list, print to the console “You found my chosen color!”

def my_func2(colors):
  list_to_append = ['blue', 'red', 'white', 'green', 'yellow']
  for color in list_to_append:
    colors.append(color)
  users_color = input('Please provide a color:')
  for color in colors:
    if users_color.lower() == color.lower():
      print('You found my chosen color!')


print('#####  Exercise2  #####')  

print(my_func2(['black', 'orange', 'brown', 'pink', 'gray']))

# 3.    Write a function that has one parameter. When you call the function, you will be passing in a list of numbers.
# a.    Iterate over the list and add up all of the numbers inside of it
# b.    If the sum of the numbers is even, return string “Even” from the function
# c.    If the sum of the numbers is odd, return string “Odd” from the function

def add_all_nums_in_list(numbers):
  total = 0
  for num in numbers:
    total += num
  if (total % 2) == 0:
    print('Even')
  else: print('Odd')


print('#####  Exercise3  #####') 
print(add_all_nums_in_list([2, 6, 1, 3, 1]))


# 4.    Write a function that has two parameters. The first parameter will represent a list, the second parameter will represent a number.
# a.    The list that is passed in needs to be a list of numbers
# b.    Iterate over the list and print to the console each value in the list that is greater than the number parameter

def greater_than_this_num(list_of_nums, num_passed):
  for num in list_of_nums:
    if num > num_passed:
      print(num)


print('#####  Exercise4  #####')  
result4 = greater_than_this_num([2, 6, 11, 13, 1], 10)
print(result4)