PythonPlaza - Python & AI

Python

IntroductionVariablesIf-else
Strings Functions while-loop
For-LoopListSet
DictionaryTuple Try..Except
Class/ObjectInheritancePolymorphism
File Handling

Python Variables:

Variables are names given to data that we need to store a specific value. There variables can be used to manipulate or process the data in our applications. So, variables are very important part of any programming language. For instance, if we need to store salary of an employee in our program, we can assign a variable by the name userSalary and assign a value to it.

After variable is defined, our program will allocate some computer storage space to store the data. You can access or modify the value assigned the variables. For example, the employee in our application can get promoted and his annual salary can go up. An new salary can be assigned to the variable. When you first declare a variable, you assign an initial value to it. This value can be changed in the program later.

There are certain rules and guidelines for using variables in Python. Here are some of the rules.

1) Variable names in Python can only contain letters, numbers and underscores. They cannot start with a number, but they can start with an underscore or an alphabet.
2) Python variables cannot have spaces. For example, a Python variable student number is not allowed, but you can variable name as student_number.
3) Python keywords cannot be used as variables like if, while, else, for.
4) Python variables are case sensitive. For example, variable student_record and Student_Record will be treated as 2 different variables.



Code Example 1:
#Declare the Variables
number = 1  
varStr="I am going to Dallas"
studentId= 3456
VarBool = True
varTemp= 98.68

#Print:
print(f"Variable varStr= {varStr}")
print(f"Variable studentId= {studentId}")
print(f"Variable varBool= {VarBool}")
print(f"Variable varTemp= {varTemp}")

#Output:
number = 1  
varStr="I am going to Dallas"
studentId= 3456
VarBool = True
varTemp= 98.68

Multiple Assignment

Python allows assignment of multiple variables in a single line. This helps to shorten the programs and make them easier to read. You need to separate the variable names with commas and do the same with the values as well.



Code Example 2:
#Declare Variables
varA, varB, varC=10,20,30

#Print:
print(varA)
print(varB)
print(varC)

#Output:
10
20
30

Built-in Data Types

Python has the following data types built-in by default, in these categories:



str Text Type
int, float, complexNumeric Types
list, tuple, rangeSequence Types
dictMapping Type
set, frozensetSet Types
bool Boolean Type
bytes, bytearray, memoryviewBinary Types
NoneType None Type

Setting Specific Data Type in Python

Python allows us to specify the data type by the following constructor functions.



ExampleData Type
x = str("Hello World")str
x = int(20)int
x = float(20.5)float
x = complex(1j) complex
x = list(("A", "B", "C"))list
x = tuple(("a", "b", "c"))tuple
x = range(6)range
x = dict(name="John", age=36)dict
x = set(("10", "20", "30"))set
x = frozenset(("A", "B", "C"))frozenset
x = bool(5)bool
x = bytes(5)bytes
x = bytearray(5)bytearray
x = memoryview(bytes(5))memoryview