Python Variables Cheatsheet

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Python Variables Cheatsheet

1. Assigning Variables
Variables in Python are used to store data. To create a variable, use the assignment
operator = to assign a value to a variable name.

# Assigning a variable
x = 10
name = "Alice"

2. Data Types
Python has several built-in data types, including:

int : Integer numbers

float : Floating-point numbers

str : Strings

bool : Booleans

list : Lists

tuple : Tuples

dict : Dictionaries

# Examples of data types


integer = 42
floating_point = 3.14
string = "Hello, World!"
boolean = True
list_example = [1, 2, 3]
tuple_example = (1, 2, 3)
dict_example = {"key": "value"}

3. Type Checking
You can use the type() function to determine the data type of a variable.

Python Variables Cheatsheet 1


# Type checking
print(type(42)) # Output: <class 'int'>

4. Type Conversion
Convert between data types using built-in functions like int() , float() , str() , and
bool() .

# Type conversion
x = 42
float_x = float(x)
print(float_x) # Output: 42.0

5. Operations with Variables


Perform arithmetic, logical, and other operations with variables.

# Arithmetic operations
x = 10
y = 5
add = x + y
sub = x - y
mul = x * y
div = x / y

# String operations
first_name = "Alice"
last_name = "Smith"
full_name = first_name + " " + last_name

Python Variables Cheatsheet 2

You might also like