Python Variables
Rakesh Kumar R
Exploring Tech & Career Growth | Java & App Developer | Ex - LT Technology Services Intern | Learning, Building & Growing
In Python, variables are used to store and manage data. You can create a variable by assigning a value to a name. Variable names must start with a letter or underscore and can contain letters, numbers, and underscores. Example:
# Creating variables
name = "John"
age = 25
is_student = True
# You can also assign multiple variables in a single line
x, y, z = 10, 20, 30
# Variable naming conventions
my_variable = "some value" # Use descriptive names with lowercase and underscores
# Constants are often named in uppercase
PI = 3.14
Variables in Python are dynamically typed, meaning their type can change during runtime. Python automatically infers the data type based on the assigned value.
Typecasting
In Python, typecasting allows you to change the data type of a variable. Here are some common typecasting functions:
1. int(): Converts a variable to an integer.
num_str = "42"
num_int = int(num_str)
2. float(): Converts a variable to a floating-point number.
num_int = 42
num_float = float(num_int)
3. str(): Converts a variable to a string.
num = 42
num_str = str(num)
4. bool(): Converts a variable to a boolean.
value = 0
bool_value = bool(value)
Remember to use typecasting carefully, as it may result in loss of information or unexpected behavior if the conversion is not suitable for the data.
type( ) Keyword
In Python, you can use the type() function to determine the type of a variable. Here's an example:
领英推荐
variable = 42
print(type(variable)) # Output: <class 'int'>
variable = "Hello, Python!"
print(type(variable)) # Output: <class 'str'>
variable = 3.14
print(type(variable)) # Output: <class 'float'>
The type() function returns the type of the variable as a class. This can be useful for checking and verifying the type of data your variables hold during runtime.
Case sensitive
Python is case-sensitive. This means that variable names, function names, and other identifiers must be written with consistent capitalization. For example:
variable = 42
Variable = "Hello"
# These are two different variables due to case sensitivity
print(variable)
print(Variable)
In the above example, `variable` and `Variable` are distinct variables. Using different capitalization for the same identifier can lead to errors or unexpected behavior.
Rules for Variable Names
Here are the key rules for naming variables in Python:
1. Valid Characters: Variable names can contain letters (both uppercase and lowercase), numbers, and underscores.
2. Start with a Letter or Underscore: Variable names must start with a letter (a-z, A-Z) or an underscore (_).
3. Case-Sensitive: Python is case-sensitive, so variables with different capitalization are treated as different variables.
4. Avoid Keywords: Variable names should not be the same as Python keywords (e.g., `if`, `else`, `while`, etc.).
5. Descriptive and Readable: Use meaningful and descriptive names for variables to improve code readability.
6. No Spaces: Spaces are not allowed within variable names. Use underscores to separate words for better readability.
7. Avoid Special Characters: Stick to letters, numbers, and underscores; avoid using special characters (e.g., !, @, #, $).
Examples of valid variable names:
my_variable = 42
count_of_items = 10
total_sum_2 = 3.14
Remembering and following these rules will help you write clean and readable Python code.