This chapter provides a brief overview of Python, covering programming basics, including keywords, identifiers, variables, data types, operators, expressions, input/output, functions, and control statements such as loops and conditionals.
Python is a high-level, open-source programming language that was created by Guido van Rossum in 1991. It is popular due to its simplicity and versatility, making it applicable for various domains such as software development, data science, artificial intelligence, and scientific computing. Python code is typically written in plain text files with a .py extension and can be run through an interpreter, which can be installed locally or accessed online.
To start coding in Python, you can either use an interactive mode or script mode:
python filename.py). This is the preferred method for more complex programs, as scripts can be easily modified and reused.Keywords in Python are reserved words that have specific meanings to the interpreter. They must be written in lowercase and cannot be used as identifiers for variables, functions, etc. Examples include def, if, else, return, and more.
Identifiers are names given to variables, functions, and other objects. Rules for naming identifiers include:
Example: marks_math, marks_english, avg.
Variables are identifiers whose values can vary. They must be assigned a value before use. An example of variable assignment is:
age = 18
Variables can hold different data types: strings, integers, floats, etc.
Python supports several built-in data types to categorize the type of data variables can hold:
int), floating-point numbers (float), and complex numbers (complex).dict) holds key-value pairs.True or False values.num = 5 #int
price = 19.99 #float
student_names = ['John', 'Alice', 'Bob'] #list
data = {'color': 'blue', 'age': 25} #dictionary
Operators in Python are symbols used to perform operations on variables and values. They include:
+, -, *, /, %, // (floor division), and ** (exponentiation).==, !=, >, <, >=, <=.and, or, not.=, +=, -=, *=, etc.An expression is a combination of constants and variables that is evaluated to produce a value. Examples include:
x + y
3 * (8 + 2)
Furthermore, operator precedence affects how expressions are evaluated; for example, multiplication is performed before addition unless parentheses are used.
input() is used.print() displays values on the screen. Example usage:user_name = input('Enter your name: ')
print('Hello,', user_name)
Debugging is the process of identifying and removing errors from a program’s code. Errors can be:
A function is a reusable piece of code that executes a specific task. Example:
def greet(name):
print(
input(), while Output is displayed using print().