[005]Introduction to Python Basics: Immutability, Built in Functions+Methods, Boolean

Koay Yong Cett
5 min readJun 22, 2020

--

Photo by Kimberly Farmer on Unsplash

We understand that string can be accessed quite easily. With the square brackets, we can access different parts of the string.

  • This is a idea of a [start:stop:step over] is what we usually refer as slicing or string slicing. This is because we can slice the string on our own preferences.
  • We also have to learn another important term that’s going to appear frequently. As a matter of fact that it’s an important concept when you get more advanced into programming. What is this concept?

Immutability:

  • It is an important term and what does immutability mean? Well, strings in Python are immutable which means that they cannot be changed.
  • I am able to reassign the hi variable with new value which is 100 and this will work.
  • If we want to replace/change the first index of the hi variable with 8, this will yield an error. Why is this happen because strings are immutable. Thus, I am not able to change the value in the hi variable after the string is created.
  • The only way that the value will be able to changed is the reassign of the whole string value not part of it.

Built-In Functions

source: https://docs.python.org/3/library/functions.html
  • Here is the list of built in function in Python and we learn few of built in functions before.
  • Let’s talk about few that we haven’t talk about it yet.
  • The len() which is used for counting the length of the string provided. Remember that length does not start with 0 like indexes instead it starts counting from 1 like human.

Another neat trick:

  • We are still able to yield the same results with this two line of code.

Built in methods:

source: https://www.w3schools.com/python/python_ref_string.asp
  • Instead of the built in function, we still have the built in methods. What does this means? Python also have the idea of methods which is similar to function but they are own by something else like string. Python has a set of built-in methods that you can use on strings.
  • All this methods can be used on the string data type.
  • Methods have the special syntax like the figure above. Now, why do we care? Well with this, Python gives us automatic tools that we can use on string.

Let’s explore some of the string methods:

  • Here, we have the upper methods to change all the characters in quote to upper case.
  • The differences between upper and capitalize method is that the capitalize method make the first character as uppercase.
  • We can see a whole list of method(purple box) available to string by pressing on the dot.
  • The find method tell us the index of where the “or” start.
  • The “or ” is replaced with the word “and”.
  • Whoa why the second line print function yielded the original string? This is because the string is immutable where they cannot be changed but can be overwrite.(something like create or destroy)
  • In this case, we create a whole new string which is quote2 and never modify the the original string. The quote will stay the same as the original string.

Boolean:

  • Boolean can be either True or False. Boolean represent this idea of true and false which in programming is kinda like zeros and ones. It can be used in some logic especially the conditionals. Boolean can be also used to control the flow of our program.
  • Do something if the things provided is true and do other thing if something is false.
  • We use boolean to denote logic. The bool(1) means that it is True while bool(0) means that it is False. We are just simply converting the integer into boolean value and boolean can be either True or False.
  • We will explore the boolean some more when we talk about the conditional logic.

Thanks for reading my stories 😃

--

--

Koay Yong Cett
Koay Yong Cett

Written by Koay Yong Cett

Every stories I shared is based on my personal opinion. Interest in ethical hacking and penetration testing. Thank you.

No responses yet