CHAPTER 2 Ankit Diswar
[Link]
Python Programming Fundamentals
Keywords
Definition: Keywords in Python are reserved words that have predefined meanings and
functionalities. They cannot be used as identifiers.
There are a total of 35 keywords in Python:
• 'False', 'None', 'True'
• 'and', 'or', 'not', 'is'
• 'if', 'elif', 'else'
• 'while', 'for', 'break', 'continue', 'return', 'in', 'yield'
• 'try', 'except', 'finally', 'raise', 'assert'
• 'import', 'from', 'as', 'class', 'def', 'pass', 'global', 'nonlocal', 'lambda', 'del', 'with'
• 'async', 'await'
Note: All keywords in Python start with a lowercase letter, except True, False, and None,
which begin with an uppercase letter:
Example Program to Display Keywords:
CHAPTER 2 Ankit Diswar
[Link]
Identifiers
Definition: Identifiers are names used to represent entities like variables, functions,
classes, objects, or methods in a program.
Rules for Identifiers:
1. Allowed characters are:
o Alphabets (a-z, A-Z)
o Digits (0-9)
o Underscore (_)
2. An identifier should not start with a digit.
3. Identifiers are case-sensitive.
4. Keywords cannot be used as identifiers.
5. There is no limit to the length of an identifier, but lengthy identifiers are not
recommended.
Special Identifiers:
• _identifier: Indicates private identifiers.
• __identifier: Indicates strongly private identifiers.
• __add__: A special identifier defined by Python, also known as a magic method
(dunder method).
Datatypes
Definition: Data types specify the type of data a variable can hold. Python dynamically
assigns types based on the value assigned to a variable.
Built-in Data Types:
1. Primitive Types: int, float, complex, bool, str
2. Sequence Types: list, tuple, range
3. Set Types: set, frozenset
4. Mapping Type: dict
CHAPTER 2 Ankit Diswar
[Link]
5. Binary Types: bytes, bytearray
6. Special Type: None
Methods to Get Data Properties:
1. type(data): Returns the type of the data.
2. id(data): Returns the memory address of the data.
3. print(data): Prints the data.
Integer Datatype
Definition: Integers represent whole numbers. Python supports the following number
systems:
1. Decimal (Base-10): Default system with digits 0-9.
Example: a = 1234
2. Binary (Base-2): Prefixed with 0b or 0B and uses digits 0 and 1.
Example: b = 0b1101
3. Octal (Base-8): Prefixed with 0o or 0O and uses digits 0-7.
Example: c = 0o123
4. Hexadecimal (Base-16): Prefixed with 0x or 0X, using digits 0-9 and letters a-f/A-
F.
Example: d = 0x1A3
Base Conversion Functions:
1. bin(value): Converts an integer to binary.
2. oct(value): Converts an integer to octal.
3. hex(value): Converts an integer to hexadecimal.
Floating-Point Datatype
Definition: Represents decimal numbers or numbers in scientific notation.
Example: a = 123.45, b = 1.23e3
CHAPTER 2 Ankit Diswar
[Link]
Complex Datatype
Definition: Represents numbers in the form real + imaginary.
Example: z = 3 + 4j
Boolean Datatype
Definition: Represents two values: True (1) and False (0).
Example: is_valid = True
String Datatype
Definition: Represents a sequence of characters enclosed in single ('), double (") or triple
quotes (''').
Example: name = "Python"
String Slicing: Strings can be sliced using indices.
Example: substring = name[0:3]
Bytes and Bytearray
1. Bytes: Immutable sequence of byte values ranging from 0 to 256.
Example: b = bytes([65, 66, 67])
2. Bytearray: Similar to bytes but mutable.
Example: ba = bytearray([65, 66, 67])
Collection Datatypes
1. List: Ordered and mutable collection.
Example: fruits = ['apple', 'banana', 'cherry']
2. Tuple: Ordered and immutable collection.
Example: coordinates = (10, 20)
3. Range: Immutable sequence of numbers.
Example: r = range(5)
4. Set: Unordered collection with no duplicates.
Example: unique_items = {1, 2, 3}
CHAPTER 2 Ankit Diswar
[Link]
5. Frozenset: Immutable version of a set.
Example: fs = frozenset([1, 2, 3])
6. Dictionary: Key-value pair collection.
Example: student = {'name': 'John', 'age': 25}
Type Conversion
Definition: Converts data from one type to another.
1. int(value): Converts to integer.
2. float(value): Converts to float.
3. complex(value): Converts to complex.
4. bool(value): Converts to boolean.
5. str(value): Converts to string.
Input and Output Operations
IO Operations is represented as “Input and Output Operations”.
To perform IO operations in python, there are two inbuilt/pre-defined methods:
1) input()
2) print()
Input Operations:
input():
• input() is an inbuilt method, which is used to read a value which is entered from
keyboard during the time of running of the program.
• input() method can read a string value by default.
• To read other type of the data/value, we required to perform the type conversion
to input() method with the required type.
Ex: To read a decimal/integer value:
CHAPTER 2 Ankit Diswar
[Link]
Output Operations:
print():
• print() is an inbuilt method used to write/print anything/any data on the screen.
• print() method is reserved with new line after printing of any data in all the time.
CHAPTER 2 Ankit Diswar
[Link]
1. F-String Formatting:
• Introduced in Python 3.6, f-strings allow embedding variables directly into strings
using curly braces {}.
• It's clean and concise.
Example:
a = 100
b = 12.234
c = "12-23j"
d = False
e = "Python"
print(f"a = {a}, b = {b}, c = {c}, d = {d}, e = {e}")
Output:
a = 100, b = 12.234, c = 12-23j, d = False, e = Python
2. Using sep Parameter:
• sep (separator) specifies the string inserted between each value while printing.
• By default, sep is a space (' ').
Example:
print(a, b, c, d, e, sep=" | ")
Output:
100 | 12.234 | 12-23j | False | Python
3. Using end Parameter:
• end specifies the string appended at the end of the output. By default, it’s a
newline ('\n').
• It’s useful to control how lines are printed.
Example:
print("Start of line: ", end="")
print(a, b, c, d, e)
Output:
Start of line: 100 12.234 12-23j False Python
[Link]