university python course unit 1

     University of The People

Learning Journal Unit Week 1

Mark Sikaundi


PART 1


  1. If you are trying to print a string, what happens if you leave out one of the quotation marks or both and why? Once you leave a quotation mark or both you will get this error saying  “SyntaxError: unterminated string literal (detected at line 1)” meaning it will show the line, where the error is and exactly what you are missing. The reason is that the python developers have assigned each and every algorithm on how python codes should be written, so a string cannot be written without double quotation marks, each string should have either single quotation marks or double quotation enclosed.

  2. You can use a minus sign to make a negative number like -2. What happens for each of the following and why?

    >>> 2++2

4

With this line of code it will print 4 simply by  adding 2 plus 2 in order to get 4 and this just shows that, if the number is written like 2++2 is a positive.

    >>> 2--2

4

This one as well will print 4, because 2--2 simply showing that the numbers are all negative unary operators

    >>> 2+-2

This one will print zero, because it has the positive unary operator

    >>> 2-+2

0

The same applies to this question it has a unary minus operator

  1. In math notation, leading zeros are OK, as in 02. What happens if you try this in Python and why?

02 will give this error “SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers” Because python is not recognized to be used.

  1. What happens if you have two values with no operator and a space in between them and why?

You will get this error saying “SyntaxError: invalid syntax. Perhaps you forgot a comma” meaning the interpreter is trying to understand what you have written. In any case if I had put 4 5 it would give me an error saying you left a comma.

 

PART 2

Next, describe at least three additional Python experiments that you tried while learning Chapter 1. Show the Python inputs and their outputs, and explain what you learned from the results of each example. 

The additional experiment i tried while learning in chapter one was to try this 

i)             >>  a = 20 - 10j

               >>  print(a)

               >>   (20 - 10j) as a complex number

 

ii)           >>   print(type(a))

              >>   (class  ‘complex’)

 

iii)         >>    a = complex(10, 10)

  >>   print(type(a))

  >>   (Class ‘complex’)

 For all the experiments I was trying, it was a test based examining how python can be useful to do math calculation in situations where one does not have a calculator present. This was so helpful to see how  to work around with complex numbers using python.

 

 

 

Reference:

Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea Press

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Post a Comment

Previous Post Next Post