Simply Basic Python — part 1

popy riliandini
3 min readJan 12, 2022

--

Why python? Sure, because python is one of the programming languages that is very close to human language. For beginners [like me], it can make your mood cheer up to understand this language.

Here, I want to share everything that I have learned before about basic python in several parts. I’ll give you the outline:

  1. Variable and its operation
  2. The powerful bucket python called List
  3. The helpful method called Function and their packages
  4. Numpy Array that is mathematic bucket python similar to List, but different

Variable

Write a name or alphabet or combine it with numbers without any space, then you can store any values into it. It is called variable.

Variable have different types, such as string, integer, float, boolean, list, etc (for more deeply and completely type, please browse it on the internet by yourself, I’m sorry 😅). And thankfully, the genius python does not need to define the type of variable. String, for simple understanding, imagine that string same as a text. You can input any character into a string. To make python understand that we have value with the type is a string, please give python signal with quote mark at the beginning and at the end of the value.

ex: var1 = “this sentence is a string and has 123 secret code”

Integer, same as the name, is all of the negative numbers and positive numbers. To write this type, it’s as simple as just writing the number.

ex: var2 = 99 or var2 = -99

Float, just same as integer but float especially for decimal number.

ex: var3 = 99,0 or var3 = 99,343

Boolean, type of variable that only stores False values. This type is usually used for if condition program or check some value True or False based on a condition. List, we will learn this type on the next story 😅.

Operation within variable

Same as mathematic calculation, we can calculate any value of the variable in python. But, not all types of variables can be combined in operation. Look at this simple example.

define

a = 5; b = 3; c = “random sentences”; d = True; e = False;

operation

syntax print( ) used for display the value

print(a + b) = 8

print(a-b) = 3

print(c*2) = “random sentencesrandom sentences” (string can be operated in multiplication, but can’t for other operation)

print(a / 5) = 1

print(a + d) = 6 (True on integer means 1, and False means 0)

print(d * e) = 0 (multiplication between boolean same as standard multiplication with value 0 stand for False and 1 stand for True)

print(a**3) = 125 (exponential operation)

print(10 % 3) = 1 (modulo operation)

Then, how to deal with a different type of variable in this mathematic operation? Python gives us a simple method to convert any type of variable. str( ) is used to convert a value into a string. And so on float( ), int( ), bool( ) will convert python values into any type. To better understand, please look at this example.

if you run code like this:

money = 100

print(“I like an apple, so I exchange my $” + money + “for apples”)

you will get an error because you add integer into a string. To handle this error, you can change the type of variable ‘money’ to string. Just like this:

money = 100

print(“I like an apple, so I exchange my $” + str(money )+ “for apples”)

And so on for another type.

The last but not least, a piece of additional information but absolutely you will use this method in many cases on programs. The type of variable can be known by using type( ) method. For example, I will use the variable ‘money’ that we define before.

type(money)

it will return int

--

--

popy riliandini

My writing shares what I want to share about something that I have learned before