python variables

codepythonic
0

 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:

  • num_str = "10"
    print(num_str) # output 10

    num_int = int(num_str) # Convert the string to an integer
    print(num_int) # output 10

  • tring to an integer

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 = 25
    print(type(age)) # Output: <class 'int'>


OUTPUT:

<class 'int'>


Get more type click it 

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 code
    print(name2) #output pythonic

OUTPUT:

code
pythonic

5. Case-Sensitive:

  • Python is case-sensitive, which means my_variable and My_Variable are different variables.
  • Be consistent in your variable names to avoid confusion and errors.
  • Example:
  • my_variable = 42
    My_Variable = "Hello"
    print(my_variable) #output  42
    print(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 = 10
    print(x) # Output: 10
    X = 20
    print(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, 3
    print(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  >>    



Tags:

Post a Comment

0Comments

Post a Comment (0)