Variables and Data Types

What is a Variable

A Python variable is a reserved memory location to store values in other words a variables are containers for storing data values.

Declaring a variable

Python has no command for declaring a variable. When you initially assign a value to a variable, it is said to be created. Variables do not need to be defined with a specific type, and they can even change types after being set.

 age = 7         # i have assigned 7 to a variable called age thus the value of age is 7
 name = "john"   # i have assigned john to a variable called name thus the value of name is John

Variables do not need to be declared with any particular type, and can even change type after they have been set.

 y = 7       # variable y created with the value 7 and since 7 is an integer thus variable y is of type integer
 y = "paul"  # variable y created earlier has been assigned paul as its values and its a string thus y is of type string

Python Data Types

Data type is an important concept in programming. Variables can store a variety of data, and different types can perform different tasks. These are the basic type of data types

 age = 7              # int
 elevy = 1.5          # float
 name = "John"        # str using a double quote
 name = 'Paul'        # str using a single quote
 is_active = True     # bool

Every value in Python has a datatype. Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc. and these data types, which are built-in by default, can be categories as follows:

 Text Type:	 str
 Numeric Types:	 int, float, complex
 Sequence Types: list, tuple, range
 Mapping Type:	 dict
 Set Types:	 set, frozenset
 Boolean Type:	 bool
 Binary Types:	 bytes, bytearray, memoryview
 None Type:	 NoneType

Variables Rules

  • Variables names are case sensitive (age and Age are different variables
  • Variables must start with a letter or an underscore
  • Variables can have numbers but cannot start with one

Type casting

Type Casting is the process of converting a literal of one type to another in Python. For typecasting, utilize the built-in functions int (), float (), and str (). int () accepts a float or string literal as an argument and returns an int value.

 age = "7"
 age = int(age)  # converting age of string 7 to integer 7

 age = 7 
 age = str(age) $ converting age of integer 7 to string 7

Casting

Casting can be used to specify the data type of a variable.

 x = str(3)    # x will be '3'
 y = int(3)    # y will be 3
 z = float(3)  # z will be 3.0

Get the Type

The type() method returns the data type of a variable.

 age = 5
 name = "John"
 print(type(age))
 print(type(name))

Case Sensitive

Python is case sensitive when it comes to variable names.

 a = 17
 A = "John"
 NB: A will not overwrite a, A and a are two different variables

Multiple Assignment

Python allows you to assign a single value to several variables simultaneously. Y

 # Example 1
 # An integer object is created with the value 1, and all three variables are assigned to the same memory location. 
 a = b = c = 1

you can also assign multiple objects to multiple variables as follows:

 name, age, school, class, is_girl = ("sally", 7, "St Paul International", "pinky class", True)