Python Variables

Python Variables

What Are Variables?

In the world of programming, variables are essential tools. They act as containers that store and manage data, making your Python code more efficient and organized.

How to Declare Variables in Python

Declaring a variable in Python is a breeze! Simply choose a descriptive name (following naming conventions) and use the assignment operator (=) to assign it a value. Here’s an example:

For example:

prog = "Hello World"

Variable Types in Python

Unlike some programming languages, Python doesn’t require you to pre-declare a variable’s type. The interpreter cleverly figures it out based on the assigned value. Here’s a breakdown of some common data types:

  • Integers (int): Whole numbers (e.g., 10, -5).
  • Floating-point numbers (float): Numbers with decimals (e.g., 3.14, -9.8).
  • Strings (str): Text data enclosed in quotes (e.g., “This is a string”).
  • Booleans (bool): True or False values (e.g., True, False).

Tip: Use the type() function to check a variable’s data type:

print(type(prog))

Variables are fundamental components of Python programming. They allow programmers to store and manipulate data, making the code more readable and maintainable.

Example

var = "Obrigado por visitar a minha página!"
print(var)

Output

Obrigado por visitar a minha página!

Deixe um comentário

O seu endereço de email não será publicado. Campos obrigatórios marcados com *

pt_PT