Data Types and Variables in Python @dev_code

Data Types and Variables in Python:

 

Python is a highly object-oriented language. In fact, virtually every item of data in a Python program is an object of a specific type or class.

Data types:

https://devmallikcode.blogspot.com/2023/04/data-types-and-variables-in-python.html
Abbreviation Type Definition Example
int Integers Positive and negative whole numbers, including 0 10
float Floating-point numbers positive and negative numbers with a decimal point 10.5
str Strings Sequences of characters, contained in either single or double quotes 'This is a string.' "This is also a string."
bool Booleans Truth values There are only two of Boolean type: True and False

 

Converting between types:

The following functions convert an object of one type to another.

  • int() converts a floating-point number or string of numerals to an integer

  • float() converts an integer or string of numerals (possible containing a decimal point) to a floating-point number

  • str() converts an object to a string

  • bool() converts an object to a Boolean value. Non-zero values and non-empty strings become True. Values equal to zero and empty strings become False.

More on floating-point numbers:

Floating-point numbers are sometimes given in scientific notation. For example,

 
1.23e4     

represents 1.23×104=12300.

You should also be aware that the internal representation for floating-point numbers is not precise. They should be used cautiously.

print(1.1 + 2.2)     
# We would expect 3.3 for this sum     

Variables:

In Python, a variable is a name given to an object. Variables are used to store different types of data in a program.

Rules for naming variables

Variable names can be any length. They can include lowercase or uppercase letters, numerals, and/or the underscore character (_). Note that lowercase and uppercase letters are counted as different characters, so variable names are case-sensitive.

The first character of a variable name cannot be a numeral.

Variable names cannot use any of Python's reserved keywords.

 

Assigning a value to a variable:

Use the equals sign (=) to assign a value to a variable.

Once a value is assigned to a variable, we can use the variable name in a statement or expression, and the value will be substituted.

# Assign the value 17 to the variable my_number 
my_number = 17 
# Print the value 
print(my_number) 

You can later assign a variable to a different value. This new value will affect any code that comes afterward. 

When assigning a new value to a variable, you can change its type as well.

my_number = 'seventeen' 
print(my_number) 

The function type() returns the type of an object.

my_number = 'seventeen' 
type(my_number) 

Since the variable was assigned to a string, the type() function returns <class'str'>.

For more on Variables in Python, read though the documentation here: https://realpython.com/python-variables/.

Join Me on Triberr, Referral link click here.

[Note: This blog is influence by Coursera.org]