Learn to Code via Tutorials on Repl.it!

← Back to all posts
Variables, Variable class, and F string tutorial.
gjirtngisnt (12)

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.

Commentshotnewtop
Wumi4 (459)

Nice! But I will fix (and add) some information for your tutorial:

  • There is a problem (not very big, but still can make inconvenience sometimes) with the f-string. It can combine with other char-string (e.g.fu adding unicode support) but not all (e.g. you can't use fb if you want binary support), so the old .format() is usually a good way. They all works the same!
  • For the += and -=, I want to add some more information, they are simply just the sugar syntax of varname = varname <operator> val. E.g.
var += 5

Is the same as you type:

var = var + 5

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).

gjirtngisnt (12)

@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 :).