Web Development
HTML Course
CSS Course
JavaScript Course
PHP Course
Python Course
SQL Course
SEO Course

Variables and Data Types

In Python, variables are used to store information. You do not need to specify the type - Python detects it automatically. You can store numbers, text, boolean values, lists, and more.

Declaring Variables

# Example of variables
name = "John"         # String
age = 25           # Integer (int)
active = True          # Boolean value (True/False)
price = 19.99          # Decimal number (float)
Basic rules:
  • Variable names cannot start with a number.
  • Do not use spaces - you can use an underscore: nume_complet.
  • Python is case-sensitive: name โ‰  Nume.

Data Types

Python has several data types. The most common are:

Type Example Description
int age = 30 Integer number
float price = 19.99 Decimal number
str name = "Anna" Text (string)
bool active = True True or False

Functions type() and isinstance()

The type() function returns the exact type of a variable. The isinstance() function checks whether a variable belongs to a specific type or group of types.

# type()
x = 42
print(type(x))  # Displays: <class 'int'>

y = "Hello"
print(type(y))  # Displays: <class 'str'>

# isinstance()
z = 3.14
print(isinstance(z, float))  # Displays: True

a = True
print(isinstance(a, bool))  # Displays: True

b = "123"
print(isinstance(b, int))  # Displays: False
Note:
  • Use type() to see the exact type.
  • Use isinstance() for logical checks in your code.
  • You can check multiple types at once: isinstance(x, (int, float))

Type Conversions

Sometimes you need to convert one data type to another. Python provides simple functions for conversions.

# Explicit conversions
x = 5
y = str(x)      # Becomes "5" - string
z = float(x)    # Becomes 5.0 - float
w = bool(x)     # Becomes True - any number โ‰  0 is considered True

# Reverse conversions
a = "10"
b = int(a)      # Becomes 10 - int

c = "3.14"
d = float(c)    # Becomes 3.14 - float

e = ""
f = bool(e)     # Becomes False - empty string is considered False
Important:
  • Conversion int("abc") will give an error - the string must contain only digits.
  • Conversion bool() returns False for: 0, "", [], None.

Functions input() and print()

The input() function receives data from the user as a string. The print() function displays information in the console.

# Simple example
name = input("What is your name? ")     # Receives a string from the user
print("Hello,", name)                    # Displays the message
print(type(name))                        # Displays: <class 'str'>

# Conversion after input
age = int(input("How old are you? "))   # Converts the string to int
print("You are", age, "years old.")    # Displays the age

Simple Exercises

  1. Declare a variable an_nastere and display its data type.
  2. Convert an integer to float and display the result.
  3. Ask the user for a name and age, then display a personalized message.
# Exercise example
name = input("Name: ")
age = int(input("Age: "))
print("Hello,", name + "! You are", age, "years old.")

๐Ÿง  Quiz - Variables & Data Types in Python

Top