Variables in Python

Variables are used for storing any data values.

Rules for naming a Variable

1.Variable name can be an alpha-numeric combination.

2.Variable name should not contain any special symbols except underscore( _ )

3.Variable name should always start with alphabets.

4.Variable names are case sensitive.

Example:

#Following are the legal/correct variable names 
    msg = "hello"
    MSG = "Hello"
    Msg2 = "Funda of web IT"
    msg_m2 = "Funda of web IT"

    #following are the wrong/illegal variabe names
    2msg = "Hello"
    msg-2 = "hello"
    msg 2 = "world"
How to create variables in Python

Python does not have any command or structure to declare variables. 

The Variable is created when it is first assigned with any value.

    a = 5
    user = "funda"

    print(a)
    print(user)

The output for the above code will be:

5
funda

Here, the variable "a" is of type int and the variable "user" is of type string. 

You can check the datatype of a variable by using the type()

    a = 5
    user = "funda"

    print(type(a))
    print(type(user))


The output for the above code will be:

<class 'int'>
<class 'str'>
How to declare the string variables

The string variables can be declared within single quotes(' ') or double quotes(" ").

    user1 = 'Funda of'
    user2 = "web IT"

    print(user1)
    print(user2)


The output for the above code will be:

Funda of
web IT

Both the variables user1 and user2 is correct and will perform the same task.


© Copyright - 2018 - 2021 | All rights reserved at Funda of Web IT