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

Document

The document provides an overview of Python's built-in data types, including numeric, sequence, set, mapping, boolean, and none types, as well as literals and constants. It also explains variable scope in Python, detailing local, global, enclosed, and built-in scopes. Additionally, it covers text file operations and control statements, including conditional, looping, and jumping statements.

Uploaded by

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

Document

The document provides an overview of Python's built-in data types, including numeric, sequence, set, mapping, boolean, and none types, as well as literals and constants. It also explains variable scope in Python, detailing local, global, enclosed, and built-in scopes. Additionally, it covers text file operations and control statements, including conditional, looping, and jumping statements.

Uploaded by

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

Python Data Types

Python has several built-in data types used to store and manipulate different kinds of data. These data types are
categorized into the following main types:
1. Numeric Types
int: Integer numbers (e.g., 5, -3, 100)
float: Floating-point numbers (e.g., 3.14, -0.001)
complex: Complex numbers (e.g., 3+5j)
2. Sequence Types
str: String of characters (e.g., "Hello", 'Python')
list: Ordered, mutable collection (e.g., [1, 2, 3])
tuple: Ordered, immutable collection (e.g., (1, 2, 3))
3. Set Types
set: Unordered, mutable collection of unique items (e.g., {1, 2, 3})
frozenset: Immutable version of a set
4. Mapping Type
dict: A collection of key-value pairs (e.g., {"name": "Alice", "age": 25})
5. Boolean Type
bool: Represents True or False values
6. None Type
None: Represents the absence of a value or a null value

Literals and Constants in Python


Literals:
Literals are fixed values assigned to variables or used directly in code. They represent constant values in Python.
Types of Literals:
Numeric Literals:
Integers: 10, -5
Float: 3.14, -0.01
Complex: 2 + 3j
String Literals:
Single or double quotes: ‘Hello’, “World”
Boolean Literals:
True, False
Special Literal:
None (represents absence of value)
Literal Collections:
List: [1, 2, 3]
Tuple: (4, 5, 6)
Dictionary: {‘a’: 1, ‘b’: 2}
Set: {7, 8, 9}
Constants:
Constants are variables whose values are not meant to be changed after they are assigned. Python does not have
built-in constant types, but by convention:
Constants are written in uppercase:
Example: PI = 3.14159, GRAVITY = 9.8
Constants are usually declared at the top of the program or in a separate module.

Variable Scope in Python


Scope refers to the region of a program where a variable is recognized or accessible. In Python, variable scope is
mainly divided into the following categories:
1. Local Scope
A variable declared inside a function is local to that function.
It is accessible only within that function.
Example:
def greet():
name = "Alice" # Local variable
print("Hello", name)

2. Global Scope
A variable declared outside all functions is a global variable.
It is accessible throughout the program, including inside functions (if not shadowed).
Example:
x = 10 # Global variable
def show():
print(x)

3. Enclosed (Nonlocal) Scope


Variables defined in an outer function, but not global.
Useful in nested functions.
Example:
def outer():
x = "Python"
def inner():
print(x) # Enclosed variable
inner()
4. Built-in Scope
Includes names of all built-in functions and exceptions (e.g., len(), print()).
These are always available unless overridden.

Text Files in Python


Text files are files that contain data in readable text format. Python provides built-in functions to create, read, write,
and modify text files using the open() function.

1. Opening a Text File


Python uses the open() function with two main parameters:
File = open(“filename.txt”, “mode”)
Modes:
“r” – Read (default mode)
“w” – Write (overwrites existing content)
“a” – Append (adds to the end)
“r+” – Read and write

2. Reading from a File

Read(): Reads entire file


Readline(): Reads one line at a time
Readlines(): Reads all lines into a list
Example:
File = open(“data.txt”, “r”)
Content = file.read()
Print(content)
File.close()

3. Writing to a File
Write(): Writes a string to a file
Writelines(): Writes a list of strings
Example:
File = open(“data.txt”, “w”)
File.write(“Hello, World!”)
File.close()

4. Closing a File
Always close the file using file.close() to free system resources.

5. Using with Statement


Automatically handles file closing:
With open(“data.txt”, “r”) as file:
Print(file.read())

Control Statements in Python


Control statements are used to control the flow of execution in a Python
program. They help make decisions, repeat actions, or jump to different parts
of code based on conditions.

Conditional Statements
These allow the program to make decisions using if, elif, and else.
Example:

X = 10
If x > 0:
Print(“Positive”)
Elif x == 0:
Print(“Zero”)
Else:
Print(“Negative”)

Looping Statements
Used for repeating a block of code multiple times.
For loop: Iterates over a sequence (list, tuple, string, etc.)
For I in range(5):
Print(i)
While loop: Repeats as long as a condition is true
I=0
While I < 5:
Print(i)
I += 1

Jumping Statements
Used to alter the normal flow inside loops.
Break: Exits the loop completely
For I in range(10):
If I == 5:
Break
Print(i)
Continue: Skips the current iteration and moves to the next

For I in range(5):
If I == 2:
Continue
Print(i)

Pass: Does nothing; used as a placeholder

For I in range(5):
If I == 3:
Pass
Print(i)

You might also like