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.
If you type " publish0x cool ", you'll get errors and the code won't work( SyntaxError: invalid syntax)
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.
Why you can't combine them? Because both are different data types, and you must convert them to work.
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
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,