0% found this document useful (0 votes)
2 views17 pages

Python

The document serves as an introductory guide to Python, covering its features, applications, installation, and basic programming concepts. It discusses modules, comments, variables, data types, keywords, and operators, providing essential information for beginners. The content is structured into sections that facilitate understanding of Python's capabilities and usage.

Uploaded by

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

Python

The document serves as an introductory guide to Python, covering its features, applications, installation, and basic programming concepts. It discusses modules, comments, variables, data types, keywords, and operators, providing essential information for beginners. The content is structured into sections that facilitate understanding of Python's capabilities and usage.

Uploaded by

nayanaditya5583
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1

Table of Contents
1. Introduction To Python..................................................................................................................... 4
1.1 What is Python?......................................................................................................................... 4
1.2 Features of Python..................................................................................................................... 4
1.3 Applications of Python’s............................................................................................................4
1.4 Python Installation..................................................................................................................... 5
1.5 First Python Program:................................................................................................................5
2. Modules, Comment & PIP............................................................................................................... 7
2.1 Modules in Python:....................................................................................................................7
Three main types of modules:..........................................................................................................7
2.2 Comments in Python..................................................................................................................8
Type of Comments:..........................................................................................................................8
2.3 What is a pip?............................................................................................................................ 9
3. Variables, Data Types, Keywords & Operators..............................................................................10
3.1 Variables in Python.................................................................................................................. 10
3.1.1 Identifier in python...........................................................................................................11
3.2 Data Type in Python.................................................................................................................12
3.3 Keywords in Python.................................................................................................................13
3.4 Operators in Python:................................................................................................................ 14
Arithmetic Operators:............................................................................................................... 14
Comparison Operators:.............................................................................................................15
Assignment Operators:..............................................................................................................15
Logical Operators:.....................................................................................................................16
Bitwise Operators:.....................................................................................................................16
Membership Operators:.............................................................................................................17

2
3
1. Introduction To Python

1.1 What is Python?


Python is a general- purpose, dynamic, high-level and interpreted programming language.
It is designed to be simple and easy to learn, making it an ideal choice for beginners. One of the key
strengths of Python is its versatility.
Python supports the object-oriented programming approach allowing developers to create
applications with organised and reusable code.

1.2 Features of Python


Readability:- Python’s syntax is designed to be clear and reusable, making it easy for both
beginners and experienced programmers to understand and write code.

Simplicity:- Python emphasises simplicity and avoids complex syntax, making it easy for learn and
use compared to other programming language.

Dynamic typing: - Python is dynamically typed, meaning you don’t need to explicitly declare
variable types.

Large standard library:- Python provides a vast standard library with ready-to-use modules and
functions for various tasks, saving developers time and effort in implementing common
functianalities.

Object-Oriented Programming(OOP):- Python supports the object-oriented programming


paradigm, allowing for the creation and manipulation of objects, classes and inheritance.

Cross-Platform Compatibility:- Python is available on multiple platforms, including windows,


MacOs, and Linux, making it highly portable and rersatile.

Extensive Third-party Libraries:- Python has a vast ecosystem of third- party libraries and
frameworks that expand its capabilities in different domains such as web development, data analysis
and machine learning.

Interpreted Nature:- Python is an interpreted language, meaning it does not require compilation.
This results in faster development cycle as code can be executed directly without the need for a
separate compilation step.

Integration Compatibilities:- Python can easily integrate with other languages like C, C++ and
Java allowing developers to leverage existing code bases and libraries.

1.3 Applications of Python’s


Python is widely used in various domains and offers numerous applications due to its flexibility

4
ease of use here are some key areas were python finds application.

Web Development:- Python is extensively used in web development frameworks such as django
and flask. These Frameworks provides efficient tools and libraries to build dynamic websites and
web applications.
Data Analysis and Visualisation:- Python’s rich ecosystem of libraries, including NumPy, Pandas,
and Matplotlib make it a popular choice for data analysis and visualisation. It enables professionals
to process, manipulate, and visualise data effectively.
Machine Learning and Artificial Intelligence:- Python has become the go to language for
machine learning and AI projects. Libraries like Tesarflow, Keras and sakit- learn provide powerful
tools for implementing complex algorithms and training models.
Automation and Scripting:- Python’s easy-to-read syntax and rapid development cycle make it an
ideal choice for automation and scripting tasks. It is commonly used for tasks such as file
manipulate, data parsing and system administration.

1.4 Python Installation

To download and install python follow these steps:


1. Visit the official python websites at [Link]
2. Download the python installer that matches your system requirements.
3. On the python releases for window page select the link for the latest python 3.X.X release.
4. Scroll Down and choose either the “Windows x86-64 installer”/ Mac Os for 64-bit or the
“Windows x86 executable installer” for 32- bit.
5. Run the downloaded Installer and follow the instructions to install python on your windows
system.
For Linux (Specifically Ubuntu)
1. Open the Ubuntu Software Center Folder on your Linux system.
2. From the all software drop-down list box select Developer Tools.
3. Locate the entry for Python 3.x.x and double- click on it.
4. Click on the install button to initiate the installation process.
5. Once the installation is complete close the ubuntu Software centre folder.

1.5 First Python Program:


Writing your first python program is an exciting step towards learning the language. Here’s a simple
example to get you started.

5
# Printing Hello World using python

print(“Hello”)

Let’s break down the code:


● The print() Function is used to display the specified message or value on the console.
● Int this case, we pass the string “Hello World!” as an argument to the print() function. The
string is enclosed in double quotes.
● The # Symbol indicates a comment in python comments are ignored by the interpreter and
are used to provide explanations or notes to the code.

6
2. Modules, Comment & PIP
2.1 Modules in Python:
Modules provide a way to organise your code logically, instead of having all your code in a single
file, you can split it into multiple modules based on their purpose.
When you want to use the functionality from a module you can import it into your current program
or another module.
This allows you to access and use functions, classes and variables defined
within that module, you can avoid writing the same code repeatedly and instead reuse the code
defined in the module.

Three main types of modules:

Modules

Built-in Modules External Modules User-Defined Modules

1. Built-in Modules: - These are modules that come pre-installed with python. They are part of the
standard library and provide a wide range of functionalities built in modules are readily available
for use without the need for additional installations.
2. External Modules:- These are modules that are created by third-party developers and are not
part of the standard library.
They extended python’s capabilities by providing additional functionalities for specific purposes
external modules can be downloaded and installed using package managers like pip (python
package index)
Popular external modules include numpy for numerical computations.
3. User-Defined Modules:- These modules are created by the python programmers themselves.
They allow users to organised their code into separate files and reuse functionality across multiple
programs.
User-defined modules can contain functions classes, variables, and other code that can be imported
and used in other python scripts or modules.

7
2.2 Comments in Python
Comments in Python are used to provide explanatory notes within the code that are not executed or
interpreted by the computer. They are helpful for improving code readability and for leaving
reminders or explanations for other developers who might work with the code in the future in
python comments are denoted by the hash symbol(#) followed by the comment, it ignores it and
moves on to the text. When the python interpreter encounters a comment, It ignores it and moves on
the next lines of code comments can be placed at the end of line or an a line by themselves It’s
important to that comments are meant for human readers and are not executed by the python
interpreter. Therefore, they have no impact on the program’s functionality or performance.

Type of Comments:

1. Single-line comments
2. Multi-line comment
1. Single-line Comments:- Single-line comments are used to add explanatory notes or comments
on a single line of code. They start with a hash symbol (#) and continue until the end of the line any
thing written after the has symbol is considered a comment and is ignored by the python interpreter.
Here’s on example:

# This is a single-line comment

X=5 # Assigning a value to the variable x

2. Multi-line Comments:-
Multi-line comments also known as block comments allow you to add comments that some
multiple lines. Python does hot have a built-in syntax specifically for multi-line comments, but you
can achieve this by using triple quotes (either single or double) to create a string that is not assigned
to any variable since it is not used else where in the code, it acts as a comment.
Here’s an example:

“”” This is a multi-line comment


It can span across multiple lines
and is enclosed within triple quotes.”””

X=5 “”” Assigning a value to the variable”””

8
2.3 What is a pip?
In simple terms pip is a package manager for python. It stands for “pip installs packages” or “pip
install python”.
When working with python, you may need to use external libraries or modules that provide
additional functionalities beyond what the standard library offers. These libraries are often
developed by the python community and are available for anyone to use.
Pip makes it easy to install a package manage and uninstall these external libraries. It helps you find
and download the libraries you need form the python package index(PyPi) which is a repository of
python packages maintained by the community With pip you can install a package by running a
simple command in your terminal or command prompt. It will automatically fetch the package from
PyPi and install It on your system along with any dependencies it requires.

9
3. Variables, Data Types, Keywords & Operators

3.1 Variables in Python


In python variables are used to store values that can be used later in a program. You can think of
variables as containers that hold data. What makes python unique is that you don’t need to explicitly
declared the type of a variable. You simply assign a value to a variable using the “=” operator.
For Example, You can create a variable called “name” and assign it a value like this:

name=”Naulesh”

# Over here name is a variable of string type


name (is a variable) = (is a assignment operator) “Naulesh” (is a value of name variable)

Here, “name” is the variable name and “Naulesh” is the value assigned to it. Python will
automatically determine the data type of variable based on the value assigned to it.
Variables in python can hold different types of data, such as numbers, strings, Lists or even more
complex objects. You can change the value of a variable at any time by assigning a new value to it.
For Instance:

age=50
age=30 # Updating the value of age variable.

Python also allows you to perform operations on variables For example you can add, subtract,
multiply or divide variables containing numbers. You can even combine variables of different types
using operators. For instance:

x=4
y=3
z=x+y # The value of ‘Z’ will be 7.
greeting =”Hello”
name=”Naulesh”
message=greeting+” “ + name # The value of message will be “ Hello Naulesh”

10
Variables provide a way to store and manipulate data in python making it easier to work with
information through put your program by giving meaningful names to variables, you can make your
code more readable and understandable.

3.1.1 Identifier in python


In python an identifier is a name used to identify a variable function, class, module or any other
user-defined object. An identifier can be made up of letters (both Uppercase and Lowercase) digits
and Underscores( _ ). However, it must start with a letter or an Underscore.
Here are some important rules to keep in mind when working with identifier in python.
1. Valid Characters:- An identifier can contain letters (a-z, A-Z), digit(0-9) and underscores(_). It
cannot contain spaces, special characters like @,# and $.
2. Case Sensitivity:- Python is case-sensitive, meaning upper- case and lowercase letters are
considered different. So, “myVar” and “myvar” are treated as two different identifiers.
3. Reserved Words:- Python has reserved word also known as keywords that have predefined
meanings in the language. These wards can’t be used as identifiers.
Examples of reserved words include “if”, “While” and “def”.
4. Length:- Identifiers can be of any length. However, It is recommended to use meaningful and
descriptive name that are not excessively long.
5. Readability:- It is good practice to choose descriptive names for identifiers that convey their
purpose or meaning. This helps make the code more readable and understandable.
Here are some examples of valid identifiers in Python’s
• my_variable
• count
• total_sum
• pl
• Myclass
And here are some examples of invalid identifiers:
• 123abc (Starts with a digit)
• my-variable(contains a hypen)
• if (a reserved word)
• my var(contains a space)

11
Understanding and following these rules for identifiers in python is important to ensure clarity,
readability and proper functioning of your code.

3.2 Data Type in Python


Data types in Python refer to the different kind of values that can be assigned to variables.
They determine the nature of the data and the operations that can be performed on them. Python
provides several built-in-data types including: numeric, dictionary, Boolean, set, sequence types and
numeric has Integer, complex number, float and sequence type has string ,list and tuple.

Integer

Numeric
Complex Number

Dictionary
Float

Python Data Types Boolean

String

Set

List

Sequence Type

Tuple

1. Numeric:- Python supports different numerical data types including integers(whole numbers),
Floating-point numbers (decimal numbers), and complex numbers( number with real and imaginary
parts)
1. Integer (int):- Integers represent whole numbers without any fractional part.
For example- age=25
2. Floating-point numbers (Float): Floating-point numbers represent numbers with decimal
point or fractions.
For example- pi=3.14
3. Complex Number (Complex): Complex numbers have a real and imaginary part .

12
For Example- z=2+3j where j is suffix with it.
2. Dictionary:- Dictionaries are key-value pairs enclosed by curly brace each value is associated
with a unique key allowing for efficient lookup and retrieval.
For example:- person={‘name’:’John’,’age’: 25, ‘City’: ‘New York’}
3. Boolean:- Boolean (bool) Booleans represents truth values either true or false. They are used for
logical operations and conditions.
For example:- is_valid= True
4. Set:- Sets are unordered collections of unique elements enclosed in curly braces. They are useful
for mathematical operations such as union intersection and difference.
For example:- Fruits=(‘apple’,’banana’,’orange’)
5. Sequence Type:- Sequences represent a collection of elements and include data types like
strings, lists, and tuples, strings are used to store textual data, while lists and tuples are used to store
ordered collections of items.
a) String(str):- Strings represent sequences of character enclosed within single or double
quotes.
For example: name=”john”
b) List(list): Lists are ordered sequences of element enclosed in square brackets. Each element
be of any data type.
For example: numbers=[1,2,3,4]
c) Tuples(tuple): Tuple are similar to lists but are immutable, meaning their elements, cannot
be changed once defined. They are enclosed in ‘()’ paranthesis.

3.3 Keywords in Python


Keywords in python are special words that are having specific meanings and purposes wihin the
python language. They are reserved and cannot be used as variable names or identifiers.
Keywords play a crucial role in defining the structure and behaviour of python programmers.
Keywords are like building block that allow us to create conditional statements, loops, functions,
classes, handle errors and perform other important operations.
✸ List of all the Keywords in python:

false await else import pass none break


except in raise true class finally is
return and continue for lambda try as
def from nonlocal while assert del global
not with async elif or yield

13
3.4 Operators in Python:
Operators in python are symbols or special characters that are used to perform specific
operations on variables and values. Python provides various types of operators to manipulate and
work with different data types.
Here are some important categories of operators in python:
● Arithmetic Operators
● Comparison Operators
● Assignment Operators
● Logical Operators
● Bitwise Operators
● Membership Operators
● Identify Operators

Arithmetic Operators:
Arithmetic Operators inn python are used to perform mathematical calculations on numeric values.
The basic arithmetic operations include:
► Addition(+): Adds two operands together. For example:

If we have q=50 and b=60, then a+b equals 80.

► Subtraction(-): Subtracts the second operand from the first operand. If the first operand is
smaller than the second operand, the result will be negative. For example:

If we have q=80 and b=60, then a-b equals 20.

► Division (/): Divides the first operand by the other operand and returns the quotient.

If we have q=80 and b=10, then a/b equals 8.0.

► Multiplication (*): Multiplies one operand by the other.


For Example:-

If we have q=8 and b=6, then a*b equals 48.

14
► Modulus (%): Returns the remainder after dividing the first operand by the second operand.
For Example:-

If we have q=20 and b=10, then a%b equals 0.

► Exponentiation (**) or Power:- Raises the first operand to the power of second operand.
For Example:-

If we have q=2 and b=3, then a**b equals 8.

► Floor division (//):- Provides the floor value of the quotient obtained by dividing the two
operands, it returns the largest integer that is less than or equal to the result.
For Example:-

If we have q=20 and b=3, then a//b equals 6.

Comparison Operators:
Comparison operators in python are used to compare two values and return a boolean value
(True or False).
► Equal to (==): Check if two operands are equal.
► Not equal to (!=): Check if two operands are not equal.
► Greater than (>): Checks if the left operand is greater than right operand.
► Less than (<): Check if the left operand is less then right operand.
► Greater then or equal to (>=): Checks if the left operand is greater than or equals to the
right operand.
► Less than or equal to (<=): Checks if the left operand is less than or equals to the right
operand.

Assignment Operators:
Assignment operators are used to assign values to variables. They include:-
► Equal to (=): Assign the value on the right to the variable on the left.
► Compound assignment operators (+=,-=, *=, /=): Perform the specified arithmetic
operations and assign the result to variable.

15
Logical Operators:
Logical Operators in python are used to perform logical operations on Boolean values.
► Logical AND (and): When returns true if both operand are true, otherwise all are false.

Condition-1 Condition-2 Result


True True True
False True False
True False False
False False False

► Logical Operator OR (or): When return true if one of them is true, then return true
otherwise false. Or if both operands are false then return false, otherwise return true.

Condition-1 Condition-2 Result


True True True
False True True
True False True
False False False

► Logical NOT (not): Returns the opposite boolean value of the operand.

Condition Result
True False
False True

Bitwise Operators:
Bitwise operators perform operations an individual bits of binary numbers.
► Bitwise AND (&):- Performs a bitwise AND operations on the binary representations of the
operands.
► Bitwise OR (|):- Performs a bitwise OR operation on the binary representation of the
operands.
► Bitwise XOR (^):- Performs a bitwise exclusive OR operation on the binary representation
of the oprands.
► Bitwise Complement (~):- Inverts the bits of the operands.
► Left Shift (<<):- Shifts the bit of the left operand to the left by the number of positions
specified by the right operand.

16
► Right Shift (>>):- Shifts the bit of the left operand to the right by the number of positions
specified by the right operand.

Membership Operators:
Membership operators are used to test whether a value is a member of a sequence.
► In: Returns true if both operands refer to the same object (sequence)
► Not in: Returns true if both operands do not refers to the same object (Sequence).

17

You might also like