Chapter 2

Python Variables

In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value.

- Unlike Java ,C and many other languages, Python variables do not require explicit declaration of type.

The type of the variable is inferred based on the value assigned.

https://www.geeksforgeeks.org/python/python-variables/

Rules for Naming Variables

To use variables effectively, we must follow Python’s naming rules:

-Variable names can only contain letters, digits and underscores (_).

-A variable name cannot start with a digit.

-Variable names are case-sensitive (myVar and myvar are different).

-Avoid using Python keywords (e.g., if, else, for) as variable names.

https://www.geeksforgeeks.org/python/python-variables/

Assigning Values to Variables

Variables in Python are assigned values using the = operator.

Dynamic Typing

Python variables are dynamically typed, meaning the same variable can hold different types of values during execution.

Basic Casting Functions

int() - Converts compatible values to an integer.

float() - Transforms values into floating-point numbers.

str() - Converts any data type into a string.

In Python, we can determine the type of a variable using the type() function. This built-in function returns the type of the object passed to it.


https://www.w3schools.com/python/python_strings.asp https://www.geeksforgeeks.org/python/introduction-to-python/

หัวข้อ