PYTHON PROGRAMMING CLASS (THE BASICS) || DAY 4 - PYTHON VARIABLES AND DATA TYPES | WEEK 1

in blurtafrica •  2 years ago 

Good day everyone!
Hope you're doing great!! Today is the fourth day of the Python Programming class week 1. If you read my first introductory article for week 1, you will know our fourth topic for the day 4 and all we have to cover for day 4.

I brought out this idea of teaching python on blurt because of the need for python programming skills in the world of technology today. This is the new trend in tech now because python can be used for various purposes in many applications ranging from web development, automation, artificial intelligence, software testing to many others. I decided to start this programming class on blurt communities because the communities comprise mainly of people who are still eager to learn new skills. Developers and non-developers alike can use this python.
Note: The English language used in this class is literally understandable by a layman. So let's begin...


PYTHON VARIABLES

Before we make use of any value in a program, we have to store it in a variable. Variables are like a container that holds our value. This is why we create variables for our values before we use it in our program. The reason is because the processor has to access these values. However, any data we make use of in our program has it's type. Some data types are built-in data types while some data types are user-defined data types.

Because python is dynamically typed, you don't need to declare a variable with a particular before making use of it's value, no, python doesn't have command for that unlike other programming languages like C or C++ where you must declare a type of variable before making use of the value it holds. In python, a variable is created the moment you first assign a value to it.

For Example;
a) In C++, if we want to create a variable to hold any value, we have to declare the type of variable we're creating.

int a = 5
float b = 5.0
str name = "Anyiglobal"

In the example above, a, b, and name are all variables. But 'a' is an integer type variable, 'b' is a floating type variable, 'name' is a string type variable. However, the numbers 5, 5.0, and Anyigloabl are the values we're storing in the variable we just created. The data types int, float, and str infront of the variable names will be explained later in this class.

b) In python, variables are created as soon as we assign a value to it. Declaring the type of variable to be created is not done in python. Python has no command for declaring a variable with any particular type. That is how the syntax of the language is written.

a = 5
b = 5.0
name = "Anyiglobal"

Can you spot the difference between our C++ example and our Python example? Yes of course I know you can, In our C++ example we have data types like int, float, and str infront of our variable, but in our Python example we do not have such. Note that a variable can still change type after they have been set. This can be done by just changing the value stored in the variable. for example, we can change the value of variable 'a' from 5 to "Anyiglobal" and it's type will change from integer type to string type automatically.

You can practice this in your Vs code as follows to see the result. Output the result using the print function, like this; print(a, b, name) and you will see the result down in the terminal.

python variables.PNG
Python variables

As we can see from the program above, we didn't declare the variables with any particular type before using their values in the program. If you try to declare the variables with any particular type as it is done in C++, the python interpreter will thrown you a SyntaxError.

python variable with declaration.PNG
python variables with declaration results to errror

Make sure you practice along with me using your text editor. If we look closely at the program in the screenshot above, we got a SyntaxError in the terminal when we wanted to execute the program in the source code environment. This is because we tried to declare a variable type for the various variables in our program, which is unacceptable in Python syntax! Source code environment is the space or environment where you type your codes in the text editor, and the Object code environment is the space or environment where you run your program. Know the difference between the two.

VARIABLE NAMES
Variable names has descriptive names like (car_name, name, fruit, age, phoneNumber, e.t.c.) or short names like (x, y, a, b, e.t.c.). There are some rules to follow when naming a variable. Some of the rules are listed below:

  • Variable names cannot start with a number, (e.g. 2name = "Anyiglobal") is an invalid variable name.
  • Variables names must start with a letter or an underscore, (e.g. x or _x) is valid.
  • Variable names can only contain underscores or alpha-numeric characters (e.g. 0-9, _, and A-z)
  • Special characters are not allowed as a variable name (e.g. $, #, %, e.t.c.)
  • Keywords are not used as variable names (e.g. for, if, def, e.t.c.).
  • Variable names are case sensitive (age, Age, and AGE are three different variables).

CONCATENATION
Concatenation simply means joining two things together. We use '+' character to achieve concatenation in python and some other programming languages.

For Example,

x = "Awesome"
print("Python is " + x)

Output
Python is Awesome

OR

x = "Python is "
y = "Awesome"
z = x + y #Concatenating two variables
print(z)

Output
Python is Awesome

NB: Try it out in your text editor. If you encounter any error let me know in the comment section!

NB: The easiest way to learn is to practice along!


The '+' character can also serve as a mathematical operator between two numbers.

For Example,

x = 5
y = 3
print(x + y)

Output
8

NB: Try this out in your text editor. If you encounter any error let me know in the comment section!


NB: Note that you cannot combine a string and a number, Python will throw a TypeError if you try to do so.

For Example,

x = 5
y = "Anyiglobal"
z = x + y
print(z)

Output
Error

python string and number concatenation.PNG

NB: Try this out in your text editor. If you encounter any error let me know in the comment section!

We have many ways of concatenating texts, and variables in a program but you will learn other ways as we proceed through this course. So please make sure you follow the series till the end. And please anything you don't understand, do well to reach me in the comment section, or you can reach me on my discord channel via anyiglobal#3876.

Use this material or this material to read and learn more about Python variables!


PYTHON DATA TYPES

Data types are used to denote the type of data to be stored into the variable. Python has several built-in data types that comes with the software. And every of these data types has various sizes they occupy in the memory. We have different classifications of data types in python. These classifications is tabulated below:

Text str
Numeric int, float, complex
Sequence list, tuple, range
Mapping dictionary
Set set, frozenset
Boolean bool
Binary types bytes, bytearray, memoryview

NUMERIC DATA TYPES
We have several classifications, but for the sake of this class we will discuss about Numeric data types. Numeric data types mainly comprises of numbers, and they are called Python Numbers.

We have 3 types of Numbers:
a) Int: These are integer numbers. Integer numbers are whole numbers. They are numbers without fractional part (i.e. there's no decimal point and set of digits after the whole number). Integer numbers ranges from negative to positive whole numbers. Example; -100, 10, 50, ..., n. It can range upto 1 million numbers or more depending on the size of the memory in the computer.

For Example,

x = 5
y = -39
z = 100000
print(x, y, z)

The above variables are all integer data types because they don't have any fractional part i.e. decimal point and set of digits after the whole number.

NB: Try this out in the text editor.

b) Float: These are numbers that contains fractional part i.e. they have decimal points and set of digits after the whole number. 1.8 * 10308 is the maximum floating point value a variable can hold. Example of floating point numbers are; 5.0, 2.55, 6.37, e.t.c..

For Example,

x = 5.0
y = -4.21
z = 6.4596
print(x, y, z)

The above variables are of float data types.

NB: Try this out in the text editor.

c) Complex: Complex are mainly numbers that have an imaginary part. If you did mathematics in your secondary school, you must be familiar with complex numbers. They are being represented in the form of a+bj, where a is the real part and b is the imaginary part. Examples of complex numbers are; 2+5j, 3+2j, 9+6j, 7+1j, e.t.c.

For Example,

x = 2+5j
y = 5+9j
z = 6+1j
print(x + y + z)

Output
(13+15j)

python complex numbers.PNG
Complex numbers

From the screenshot above, we can see that python added the three complex numbers together to give us an output in the terminal.


Verifying the type of data types
You can verify any data type to know it's type using the 'type()' function.

For Example,
To verify integer data type use the following:

x = 5
print(type(x))

Output
<class 'int'>

python integer type function.PNG
integer data type


To verify float data type use the following:

x = 5.08
print(type(x))

Output
<class 'float'>

python float type function.PNG
float data type


To verify complex data type use the following:

x = 3+5j
print(type(x))

Output
<class 'complex'>

python complex type function.PNG
complex data type

NB: Try this out in the text editor.


NB: If you want to read more about data types click here.



Conclusion

Python variables and data types are very important to know as a programmer. Variables serves as a container for holding our values throughout the program as we discussed in the class. However, these variables can also change type by reassigning new value to it. We also talked about data types in the class. As discussed, we have several classifications of data types in Python but the one we touched is Numeric data type. In numeric we have 3 numbers namely; int, float, and complex numbers. integers are whole numbers ranging from negative to positive whole numbers. Floating numbers are numbers with decimal points and a set of digits after the whole number. And finally, complex numbers are numbers that has real part and imaginary part.



This is the end of this particular class. I believe that if you followed this class till the end, you must have grabbed one or more information from the class. Please make sure to practice along with your laptop and text editor to grab every bit of the information passed.

Please do well to follow this blog, and also don't forget to reblurt this post so that it can reach larger number of blurtians who wants to learn this skill. Tomorrow, we will look into "Python String, and Operators".


Question of the Day: Write a python program that concatenates your 'first name' and your 'last name'?


rule:

  • Use your text editor to write the program
  • Show the screenshot of the program in the comment section

Prizes
first position: 20 BLURT
second position: 10 BLURT



Am glad you participated in this class! I believe you have learnt something new today.

I am @anyiglobal, a Computer Scientist, Software Engineer and a Blogger!



Source

Source to the post

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!
Sort Order:  

Hi, @anyiglobal,

Thank you for your contribution to the Blurt ecosystem.

Your post was picked for curation by @onchain-curator.


Please consider voting for the witness @symbionts.
Or delegate to @ecosynthesizer to earn a portion of the curation rewards!