Variables and data types (integers floats strings booleans) in python

Variables and data types (integers, floats, strings, booleans)

Variables in Python programming, like other programming languages, are a symbolic program data storage container that stores the relevant program variable values ​​in the computer’s secondary memory. Program variables declared in Python allow the programmer to store various variable values ​​declared in the program and manipulate data operations on them.

Variables and data types (integers floats strings booleans) in python

Variable Types and Declaration Methods in Python Programming.

Python Variable Declaration and Assignment Values.

Like other programming languages, in Python programming you do not need to explicitly declare the default data types of any variable. You can declare variable data types by assigning a value to integer, float, character, variable values ​​using the assignment operator = in a Python program.

Python Variable Declaration Example.

# Let’s assign a value to a Python variable.

id = 101

emp_name = “David”

company=”vcanhelpsu.com”

temp = 100.5

condi_value = True

Rules for variable declaration in Python.

Remember that variable declaration names in Python programs can use alphabetic small or capital letter characters (a-z, A-Z), decimal/numeric digits (0-9) numbers and underscore (_) special character.

Never start any variable in Python program with decimal or numbers.

Variable names in Python programming can be case-sensitive, which can have small letter or capital letter variable declaration data type elements. For example, (id, and ID both are different variable declaration methods).

Declaration examples of some valid Python variable names.

company_name = “vcanhelpsu.com”

est_year = “2021”

contact = 9413

Variable Assignment Types in Python Programs

You can assign the desired value to any program variable by using the = equal/assignment operator in Python programs.

In Python programs, you can reassign previously assigned program variable values ​​with new values.

Python programming provides you the features to assign more than one different data type variable values ​​at a time.

Python Variable Value Assignment Example.

# Assigning a value to a variable

p = 99

q = 18

salary =999.60

# Reassigning value to declared program variable in Python

p = p – 9 # Here the value of p will now be 90.

q = q / 3 # Here the value of q will now become 6 when divided.

# Multiple Variable Value Assignment Simultaneously in Python Program

p, q, r = 01, 67, 89 # Here multiple variable values ​​are assigned simultaneously in the declaration with the value p = 01, q = 67, r = 89.

Scope of Variables in Python Program.

Scope of declared program variable in Python Local or Global scope lets you set a scope of access of declared variable, that how far the scope of declared variable is accessible to the programmer in the current Python program.

Remember that if you declare or define the scope of a variable in Python program inside a function, then the scope of declared defined variable here is of local nature. While the scope of variables declared or defined outside any function in Python program is of global nature. And these functions are accessed locally and globally in Python program.

Scope of Variables in Python Program.

# Python global variable declaration

global_variable = 9

def gb_function():

# Python local variable declaration

local_variable = 3

print(f”variable scope function – global_variable scope = {global_variable}, local_variable scope = {local_variable}”) # Global and local Python function variable declaration

# Accessing Python variables

print(f”let display outside function – global_variable = {global_variable}”)

gb_function()

Python Static Variables.

There are no built-in static data type variables in Python programming, but if you want to declare a variable with static nature, which you do not want to change in the program, then you can write those static variables in large characters.

Python Static Variable Example.

# Python Static Variable Type

TEMP = 99.7

In Python programming language, every declared data type has its own storage method. Which instructs the program compiler or interpreter program translator how and where the programmer wants to use the data in the current program. Remember that in Python and every other programming language, predefined data types have their own declaration methods, and these determine the operations that can be performed by the programmer on the data type data as well as the memory storage location to be allotted.

Common data type declarations in Python programming.

Python Numeric Type Declaration.

Integer (int) data type – In integer data type, you can store any integer value without decimal point in a Python program variable.

Integer variable Example.

p = 9

Floating Point (float) data type – In floating data type declaration, you can store real numbers with a point after the decimal point as Python program variables.

Floating Variable Example.

y = 3.14

Python Sequence Type Data Declaration.

String (str) data type – In Python program, string variable declaration can be declared as a sequence of characters enclosed in single quotes (‘ ‘) or double quotes (“”).

python string variable example.

comp_name = “Vcanhelpsu”

List (list) data type – Represents a collection of data or information stored in a sequence of objects in a list, the data objects declared in the list can be of various data types.

Python List Variable Example.

integer = [9, 11, 67, 56, 44, 88 ]

Tuple (tuple) data type – Represents a sequence of objects stored in a Python list data type, but remember that Python tuple data types are not modifiable (i.e. they cannot be changed after declaration).

Python Tuple Example.

values ​​= (76, 34)

Python Boolean data type.

Boolean (bool) data type – Represents true or false values ​​in a Python program, the result will be in the form of true or false.

Boolean data type example.

condition = true

Python mapping data type.

Dictionary (dict) data type – Dictionary data type represents the key pair values ​​data type in Python program, where the values ​​of dictionary data type are unique and immutable in nature.

Python dictionary data type example.

Course = {‘name’: ‘Python’, ‘fee’: 999, ‘duration’:2 }

Python Set Data Type.

Set data type – Represents the unordered collection of unique elements data elements in Python program.

Python Set data type Example.

uniq_integers = {9, 5, 6, 10, 99}

Python None Data Type.

NoneType data type – In Python program, the value of a variable is declared as none data type with the absence of any values ​​or zero value.

NoneType data type Example.

outcome = none

Checking Python data type.

If you want to know the nature of a stored data type in Python. Then you can find out the stored data type of any variable or value declared in a Python program with the Python type() function.

p = 1

print(type(p)) # the Output is – <class ‘int’>

comp_name = “vcanhelpsu”

print(type(comp_name)) # the Output is – <class ‘str’>

condition = True

print(type(condition)) # the Output is – <class ‘bool’>

Python Data Type Conversion (Type Casting) Methods.

Python programming allows you to convert a program variable data type from one data type to another using built-in Python library functions like int(), float(), str(), bool(), etc.

# Convert float data type to integer data type

p = 99.10

print(int(p)) # the result is – 99

# Convert integer data type to float data type

q = 9

print(float(q)) # the result is – 9.0

# Convert integer data type to string data type

id = 101

print(str(id)) # the result is – “101”