[003] Introduction to Python Basics: Statement and Expressions, Augmented assignment operator, String, Type Conversion

Koay Yong Cett
4 min readJun 20, 2020
Photo by Brett Jordan on Unsplash

Let’s learn another important key term which is expressions and statements.

Let’s look into the example code given:

Expression:

  • The expression in this example code highlighted by yellow colour in the figure above. It’s a piece of code that produce a value. In our case, this produces a value of 20 when 100 divided by 5.

Statement:

  • Statement is the entire line of code that performs some sort of action. In this case, we are performing the division of 100 by 5 and then assigning that value to the variable (user_height).

Augmented assignment operator:

  • This is the normal way of adding up the number.
  • Then, this is the second method but still yielded the same result(7).
  • Same thing as before, this is the augmented assignment operator (+=) which is a short hand of doing the addition. It is done with shorter and simpler code.
  • What if we do this at the beginning, we execute the code and will get the error. This is because ‘some value’ is not defined. We haven’t created anything yet. In order for the augmented assignment operator to work, we need to make sure that the variable has some previous value.
  • Here, we have a list of augmented assignment operator in figure above.

String(str):

  • Let’s talk about the string data type in Python. A string is simply a piece of text and it can be written in the ‘’ or “” mark. Well, we can observe the colour changes in side the ‘’ mark in the figure above.
  • We can use the type() function here to check for the type. In the figure above, the text is shown as a type of string.
  • The third way to write string in Python and it is used for long string. We start and end with three single quote(’’’). This is really useful for long sentences.
  • The string is also able to combine into a sentences by using the addition sign which we seen in the number operation.

String Concatenation:

What is string concatenation? Well it simply means adding strings together.

  • Here is an example of string concatenation.
  • This will not work because string concatenation only works with strings.

Type Conversion:

Remember that string (str) is actually a built in what we call a function or a built in action that we can use.

  • Now what happens if I do str(100) and clearly that 100 is an integer. I still get a 100 in the result but this is an int or string?
  • We are using type to evaluate the code. The int 100 is converted to string.
  • Even more bracket now and the type of 100 is converted back to int.
  • This code is same as the code that are commented out and it yielded a class int. This is basically idea of type conversion.

Thanks for reading my stories 😃

--

--

Koay Yong Cett

A Bachelor CS student with major in Network Security (UniSZa). Every stories I shared is based on my personal opinion. Thanks you. Having my Internship now.