0% found this document useful (0 votes)
12 views10 pages

Notes

Chapter 3 provides an overview of Python, a versatile programming language created by Guido van Rossum in 1991, used for web development, software development, mathematics, and system scripting. It covers Python's execution modes, keywords, variables, data types, sequences, operators, input/output operations, and control statements like if-else and loops. The chapter emphasizes the importance of functions for code reuse and readability.

Uploaded by

nishayadav98122
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)
12 views10 pages

Notes

Chapter 3 provides an overview of Python, a versatile programming language created by Guido van Rossum in 1991, used for web development, software development, mathematics, and system scripting. It covers Python's execution modes, keywords, variables, data types, sequences, operators, input/output operations, and control statements like if-else and loops. The chapter emphasizes the importance of functions for code reuse and readability.

Uploaded by

nishayadav98122
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/ 10

Chapter 3 – Brief Overview of Python

PYTHON: Python is a popular programming language. It was created by Guido van Rossum, and released in
1991.

It is used for:

 web development (server-side),

 software development,

 mathematics,

 system scripting.

What can Python do?

 Python can be used on a server to create web applications.

 Python can be used alongside software to create workflows.

 Python can connect to database systems. It can also read and modify files.

 Python can be used to handle big data and perform complex mathematics.

 Python can be used for rapid prototyping, or for production-ready software development.

Execution Mode:

We can develop a python program in 2 different styles.

 Interactive Mode and

 Batch Mode.

Interactive Mode :

Interactive mode is a command line shell. If we write a python program in the command line shell.

Typically the interactive mode is used to test the features of the python, or to run a smaller script that
may not be reusable.

Script Mode :

script mode is mainly used to develop business applications. In batch mode, we can write a group of
python statements in any one of the following editors or IDEs

Python Keywords:

Keywords in Python are reserved words that have special meanings and serve specific purposes in the
language syntax. Python keywords cannot be used as the names of variables, functions, and classes or
any other identifier.

The list of keywords are:


['False', 'None', 'True',"__peg_parser__ 'and', 'as', 'assert', 'async', 'await', 'break',
'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if',
'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

Identifiers: Python Keywords are some predefined and reserved words in Python that have special
meanings. Keywords are used to define the syntax of the coding. The keyword cannot be used as an
identifier, function, or variable name. All the keywords in Python are written in lowercase except True
and False.

Rules for Keywords in Python

 Python keywords cannot be used as identifiers.

 All the keywords in Python should be in lowercase except True and False.

 We cannot use special symbols.

 It can be of any length

Variables: In Python, variables are used to store data that can be referenced and manipulated during
program execution. A variable is essentially a name that is assigned to a value. Unlike many other
programming languages, Python variables do not require explicit declaration of type. The type of the
variable is inferred based on the value assigned.

Example:

# Variable 'x' stores the integer value 10

x=5

# Variable 'name' stores the string "Samantha"

name = "Samantha"

print(x)

print(name)

Data Types: In programming, data type is an important concept.

Variables can store data of different types, and different types can do different things.

Python has the following data types built-in by default, in these categories:
Python Numbers

There are three numeric types in Python:

 int

 float

 complex

Variables of numeric types are created when you assign a value to them:

Example

x = 1 # int
y = 2.8 # float
z = 1j # complex

Sequences in Python

Sequences are containers with items stored in a deterministic ordering. Each sequence data type comes
with its unique capabilities.

There are many types of sequences in Python. Let’s learn them with Examples.

Types of Sequences in Python

Python sequences are of six types, namely:

1. Strings

2. Lists

3. Tuples

4. Bytes Sequences

5. Bytes Arrays
6. range() objects

Strings in Python

In python, the string is a sequence of Unicode characters written inside a single or double-quote. Python
does not have any char type as in other languages (C, C++), therefore, a single character inside the
quotes will be of type str only.

Lists in Python

Lists are a single storage unit to store multiple data items together. It’s a mutable data structure, therefore,
once declared, it can still be altered.

A list can hold strings, numbers, lists, tuples, dictionaries, etc.

Tuples in Python

Just like Lists, Tuples can store multiple data items of different data types. The only difference is that
they are immutable and are stored inside the parenthesis ().

1. To declare a tuple, either use tuple() or parenthesis, containing comma-separated values.

Mapping: The map() function is used to apply a given function to every item of an iterable, such as
a list or tuple, and returns a map object (which is an iterator).

Let's start with a simple example of using map() to convert a list of strings into a list of integers.

s = ['1', '2', '3', '4']

res = map(int, s)

print(list(res))

Python Operators

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

Example

print(10 + 5)

Python Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical operations:
Python Comparison Operators

Comparison operators are used to compare two values:

Python Relational Operators

Relational operators are used to compare two values:


Python Assignment Operators

Assignment operators are used to assign values to variables:

Python Logical Operators

Logical operators are used to combine conditional statements:

Python Membership Operators


Membership operators are used to test if a sequence is presented in an object:

Operator Precedence

Operator precedence describes the order in which operations are performed.

Example

Parentheses has the highest precedence, meaning that expressions inside parentheses must be evaluated
first:

print((6 + 3) - (6 + 3))

Multiplication * has higher precedence than addition +, and therefore multiplications are evaluated
before additions:I

Input and Output:

Understanding input and output operations is fundamental to Python programming. With the print()
function, we can display output in various formats, while the input() function enables interaction with
users by gathering input during program execution.

Taking input in Python

Python input() function is used to take user input. By default, it returns the user input in form of a string.

Example:

name = input("Enter your name: ")

print("Hello,", name, "! Welcome!")

Output

Enter your name: GeeksforGeeks


Hello, GeeksforGeeks ! Welcome!

Syntax Errors in Python


Syntax error occurs when the code doesn't follow Python's rules, like using incorrect grammar in
English. Python stops and points out the issue before running the program.

Python Logical Errors (Exception)

Logical errors are subtle bugs in the program that allow the code to run, but produce incorrect or
unintended results. These are often harder to detect since the program doesn’t crash, but the output is
not as expected.

Python Functions is a block of statements that does a specific task. The idea is to put some commonly
or repeatedly done task together and make a function so that instead of writing the same code again and
again for different inputs, we can do the function calls to reuse code contained in it over and over again.

Benefits of Using Functions

 Code Reuse

 Reduced code length

 Increased redability of code

If….Else statement: In Python, If-Else is a fundamental conditional statement used for decision-making in
programming. If...Else statement allows to execution of specific blocks of code depending on the condition is True or
False.

if Statement

if statement is the most simple decision-making statement. If the condition evaluates to True, the block of code
inside the if statement is executed.

Example of If Statement:

i = 10

# Checking if i is greater than 15

if (i > 15):

print("10 is less than 15")

print("I am Not in if")

if....else Statement

if...else statement is a control statement that helps in decision-making based on specific conditions. When the if
condition is False. If the condition in the if statement is not true, the else block will be executed.

i = 20

# Checking if i is greater than 0


if (i > 0):

print("i is positive")

else:

print("i is 0 or Negative")

Python For Loops are used for iterating over a sequence like lists, tuples, strings, and ranges.

 For loop allows you to apply the same operation to every item within loop.

 Using For Loop avoid the need of manually managing the index.

 For loop can iterate over any iterable object, such as dictionary, list or any custom iterators.

Range function:

The Python range() function returns a sequence of numbers, in a given range. The most common use of it is to
iterate sequences on a sequence of numbers using Python loops.

Example

In the given example, we are printing the number from 0 to 4.

for i in range(5):

print(i, end=" ")

print()

In Python programming language there are two types of loops which are for loop and while loop. Using these
loops we can create nested loops in Python. Nested loops mean loops inside a loop. For example, while loop
inside the for loop, for loop inside the for loop, etc.

You might also like