Python 3: Lesson 4 - Variables & Data Types
Python 3: Lesson 4 - Variables & Data Types

By CN00b | Python | 18 Sep 2019


Hi people,

In Python, to declare something, you need to declare a variable.

What is a variable?

A variable is just a reserved memory location, to store a value; is created in the moment when you store the value to it.

Comparative to other programming languages, there is no special command to declare a variable in Python.

Just like when you reserve a ticket for a concert. You want to go to a concert, find a convenable seat, book online and when you hit the confirm button, the variable is created.

There are some conditions you must keep count on, when you create a variable name:

 -a variable name cannot start with numbers
- variable names are case-sensitive
- a variable name can contain only numebers, letters and underscore
- a variable name starts ONLY with underscores or letters

If you wanna create a variable with multiple words, you must use underscore.

Example:

publish0x_cool

If you type " publish0x cool ", you'll get errors and the code won't work( SyntaxError: invalid syntax)

351665157-ccbaa17d6ab123b6cd2e7b79b7071ea4fcecd3e951a817d70d29eecbd003b5ad.jpeg

To store a value to a variable, we'll use the equal sign " = ".

publish0x = "cool"

The value can be changed by declaring another value ( remember, python code is interpreted).

publish0x = "cool"
publish0x = "nice"

Now, if you run the code, it will show you the value " nice". To verify this, you use the print function.

I use screenshots to discourage copy-paste. To learn Python, you must code, test, code. Always add comments to your code, it helps a lot, because you can reuse the code later.

Python allows you to asign a value to multiple varibles or to assign more values to multiple variables in one line.

I guess you wonder why x got one, and not two or three value? This is because in Python, the counting starts from 0.
if we have a list: apple, banana, cherry, potato. apple is 0, banana 1, cherry 2, potato 3. This is very important. To assign another value, you must specify it.

 

You can combine a text with a variable, by using the print function and the " + " operator.

w = "awesome"
print("Publish0x is " + w)

You can also combine variables between them, only with a condition:the value must be text with text(in Python, this is called concatenation), numbers with numbers( + sign works like an mathematical operator in this case)

If you combine text with numbers, you'll get errors.

351665157-8f76cca7831e3320553e7daa3ff0aa27f780f042b536ecdc3b427a4ac3107cd9.jpeg

Why you can't combine them? Because both are different data types, and you must convert them to work.

351665157-ccbaa17d6ab123b6cd2e7b79b7071ea4fcecd3e951a817d70d29eecbd003b5ad.jpeg

I'll mention only the most important data types, at least for an entry level.

There are several built-in data types:

Text types: str ( strings)

Strings are marked with single quotation or double quotation. Both are accepted: "string" or 'string'. If you do not put the quotations, it will give you a syntax error.

Numeric types: int (integer), float

Binary type: bool (boolean) : are 2 types, True or False

Sequence types: tuples, list, range

351665157-871de70eda03a37e5df37e7cc2fe18cc7fabfdf45e88c58053423373bda75f52.jpeg


Thanks for following. I suggest you to read all and to test the examples of code in interpreter and to add/create your own code to get used with the terms. To test a code, always use the print function.

See you in the next lesson,

Cheers!


CN00b
CN00b

IT Hardware/ Software Noob Crypto Enthusiast Mountains Explorer


Python
Python

In this blog, i'll provide various lessons and tips for begginers and not only (later) about Python (version 3.xx), how to get started, how to use it, how to build an AI, etc. I'll try to cover more parts, but easy to understand, maybe 1-2 lessons/day.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.