Python variable creation is very easy! You don’t have to define the datatype of a variable before creating it! All you need to do is think of a meaningful name and assign a value to it. The value assigned to the variable determines its datatype.
Rules for user variable names in Python
- User variables must begin with Alphabet
- Underscores and numbers can be used only after the first alphabet
Camel Casing: The preferred way to create variables
CamelCasing is the style of creating variable names which are easy to read. so instead of naming your variables like x, y, z, foo, bar! Use better names which can help you understand your own code after six months!
For example if you are storing dates in some variables, then, names like “DateOfBirth” or “Date_of_Birth” are easily understood as compared to “d1” or “d2”
Sample valid python variables
|
1 2 3 4 5 6 7 8 |
# Defining some valid python names UserValue=10 UserName='Ram' User1='Ravi' User_Name='Ram' |
Sample Invalid python variables
|
1 2 3 4 5 6 7 8 |
# Starting a variable name with a number is NOT allowed 1User='Ram' # Starting a variable name with special character is NOT allowed .User='Ram' # Starting a variable name with special character is NOT allowed ^User_Name='Ravi' |
Python variable datatypes
There are many variable types in Python 3. Here is a list of the most common types:
- bool (Boolean)
- int (Integer/Long)
- float (decimal values)
- complex
- string
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# Creating variables in python # Creating an Integer variable intValue = 4 # Creating a Float variable floatValue = 1.5 # Creating a long variable longValue = 121212121212121212121212121212121212121212121212121212121212121 # Creating a complex variable complexValue = 1j # Creating a float variable floatValue1 = 1/3 # Creating a Boolean variable falseCondition = False trueCondition = True # Creating a string variable # you can use single, double or triple quotes for creating string variables strValue1='Hello Python!' strValue2="hello Python!" strValue3='''Hello Python again!''' |
Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!
