Week 1 - First Day of Learning Python and Markdown

Liaw Bei Le · March 26, 2021

Today was the first day of my programming journey with the guidance of my mentor, Soham. We will be focusing on learning about Python. We mainly covered the basics of Python, such as basic functions, operators, using variables, different data types, loops, and also did a few practice exercises as below.

Cheatsheets

I also learnt how to use markdown text syntax and on using useful online ‘cheatsheets’. These are the ones that I found and am using:


What I learnt:

1. Basic functions

  • print( )
    print("Hello world")
    
    • the print() function prints out the string (of texts) that is surrounded by “ “, in this case it prints Hello world
  • input( )
    name=input('Please enter your name')
    print(name)
    
    • the value on the right would be the input that the user types in after seeing the prompt Please enter your name (the string within the ‘ ‘ in the function), as we use input( ) function to ask for user input
    • the variable, which I named name, is assigned the value on the right as I used the = operator
    • I used print( ) function to print the variable name which was assigned the value that the user typed in.
      The user input will be printed.
  • type( )
    • gives the data type of the output, e.g. string / integer / float…
      name=input('Please enter your name')
      print(type(name))
      
      • If someone enters a name e.g. ‘John’, the output will show data type string

2. Math operators

  • ** to the power of
  • %
  • /
  • +
  • -
  • // “floor” division (rounds down to nearest whole number), removes any remainders after division
  • *
    print(6/3)
    print(4*5)
    print(5**2)
    print(11%2)
    print(1==2)
    print(1==1)
    1+2
    3+2
    
    • the first 4 lines of codes above prints the values within the brackets which are the result of the mathematical formulae within (respectively: division, mutiplication, to the power of, remainder of).
    • the 5th and 6th lines of code prints the result of the expressions as True or False.
      In this case, 1==2 is false since 1 is not equal 2, and the print( ) function gives the output False, while 1==1 is True and the print( ) function gives the output True
    • as we did not specify a print( ) function for the last 2 lines of code, the program only prints the result of the last line, 3+2, which is 5.

3. Variables

  • Store values into a variable
      number = 10+2
      second_number=number*5
      print(number, second_number)
    
    • the variable which I named number is assigned the value on the right, which is 12 (the sum of 10 and 2)
    • the next variable which I named second_variable will be assigned the value on the right, which is variable*5 and that is also 12*5, which is the result 60
    • the print( ) function prints out both variables, giving us their assigned values 12 60

4. Data Types

  • Boolean expressions: returns a value of True/ False
  • Comparisons ==, !=, is (different from ==)
    a = 3==2
    b = 3==3
    c = True
    print(a, b, c)
    
    • for the 1st line, on the right the expression is false, giving a result False.
      a being the variable on the left is assigned the value on the right, False
    • for the 2nd line, on the right the expression is true, giving a result True.
      b being the variable on the left is assigned the value on the right, True
    • for the 3rd line, on the right the value is True.
      c being the variable on the left is assigned the value on the right, True
  • is operator gives true if it refers to the same object
    (false even if they are equal, if they are not same)
    x = [1,2,3]
    y = [1,2,3]
    z = x
    print(x == y) # Prints out True
    print(x is y) # Prints out False
    print(z is x) # Prints out True
    
  • not operator -
    using “not” before a boolean expression inverts it
    print(not False) # Prints out True
    print((not False) == (False)) # Prints out False
    
  • Numeric Types
    int, float, etc
    int_number=12
    float_number=12.2
    print(int_number, float_number)
    
  • and operator
    #check if a and b is true
    #use if a and b:
    a=True
    b=True
    if a and b:
      print("a and b are true")
    
    • In the first 2 lines of code, we have 2 variables which were each assigned the numbers on the right (an integer (whole number) and a float (number with decimals))
    • For the last line of code, using print( ) funtion, we print the 2 variables and the output is their assigned values 12 12.2
  • Text – strings
    • Strings can be printed by using single ‘ ‘ or double quotation “ “ marks within the print( ) function
      print('This is a string with 1 quotation mark')
      print("This is a string with two quotation marks")
      string1='string1'
      string2="string2"
      print(string1, string2)
      
      • For the first 2 lines of codes, we print the strings within the print( ) function surrounded by ‘ ‘ or “ “
      • Output:
          This is a string with 1 quotation mark
          *This is a string with two quotation marks
        
      • For the 3rd & 4th lines of code, we have 2 variables which were each assigned the values on the right, which are strings
      • For the last line of code, using print( ) function, we print the 2 variables - Output:
          string1
          string2
        
  • Operators (conditions) – and, or
  • List: [1, 2, 3] and Nested List: [1, 2, [3, ‘hello’]]
  • Tuple: (1, 2, 3)
  • Dictionary: {key: value, key_2: value_2}
    my_first_list=[1, 2, 'apple', 'orange', True, False, 5, ['inside_list', 2, 3, 4]]
    print(my_first_list)
    
    • we named a variable my_first_list and assigned it the value on the right, which is a list as shown by values surrounded within [ ] square brackets.
    • This list has a list nested within it too as seen from [ ] within the [ ]
    • using print( ) function, we print the variable which gives the output of its assigned value, the list [ 1, 2, ‘apple’, ‘orange’, True, False, 5, [ ‘inside_list’, 2, 3, 4] ]
      countries=['Singapore', 'India', 'Malaysia', 'Australia']
      print(countries[0])
      print(countries[1:4])
      print(len(countries))
      countries.append('Japan')
      countries[2]='China'
      
    • 1st line: the countries variable is assigned to a list which is on the right (in this case it is a list of country names)
    • 2nd line: countries[0] takes the value at the 1st position of the list, as the position count goes incrementally starting from 0 in arrays.
      Here, it is the string Singapore
      Using print( ) I printed the value assigned to variable countries[0] which is the string Singapore
    • 3rd line: countries[1:4] takes the values at the 2nd to 4th positions of the list, as the position count goes incrementally starting from 0 in arrays & it is list[n:n-1] positions when retrieving values using arrays.
      Here, the values are the strings India Malaysia Australia which are in the 2nd to 4th positions Using print( ) I printed the values in the specified positions of the list assigned to variable countries which are the strings India Malaysia Australia
    • 4th line: len( ) function returns the length of the data, here it returns the length of the countries variable which was assigned to a list.
      The length of the list, which is the number of values inside the list, is retrieved by len( ).
      As there are 4 strings in the list, len(countries) gives the result of 4.
      Using print( ), the output 4 is printed.
    • 5th line: we used the append attribute of a list to add a string at the last position of the list that was assigned to variable countries, by using countries.append(‘Japan’), the countries variable now is assigned to a list [‘Singapore’, ‘India’, ‘Malaysia’, ‘Australia’, ‘Japan’]
    • Last line: countries[2]=’China’ changes the value in the 3rd position of the list, ‘Malaysia’, to ‘China’.
      The countries variable changes to a list [‘Singapore’, ‘India’, ‘Malaysia’, ‘Australia’, ‘Japan’]
      As it is on the last line, although there is no print( ) function used, this list is printed out.
      numbers=[1284, 2480, 482034, 38405, 38402840, 47284, 38980, 17390]
      for number in numbers:
      if number%5==0 or number%2==0:
        print(number)
      
    • 1st line: the numbers variable is assigned to a list which is on the right (in this case it is a list of integers)
    • 2nd line: in the list assigned to the variable numbers, for each element in the list from left to right (one by one), assign the current element to a variable which we call number
    • 3rd line: if the remainder of the element (number) after division by 5 or by 2 is 0,
    • 4th line: print the element (number) within the numbers list that fulfils this criteria. Repeat 3rd & 4th line for the next element in the list.
    • In this case, those elements (number) within the list which are divisible by 5 and 2 exactly will be printed, and the resulting output is 1248 2480 482034 38405 38402840 47284 38980 17390

5. Loops / conditions - if, elif, else

if a == b: print("a is equal to b")
  • Simple calculator program:
    # Program asks user for 2 numbers 
    # and a operation (sum, subtract, power, div)
    # then prints the output
    number1=int(input('Number one: '))
    number2=int(input('Number two: '))  
    print(type(number1))
    a=input("Operation: ")  
    if a=='sum':
      print(number1+number2)
    elif a=='minus':
      print(number1-number2)
    elif a=='times':
      print(number1*number2)
    elif a=='divide':
      print(number1/number2)
    else:
      print('Try again')
    
    • For the first 2 lines of codes, using the input( ) function, I prompted the user who will see strings Number one: and Number two: to input values
    • as I used the int( ) function, I specify that the user must input integer values only
    • I assigned variables number1 and number2 to the integer values on the right which the user will input
    • 3rd line: type( ) function prints the type of the value assigned to variable number1, in this case its an integer value, therefore <class ‘int’> is printed
    • 4th line: I prompted the user who will see string Operation: to input a value.
      variable a is assigned to the value on the right which the user inputs.
    • 5th & 6th line: if the value of a equals to the string sum, print the value of the result of number1+number2 using print( )
    • 7th & 8th line: else if the condition in the previous lines is not met (a is not equals to the string sum) but is equal to the string minus, print the value of the result of number1-number2 using print( )
    • 9th to 12th lines: similar to 7th & 8th line’s logic but for a equals to times or divide respectively and printing the results of the expressions stated within print( )
    • Last 2 lines: if none of the criterias stated above were met/ were true, print the string indicated which is Try again
  • Simple calculator program using list:
    operations=['sum', 'minus', 'times']
    number1=10
    number2=20
    for operation in operations:
      if operation=='sum':
          print(number1+number2)
      elif operation=='minus':
          print(number1-number2)
      elif operation=='times':
          print(number1*number2)
      else:
          print('Try again')
    
    • 1st line: I assigned the variable named operations to the list on the right, which is a list of strings (of operations)
    • 2nd & 3rd lines: I assigned variables number1 and number2 to the values on the right which are 10 and 20 respectively
    • 4th line: for elements in the list, assign them to a variable which we call operation
    • 5th line: if the element (operation) is the string sum,
    • 6th line: print the results of the expressions stated within print( ), in this case it is number1+number2.
    • 7th & 8th line: else if the condition in the previous lines is not met (operation is not equals to the string sum), but is equal to the string minus, print the value of the result of number1-number2 using print( )
    • 9th & 10th line: else if the conditions in the previous lines are not met (operation is not equals to the string sum or minus), but is equal to the string times, print the value of the result of number1*number2 using print( )
    • Last 2 lines: if none of the criterias stated above were met/ were true, print the string indicated which is Try again

Thoughts

It was a fulfilling day of learning Python basics and I feel more confident in Python, as I practised on some basic programming exercises. I’m especially happy to have made a basic calculator program!

Additional Resource(s):

The site that I further used to obtain programming exercises to practise on is:

Debugging

In my practices, there were a few programming bugs, so I spent some time to look through my codes, thought about what possibly went wrong, and successfully debugged some of my programs that needed fixing.

Debugging was actually quite fun, as it allowed me to learn from my mistakes.

Twitter, Facebook