1. Creating Variables:
- In Python, you can create variables by assigning a value to a name using the
=
sign. - Variable names can contain letters (a-z, A-Z), numbers (0-9), and underscores (_).
- Variable names cannot start with a number.
- Example:
name = "John"
age = 25
print (name)
print (age)
aO
OUT PUT :
John
25
2. Casting (Data Type Conversion):
- Python is a dynamically-typed language, meaning you don't need to declare the data type of a variable explicitly.
- However, you can convert a variable from one data type to another using casting functions like
int()
,float()
,str()
, etc. - Example:
- tring to an integer
num_str = "10"
print(num_str) # output 10
num_int = int(num_str) # Convert the string to an integer
print(num_int) # output 10
OUTPUT :
10
10
3. Getting the Type of a Variable:
- You can use the
type()
function to check the data type of a variable. - Example:
- age = 25print(type(age)) # Output: <class 'int'>
OUTPUT:
<class 'int'>
4. Single or Double Quotes?
- In Python, you can use either single (
'
) or double ("
) quotes to define strings. - Both are equivalent, and you can choose whichever you prefer, as long as you maintain consistency within your code.
- Example:.
- name1 = 'code'name2 = "pythonic"print(name1) #output codeprint(name2) #output pythonic
OUTPUT:
code
pythonic
5. Case-Sensitive:
- Python is case-sensitive, which means
my_variable
andMy_Variable
are different variables. - Be consistent in your variable names to avoid confusion and errors.
- Example:
- my_variable = 42My_Variable = "Hello"print(my_variable) #output 42print(My_Variable) #output Hello
OUTPUT :
42
Hello
6. Variable Reassignment:
- You can change the value of a variable after it's been assigned.
- Example:
- x = 10print(x) # Output: 10X = 20print(X) # Output: 20
OUTPUT :
10
20
7. Multiple Assignment:
- Python allows you to assign multiple variables in a single line.
- Example:
- a, b, c = 1, 2, 3print(a,b,c) #output ; 1 2 3
OUTPUT:
1 2 3
Remember to choose meaningful and descriptive variable names that convey the purpose of the data they store. Properly managing data types and variable names will make your code more readable and maintainable.
<< Previous Next >>