Hello! This tutorial is for those who are new to python and have heard this term before but don't know what it means.
What is a Variable?
A variable is something that changes in your python program. For example, a variable could be the score in a game, your health in a game, your amount of lives in a game, or even letter/word data.
How to assign and edit variables:
To assign an integer(or a numeric variable), you should type something in this format, but use your own variable name and value. varname = 5. this is a valid value, and python will process it.
Boolean(true or false):
thing = False(ALWAYS CAPITALIZE TRUE/FALSE)
String(word/letter variable):
rep = 'ReP'
float(decimal number):
d = 4325343254.354356452642
to edit(you can only edit integers/FLOATS), you use += or -=.
to edit a string, you have to reassign the variable like so:
a = 'a'
a = 'b'
FINALLY:
F strings are the best (in my opinion) way to print variable values. to do this, you use the f string format. Here is what it looks like: print(f'{your_var} is the value of your_var!') The output would be your_var value.
If you enjoyed or were helped by this, please upvote!! Thanks.
Variables, Variable class, and F string tutorial.
Voters
Nice! But I will fix (and add) some information for your tutorial:
fu
adding unicode support) but not all (e.g. you can't usefb
if you want binary support), so the old.format()
is usually a good way. They all works the same!+=
and-=
, I want to add some more information, they are simply just the sugar syntax ofvarname = varname <operator> val
. E.g.Is the same as you type:
And you can also use it for any other arithmetic operators! (
+
,-
,*
,/
,**
,%
,//
, and so on.)And also, you can edit the variable value by using
varname = value
, not just with ints and floats, but with any other types! I wont give an example here since it's very easy.Finally, I think that the terms that you list is not just for Python beginner, but also for any programming beginner because it's the core of a programming language like Python (except the f-string).
@Wumi4 Thanks for your help! Sorry I did not include the information you mentioned. This is the first tutorial I've ever done. Thank you for your feedback :).