In this tutorial, you will learn about Python Variables. Variables are a container to store or hold the various type of data. Variables are kept your code more organized and maintained. Every programming language has a variable type and most are common in terms of definition, name, and property of variables.
Syntax
A Syntax of Python since Python 3.5 and above, you declare a variable type like this:
variable_name: type_name
Create a Simple Python Variables
Let’s do some code practice how to create variables in Python.
Text – Strings Variables
Give a name like “message” or any name, There some variable naming rule will learn later.
I will store a text in variable “message”.
message = "Hello, Buddy !" print(message)
Output: Hello, Buddy !
This how to create a String Variable.
- Don’t use the number before a variable name like
2msg
The string also has some inbuilt useful method, follow this link to know more in-depth: Python Strings Tutorial
Numbers Variables
phone_number = 1234567890 print(phone_number)
Output: 1234567890
For a detailed explanation about Numbers in python must follow this tutorial: Python Numbers & Types Tutorial
Python Variables Naming Rules
Python is a case-sensitive language, so it must define (naming) of the variable as proper. Here are some pythons variables rules :
- A variable name must start with a letter or the underscore character (like _var or mVar)
- It can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive (var, Var, and VAR are three different variables)
- Can’t start with a number
Do comment if you have any doubt and suggestions on this tutorial.
Note
: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6Python 3.7
All Python variables examples are in Python 3 version, so it may change its different from python 2 or upgraded versions.