Python is cool.

100 Days of Code Challenge Day 11

Python is cool.

Photo by David Clode on Unsplash

Actually, I am a 'noobish' kind of guy and just wanted to have a catchy title :) I started learning python today and that is what this article is actually about. If you don't have a sense of humor, well.....sorry.

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). Now that I am finished with Codeimmersives, I have decided to review the basics of programming/computer science, and continue to build projects. I am documenting my journey by sharing these blog posts. Please help me out by dropping a comment below on what you think!

Preface

I decided a week ago to sign up for another coding bootcamp called devCodeCamp. Some of the technologies they teach will be somewhat new for me. (as listed on their web site)

  • C# .NET
  • Python & Django
  • JavaScript & React.js
  • HTML5, CSS3, Bootstrap
  • ASP.NET Core
  • Web Services & APIS
  • Databases & SQL
  • Git & GitHub
  • Redux & Redux Toolkit
  • Data Structures, Algorithms, Design Patterns

Python

Class starts Monday so I figured I would start becoming acquainted with Python.

1. Naming python files

First thing I learned about the language is the files end with 'py'.#

2. Bringing in other files

When you are working with multiple python files you can 'bring them in' to your main.py file by typing 'import (name of file) - with out the extension.

3. Debugging tool in VS Code.

I've actually never used this tool. (on the left of the editor with a play and bug symbol) When you use it for the first time, it prompts you to create a launch.json file. Do so.
Where the launch.json file says "program", you can change it to run from the a specific file if you like so it doesn't just run on the file you are currently working on.

4. First lines of code

import greeting

greeting.display_greeting()

hours_worked = 45

pay_rate = 15

over_time_rate = 1.5

if(hours_worked > 40):
  extra_hours = hours_worked - 40
  over_time = extra_hours * pay_rate * over_time_rate
  total_pay = 40 * pay_rate + over_time
else:
  total_pay = hours_worked * pay_rate




print(total_pay)

Importing the other python file that has a function which displays a greeting to the console, declaring variables, if statement, else statement, and some simple logic.

5. While loops

i=1
while i <= 5:
  print(i)
  i += 1

word = input('enter a word:')


for char in word:
  print(char)

The input() function...interesting little bugger that JavaScript doest have (at least to my knowledge)

x = input("Enter your name:")
print("Hello, " + x)

Declaring functions

def run_calculations():
  print(add_two_numbers(3,5))
  print(subtract_two_numbers(10,2))
  print(multiply_two_numbers_and_divide(5,8,2))

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

def subtract_two_numbers(num, num2):
  result = num1 - num2
  return result

def multiply_two_numbers_and_divide(num1, num2, num3):
  result = (num1 * num2)/num3
  return result



run_calculations()

def add_all_odd_numbers(numbers):
  grand_total = 0
  for val in numbers:
    if val % 2 != 0:
      grand_total += val
  return grand_total  


print('here it is===>',add_all_odd_numbers([1,2,1,4,3,3,2,4]))

6. First codewars in python

Your Job Find the sum of all multiples of n below m

Keep in Mind

n and m are natural numbers (positive integers)

m is excluded from the multiples

Examples

sumMul(2, 9) ==> 2 + 4 + 6 + 8 = 20

sumMul(3, 13) ==> 3 + 6 + 9 + 12 = 30

sumMul(4, 123) ==> 4 + 8 + 12 + ... = 1860

sumMul(4, -7) ==> "INVALID"

my solution:

def sum_mul(n, m):
    if n == 0 or n < 0 or m == 0 or m < 0:
        return 'INVALID'
    if n > m:
        return 0
    current_value = n
    result = 0
    while current_value < m:
        result += current_value
        current_value = n + current_value

    return result

Best practices?

def sum_mul(n, m):
    if m>0 and n>0:
        return sum(range(n, m, n))
    else:
        return 'INVALID'

The sum() function in python

a = (1, 2, 3, 4, 5)
x = sum(a)
print(x)

# result
# 15

The sum() function returns a number, the sum of all items in an iterable.

sum(iterable, start)

iterable Required. The sequence to sum

start Optional. A value that is added to the return value

a = (1, 2, 3, 4, 5)
x = sum(a, 7)
print(x)

# result  22

range() function

x = range(6)

for n in x:
  print(n)


# output:

# 0
# 1
# 2
# 3
# 4
# 5

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.

range(start, stop, step)

  • start

Optional. An integer number specifying at which position to start. Default is 0

  • stop

Required. An integer number specifying at which position to stop (not included).

  • step

Optional. An integer number specifying the incrementation. Default is 1

x = range(3, 6)

for n in x:
  print(n)

# output

# 3
# 4 
# 5
x = range(3, 20, 2)

for n in x:
  print(n)

# output

# 3
# 5
# 7
# 9
# 11
# 13
# 15
# 17
# 19

Final thoughts

Getting through the training videos was a bit slow for me since I have some experience with Java Script. The language thus far looks neat and clean, no semi colons, etc and everything is indented. Python has some interesting functions out of the box and I'm looking forward to see how useful they actually are. There are subtle differences from JavaScript like the 'or' operator or the & operator but not enough to be too confusing.