Header Ads Widget

Responsive Advertisement

Variables and Data Types in python3

                  Variables in Python3

 





Variables :

Variables are containers which can store some data values in it. The values can be of any type like Integer, Decimal, Boolean, Character, String etc.
Since Python is a dynamically typed language, we don't need to mention the datatype of a variable unlike C, C++, Java programming languages.

Declaring a variable in python is very simple just type your variable name by following some naming conventions which we will see later and assign whatever values you want to assign.

x = 3          # Integer Value
print(x)

y = 3.9        # Floating Value
print(y)

z = "Python"   # String Value
print(z)

a = True       # Boolean Value
print(a)

b = False      # Boolean Value
print(b)


Naming Convention For Variables:

1. Variables should be mnemonic that is easy to remember or understand.
 For example if you have to assign a value of simple interest then there is no point of       creating a variable with names like "a", "b", "x" like this instead we can use variables like "simpleInterest" or "simple_interest", because they are easy to remember and resembles   according to our need.

 
2. Variables can't be a keyword. Keywords are reserved words which have specific meaning in python and using them as a variable can create perplexity while storing the data values in it.
The keyword cannot be used as an identifier, function and a variable name. There are 33 keywords in python written in lowercase except True and False. 


3. While using multiple words for a variable we should use only underscores as a separator and nothing else not even spaces.
For E.g. simple_interest, compound_interest, value_of_amount, etc.

4. Variable names can be alpha-numeric but it should always starts with a character or a underscore only. 
For E.g. constant, alpha, _value, value9 are some valid variable names but 9constant, +constant are invalid variables.

Variable names are case-sensitive that means age and AGE are different. 

 

Data Types:

Datatype of a variable tells us what kind of value we are storing and what operations can be performed on a particular data.
Basically in python we have 5 types of datatypes.
  1. Numeric Data Type : Integer, float, complex.
  2. Dictionary
  3. Boolean
  4. Set
  5. Sequence Type : String, List, Float.

Numeric Data Types :

In python numeric data type contains only numeric values like integer, float or complex values.
Integer values : 2, 3, 99, 100 etc.
Float values: 2.2, 3.45, 10.0 etc.
complex values: -2 + 3j  [ real no + (imaginary no)j ]

NOTE : type() function is used to determine the datatype of a variable.

x = 4
print(type(x))       # <class 'int'>
 
y = 10.8
print(type(y))       # <class 'float'>

z = "hello world"
print(type(z))       # <class 'str'>

a = True
print(type(a))       # <class 'bool'>

z = 2 + 3j
print(type(z))       # <class 'complex'>



Boolean:

Boolean datatype have only two values that is True and False. Sometimes when we have conditional statements then we use boolean values because a statement can be either true or false but can't be both.

x = True
print("This is a true statement", type(x))

y = False
print("This is a false statement", type(y))


We will learn about other datatypes as well in upcoming posts.

Thanks For Reading ❤






Post a Comment

2 Comments

  1. Very nice description

    ReplyDelete
  2. The concept is nicely explained in simple words thank you

    ReplyDelete