0% found this document useful (0 votes)
3 views3 pages

Python Variables and DataTypes

The document explains variables and data types in Python, highlighting that a variable is a container for data values and can be created by assigning a value. It outlines variable naming rules, various data types including numeric, text, sequence, set, mapping, boolean, and binary types, as well as type checking and conversion. Additionally, it discusses dynamic typing and the NoneType for representing null values.

Uploaded by

edsonedwin03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Python Variables and DataTypes

The document explains variables and data types in Python, highlighting that a variable is a container for data values and can be created by assigning a value. It outlines variable naming rules, various data types including numeric, text, sequence, set, mapping, boolean, and binary types, as well as type checking and conversion. Additionally, it discusses dynamic typing and the NoneType for representing null values.

Uploaded by

edsonedwin03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Variables and Data Types in Python

Python Variables and Data Types

1. What is a Variable?

A variable is a container for storing data values. In Python, variables are created when you assign a value.

Example:

x = 10

name = "Edson"

price = 99.99

Use:

Variables store data that can be reused and manipulated throughout the program.

2. Variable Naming Rules:

- Must begin with a letter (a-z, A-Z) or underscore (_)

- Can include numbers but not as the first character

- Cannot be a reserved Python keyword

Example:

_valid = 1

user1 = "Alice"

Invalid:

1user = "Bob" # Starts with a digit

class = "Python" # 'class' is a keyword

3. Data Types:

A. Numeric Types:

- int: Integer numbers

Example: age = 25
Variables and Data Types in Python

- float: Decimal numbers

Example: height = 5.8

- complex: Complex numbers (a + bj)

Example: z = 3 + 4j

B. Text Type:

- str: Sequence of characters

Example: msg = "Hello, Python!"

C. Sequence Types:

- list: Ordered, mutable

Example: fruits = ["apple", "banana", "cherry"]

- tuple: Ordered, immutable

Example: dimensions = (1920, 1080)

- range: Sequence of numbers

Example: numbers = range(5)

D. Set Types:

- set: Unordered, no duplicates

Example: unique_numbers = {1, 2, 3, 4}

- frozenset: Immutable set

Example: frozen = frozenset([1, 2, 3])

E. Mapping Type:

- dict: Key-value pair

Example: person = {"name": "Alice", "age": 30}


Variables and Data Types in Python

F. Boolean Type:

- bool: True or False

Example: is_active = True

G. Binary Types:

- bytes: Immutable binary sequence

- bytearray: Mutable binary sequence

- memoryview: Access internal data of objects

4. Check Data Type using type():

Example:

x = 10

print(type(x)) # Output: <class 'int'>

5. Type Conversion (Casting):

int("5") => 5

float("3.14") => 3.14

str(10) => "10"

6. Dynamic Typing:

Python allows changing variable types:

x=5 # int

x = "text" # now a string

7. NoneType:

Represents null/empty value.

Example:

x = None

print(type(x)) # Output: <class 'NoneType'>

You might also like