The Noobs of Python: Ep.1.0 - The Introduction

Ok so i thought it would be good to take step back and go to basics even more. Noobs like me who never had any previous programming experience need any help they can get and explanation on every step.
As we can see in the previous examples Variable is a character sensitive name assigned to a specific data/value.
Now we have many different types of data, some of the basic ones are: integers, floats, strings, lists and booleans

Lest start with integers. Integer is a whole number, not a fraction, for instance 2, 3, 57, 481, -9.
We can assign numbers to a variables and using function type() check what type that variable is.

#define a
a = 1
#print type of a
print(type(a))

output will be: int

#define b
b = 9
#print type of b
print(type(b))

Again output will be: int

We can perform basic calculations on these variables.

a = 1
b = 8
c = a + b
print(c)

Floats are numbers with fractions: 2.1, 3.8, 203.89. You can perform mathematical operations on them just like on integers.

a = 2.5
b = 3.7
print(a + b)

and our output would be: 6.2


Next are strings. String is a sequence of characters. It can be a name, number or any combination of characters. You can assign a string to a variable with single or double quotations: 'string1' "string2"

#define var first_name
first_name = "John"

#define var last_name
last_name = "Smith"

#print var first_name and last_name
print(first_name, last_name)

That program will print output: John Smith. Each data type behaves a bit differently with different operations. For instance if i would print above variables like this:

print(first_name + last_name)

The output would be: JohnSmith

Strings can be also numbers like: python random_number_string = '39762''
so when i call type() function it should return that variable random_number_string is a string

#define var random_number_string
random_number_string = '39762'

#print type of random_number_string
print(type(random_number_string))

And the output will be: str


Now we will get to lists and other data typed in later episodes but i would like you to pay attention to the input() function and how to change one data type to another.

When you assign variable to user input, that variable is a string unless you convert it.
For instance:

# provide fists input
input_var1 = input("Please enter data for var1: ")

# provide secund input
input_var2 = input("Please enter data for var2: ")

# print message + type of input_var1
print("input_var1 is a ", type(input_var1))

# print message + type of input_var2
print("input_var2 is a ", type(input_var2))

And here is out output:

pj@LT440S:~$ python3 input_test.py
Please enter data for var1: test1
Please enter data for var2: 123
input_var1 is a  <class 'str'>
input_var2 is a  <class 'str'>

Now lest say i want to perform a basic addition of two inputs.

>>> var1 = input("Please type a number: ")
Please type a number: 20
>>> var2 = input("Please type a number: ")
Please type a number: 10
>>> print(var1 + var2)
2010

As you can see instead of addition python just printed those 2 provided inputs next to each other like in our previous example with first and last name strings.
If you wan to add them together you need to convert var1 and var2 to integers or floats like this:

int(var1)
int(var2)
float(var1)
float(var2)

If you call a type function on above you will see that type has changed

>>> type(int(var1))
<class 'int'>
>>> type(int(var2))
<class 'int'>

You can also assign that conversion to new variable:

>>> var3 = int(var1)
>>> var4 = int(var2)
>>> type(var3)
<class 'int'>
>>> type(var4)
<class 'int'>

Now we can do calculations on them:

# First input
var1 = input("Please type a number: ")
# Secund input
var2 = input("Please type a number: ")

# Var3 = integer of var1
var3 = int(var1)
# Var4 = integer of var2
var4 = int(var2)

print(var3 + var4)
# Or you can print the result like this
print(int(var1) + int(var2))

And the output:

pj@LT440S:~$ python3 input_test2.py
Please type a number: 20
Please type a number: 10
30
30

Once again i would like to state i'm a uber noob of Python and learned the above through various other tutorials, practicing, experimenting and trying to create more and more complicated programs. The above examples were for me like magic so after i finally understood it i thought i would break it down and spoon feed other noobs like me who struggle to understand programming. Of course if something is still a bit unclear ask and I''ll try to answer or I'm sure @Miguel_Sensacion and many other more advanced people on this forum will be able to answer.

3 Likes