DEV Community

Cover image for VARIABLE:
bhuvaneswari nandhagopal
bhuvaneswari nandhagopal

Posted on

VARIABLE:

A variable is a named container used to store data values so they can be referenced and manipulated later in the program.

Variable Naming Rules:(Alphanumeric)

1.Variable Name Must Start with a Letter or Underscore:
Correct:

name = "Ram"
_age = 20
Enter fullscreen mode Exit fullscreen mode
Wrong:
1name = "Ram"
Enter fullscreen mode Exit fullscreen mode

2.Variable Name Cannot Contain Spaces:
Wrong:
student name = "Ram"

Correct:
student_name = "Ram"

Use underscore _ instead of spaces.

3.Variable Names Can Contain Letters, Numbers, and Underscore:

Correct:

name1 = "Bhuvana"
student_mark = 95
print(name1, student_mark)
Enter fullscreen mode Exit fullscreen mode

Output:
Bhuvana 95

4.Special Characters Are Not Allowed:
Example:
@,#,!,&...

5.Python Keywords Cannot Be Used as Variable Names:

Wrong:
class = 10
for = 20
(class, for, if, while) are Python keywords.

6.Variable Names Are Case Sensitive:
Example:

name = "Ram"
Name = "Sam"
print(name)
print(Name)
Enter fullscreen mode Exit fullscreen mode

output:
Ram
Sam
name and Name are different variables.

Good Variable Names:

student_name = "Ram"
total_mark = 450
age = 20

Good variable names make programs easy to understand.

Data Types in Variables:
1.Integer
2.String
3.floating-point
4.Boolean

integer:
An integer is a whole number that can be positive, negative, or zero, without any fractional or decimal part.

Example:

age = 20
print(age)
Enter fullscreen mode Exit fullscreen mode

Output:
20

Here, 20 is an integer.

Integers can also be used for mathematical calculations.

Example:

a = 10
b = 20

print(a + b)
print(a - b)
print(a * b)

Enter fullscreen mode Exit fullscreen mode

output:
30
10
200

Integer:
10
25

String:
n Python, a string means text or words. Strings are written inside single quotes ' ' or double quotes " ".

Example:

print("Hello")
print('Hello')
Enter fullscreen mode Exit fullscreen mode

Output:
Hello
Hello

Both single quotes and double quotes work the same way in Python.

name = "Bhuvana"
print(name)
Enter fullscreen mode Exit fullscreen mode

output:
Bhuvana

Wrong Example:
print(Hello)

Correct Example:
print("Hello")

Strings are very important in Python because we use them to store names, messages, sentences, and other text data.

Floating-point:
A floating-point number means a number with a decimal point.

It is also called a float in Python.

Examples:

10.5
3.14
99.9
5.0

These are floating-point numbers because they contain decimal point.
Example:

temp = float(input("enter your body temperature:"))   
if temp > 100:
    print("you have fever.")
else:
    print("you do not have fever")
Enter fullscreen mode Exit fullscreen mode

output:
enter your body temperature:118
you have fever.

*Boolean: *
A Boolean has only two values:

True
False

Boolean is used to check conditions.

True Example:

is_student = True
print(is_student)
Enter fullscreen mode Exit fullscreen mode

output:
True

False Example:

is_raining = False
print(is_raining)
Enter fullscreen mode Exit fullscreen mode

output:
False

Top comments (0)