[tut] Variables and stuff like that #002

in pxlfussel •  4 years ago 

Hey ho Folks,

it is me again teaching you some python!

Let us talk about something everyone of us knows since school. Variables.
But in programming languages variables does not only prevent numbers of being forgotten. They also store
things like Strings, lists and arrays.

But let us go in some deeper.

First of all start your editor and create a new file called tut2.py or what ever you want to name it.
now enter the following lines

def main():
    x = 10
    print(x)

main()

Ok start your terminal head to the folder where your file is saved and run it with:
python tut2.py

What you now will see, is the result of the lines you enter:
10

But let us talk about that. As you can see it starts like nearly everytime with
def main():
With that we define the main function. It is not really needed in python.
You also can just write:

x=10
print(x)

thats enough, but i think it is an unclean way to code and python code is hard to read, because
python didn't use {} brackets to organize functions, sections and so on.
!FOR PYTHON IT IS IMPORTANT THAT YOU USE TAB KEY TO ORGANIZE YOUR CODE!
If you write

def main():
print(x)

Python will throw an error, because print needs to be indented to make python know, that this line is part of the function.
But let's go on.
The next line does two things:
x = 10
It tells python to create a variable named x and give it the value 10.
The last two line are business as last time.
print(x) prints the value of the variable and
main() calls the main function because without we will see nothing.
In other programming languages you need to define a variable with a type.
In python there is no need to, because a variables type is defined by it's first assignment.
So this variable is now a number variable and will throw an error, if you try to assign an other value
as a number.

If you have mor variables you can do some operations with it. For numeral vars you can use + - * / to do calculations:

result = x +y

and so on.
But what happend if you have a variable which contains text?

Let's edit the last code and write:

def main():
    x = "Peter "
    y = "Farmer"
    name = x + y
    print(name)

main()

Ok what happens now? When you add to variables which contains text, the result is that the two words will stored together in a variable. Please notice that there is a space after Peter, so the result is Peter Farmer and not PeterFarmer.

The only types you need to define are arrays and python dictionarys.

array = []
dict = {}

Arrays are a container of different values of the same kind.
For example:
array = [10, 20, 30, 40, 50]

And dictionarys are an equivalent to json objects. But on both i will tell more, when we are getting deeper.

Ok that was really much stuff but
stay tuned and interested and you will see learning is an easy thing

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 STEEM!