0% found this document useful (0 votes)
111 views

Python Variables Cheatsheet

Variables in Python are used to store data and are assigned values using the = operator. Python has built-in data types like integers, floats, strings, booleans, lists, tuples and dictionaries. The type() function can check the data type of a variable and functions like int(), float(), and str() can convert between data types. Variables can be used in arithmetic, logical, and other operations.

Uploaded by

A_G_Nair
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views

Python Variables Cheatsheet

Variables in Python are used to store data and are assigned values using the = operator. Python has built-in data types like integers, floats, strings, booleans, lists, tuples and dictionaries. The type() function can check the data type of a variable and functions like int(), float(), and str() can convert between data types. Variables can be used in arithmetic, logical, and other operations.

Uploaded by

A_G_Nair
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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