Basic Of Python
- Definition: A program is a set of instructions written in a programming language to perform tasks or solve problems.
- Key Components: Programs consist of input, processing logic, and output, using executable statements, functions, and data structures.
- Examples: Programs include tools like web browsers for internet browsing and calculators for mathematical operations.
- An expression is an instruction that a Python interpreter can execute.
- It is a combination of operators (for computations) and operands (values or variables).
- Operators perform arithmetic or logical computations, while operands are the values or variables on which operators act.
Three essential components are required to represent data in Python..
- Literals
- Identifiers/Variables
- Data Types
Literals are nothing but input values passing to the program. We have 4 types of literals. They are
Numerical Literals: Include integers, floats, and complex numbers.
- integer (whole number)
- float (Decimal number)
- complex (real & imaginary part)
# Numerical Literals
num = 10 # integer
num2 = 30.5 # float
num3 = 2+3j
Boolean Literals: True
and False
, representing logical values.
- True
- False
# Boolean Literals
var = True #bool
var2 = False
String Literals: Enclosed in single, double, or triple quotes.
- String
# string Literals
str1 = "Python"
str2 = 'Hello'
Special Literals : None
, indicating the absence of a value or uninitialized variables
- None
# Special Literals
var = None
- Comments are non-executable code used by developers to make the program more understandable.
- The Python interpreter ignores them during execution.
-
Single-Line Comments: Start with
#
and are used for brief explanations.
# Single-line Comments
age = 20
-
Inline Comments: Placed after executable code, also start with
#
.
name = "Suresh" # Name
-
Multi-Line Comments: Enclosed in triple single or double quotes (
'''
or"""
) for larger explanations.
""" Python is
high-level programming
language """
''' Python is
high-level programming
language '''
-
Indentation is used to represent a block of code and indicates its belongingness to specific constructs like
if
,for
, or functions.
- A minimum of one space is required, but 4 spaces or a tab is recommended for consistency.
- Incorrect or inconsistent indentation generates errors, and IDEs assist with auto-indentation.
- Keywords are reserved words in Python with predefined functionality and cannot be used as identifiers.
- Python 3.10 has 35 keywords, though this number may change with future versions.
-
Keywords can be explored using Python's help system, e.g.,
help> keywords
orhelp> continue
for details.
import keyword
# List all Python keywords
print("List of Python Keywords:")
print(keyword.kwlist)