0% found this document useful (0 votes)
7 views13 pages

Python Bca Unit 1

Basics: Python Interpreter, writing code in Jupyter Notebook, Indentation, comments, importing a module, binary operators, standard scalar data types, type casting, if-else statements, loops(while, for), pass, range, ternary expressions.

Uploaded by

mybzns.124
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)
7 views13 pages

Python Bca Unit 1

Basics: Python Interpreter, writing code in Jupyter Notebook, Indentation, comments, importing a module, binary operators, standard scalar data types, type casting, if-else statements, loops(while, for), pass, range, ternary expressions.

Uploaded by

mybzns.124
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

BCA 2 PYTHON WEBSOL UNIT - 1

Unit I

Basics: Python Interpreter, writing code in Jupyter Notebook, Indentation, comments,


importing a module, binary operators, standard scalar data types, type casting, if-else
statements, loops(while, for), pass, range, ternary expressions.

Python
Python is a general purpose, dynamic, high-level, and interpreted programming language. It
supports Object Oriented programming approach to develop applications. It is simple and easy to
learn and provides lots of high-level data structures.

Python is easy to learn yet powerful and versatile scripting language, which makes it attractive
for Application Development.

Python supports multiple programming pattern, including object-oriented, imperative, and


functional or procedural programming styles.

Python is not intended to work in a particular area, such as web programming. That is why it is
known as multipurpose programming language because it can be used with web, enterprise, 3D
CAD, etc.

We don't need to use data types to declare variable because it is dynamically typed so we can
write a=10 to assign an integer value in an integer variable

Python makes the development and debugging fast because there is no compilation step included
in Python development, and edit-test-debug cycle is very fast.

History
Python was invented by Guido van Rossum in 1991 at CWI in Netherland. The idea of Python
programming language has taken from the ABC programming language or we can say that ABC
is a predecessor of Python language.

Dr. Amit Vyas sir WEBSOL 9214525215 BCA SEM - 4 Page 1


BCA 2 PYTHON WEBSOL UNIT - 1

There is also a fact behind the choosing name Python. Guido van Rossum was a fan of the
popular BBC comedy show of that time, "Monty Python's Flying Circus". So he decided to
pick the name Python for his newly created programming language.

Why learn Python


Python provides many useful features to the programmer. These features make it most popular
and widely used language. We have listed below few-essential feature of Python.

o Easy to use and Learn


o Expressive Language
o Interpreted Language
o Object-Oriented Language
o Open Source Language
o Extensible
o Learn Standard Library
o GUI Programming Support
o Integrated
o Embeddable
o Dynamic Memory Allocation
o Wide Range of Libraries and Frameworks

Where is Python used


Python is a general-purpose, popular programming language and it is used in almost every
technical field. The various areas of Python use are given below.

o Data Science
o Date Mining
o Desktop Applications
o Console-based Applications

Dr. Amit Vyas sir WEBSOL 9214525215 BCA SEM - 4 Page 2


BCA 2 PYTHON WEBSOL UNIT - 1

o Mobile Applications
o Software Development
o Artificial Intelligence
o Web Applications
o Enterprise Applications
o 3D CAD Applications
o Machine Learning
o Computer Vision or Image Processing Applications.
o Speech Recognitions

Python Popular Frameworks and Libraries


Python has wide range of libraries and frameworks widely used in various fields such as machine
learning, artificial intelligence, web applications, etc. We define some popular frameworks and
libraries of Python as follows.

o Web development (Server-side) - Django Flask, Pyramid, CherryPy


o GUIs based applications - Tk, PyGTK, PyQt, PyJs, etc.
o Machine Learning - TensorFlow, PyTorch, Scikit-learn, Matplotlib, Scipy, etc.
o Mathematics - Numpy, Pandas, etc.

Python Interpreter
Python is an interpreted language developed by Guido van Rossum in the year of 1991. As we
all know Python is one of the most high-level languages used today because of its massive
versatility and portable library & framework features. It is an interpreted language because it
executes line-by-line instructions. There are actually two way to execute python code one is in
Interactive mode and another thing is having Python prompts which is also called script mode.
Python does not convert high level code into low level code as many other programming
languages do rather it will scan the entire code into something called bytecode. every time
when Python developer runs the code and start to execute the compilation part execute first and
then it generate an byte code which get converted by PVM Python Virtual machine that
understand the analogy and give the desired output.

Dr. Amit Vyas sir WEBSOL 9214525215 BCA SEM - 4 Page 3


BCA 2 PYTHON WEBSOL UNIT - 1

Interpreted Languages: Perl, BASIC, Python, JavaScript, Ruby, PHP.


Compiled Languages: C, C++, C#, COBOL and CLEO.

Writing code in Jupyter Notebook

The Jupyter Notebook is an open-source web application that allows you to create and share
documents that contain live code, equations, visualizations, and narrative text. Jupyter has
support for over 40 different programming languages and Python is one of them.

Dr. Amit Vyas sir WEBSOL 9214525215 BCA SEM - 4 Page 4


BCA 2 PYTHON WEBSOL UNIT - 1

Comments
Comments in Python are the lines in the code that are ignored by the interpreter during the
execution of the program.
 Comments enhance the readability of the code.
 Comment can be used to identify functionality or structure the code-base.
 Comment can help understanding unusual or tricky scenarios handled by the code to
prevent accidental removal or changes.
 Comments can be used to prevent executing any specific part of your code, while making
changes or testing.

single line comments starts with hashtag Python does not provide the option
symbol #. for multiline comments

Import module in Python


In Python, modules help organize code into reusable files. They allow you to import and use
functions, classes and variables from other scripts. The import statement is the most common
way to bring external functionality into your Python program.
Why do we need to import modules?
 Reuse Ready-Made Tools: Python modules let you use pre-written code
like [Link]() or [Link]() instead of writing everything yourself.
 Keep Code Organized: Modules break large programs into smaller, manageable pieces
across multiple files.
 Boost Productivity: You get access to Python’s standard library or third-party packages
without reinventing the wheel.
 Write Less, Do More: Importing a module gives you lots of functionality in a single line.
 Avoid Clutter: Code stays cleaner, shorter and more readable when organized into reusable
modules.

Importing a Module
The most common way to use a module is by importing it with the import statement
import math import random
pie = [Link] res = [Link](1, 10)
print("Value of pi:", pie) print("Random Number:", res)

binary operators
In Python, "binary operators" typically refer to operators that require two operands, one on the
left and one on the right. This broad category includes several types of operators:

1. Arithmetic Operators: These perform mathematical calculations.


Operator Description Example

Dr. Amit Vyas sir WEBSOL 9214525215 BCA SEM - 4 Page 5


BCA 2 PYTHON WEBSOL UNIT - 1

+ Addition 5 + 3 (results in 8)

- Subtraction 5 - 3 (results in 2)

* Multiplication 5 * 3 (results in 15)

/ Division 5 / 3 (results in 1.666...)

// Floor Division 5 // 3 (results in 1)

% Modulo (remainder) 5 % 3 (results in 2)

** Exponentiation 5 ** 3 (results in 125)

2. Comparison Operators: These compare two values and return a Boolean (True or False).
Operator Description Example

== Equal to 5 == 3 (results in False)

!= Not equal to 5 != 3 (results in True)

> Greater than 5 > 3 (results in True)

< Less than 5 < 3 (results in False)

>= Greater than or equal to 5 >= 3 (results in True)

<= Less than or equal to 5 <= 3 (results in False)

Data types in Python


In programming, we often work with a variety of values: sentences, numbers, collections of
items, and more. For example, a student's marks, represented by a number, would be considered
an integer data type in Python, while a student's name, which is a sequence of characters, would
be a string data type.

In Python, every value is associated with a data type because Python is an object-oriented
language where everything is treated as an object.

Dr. Amit Vyas sir WEBSOL 9214525215 BCA SEM - 4 Page 6


BCA 2 PYTHON WEBSOL UNIT - 1

1. Numeric Data types in Python

Numeric data types in Python represent numbers and are categorized into three types:

 Integers in Python

Integers, as you know in mathematics are numbers without any fractional part. They can be 0,
positive or negative. There is no limit to how long an integer can be in Python. They are
represented by int class.

 Floating Point numbers in Python

Floating point numbers (float) are real numbers with floating point representation, they are
specified by a decimal point. They are represented by the float class.

 Complex numbers in Python

Complex numbers in python are specified as (real part) + (imaginary part)j. They are represented
by the complex class.

2. Sequence Data types in Python

Sequence data types in Python are an ordered collection of similar or different values. They are
also called container data types as they usually contain more than one value. We can access
elements of a sequence data type by indexing. String, list, and tuple are the different types of
containers used to store the data in a sequential manner.

 Strings in Python

Dr. Amit Vyas sir WEBSOL 9214525215 BCA SEM - 4 Page 7


BCA 2 PYTHON WEBSOL UNIT - 1

A string can be defined as a sequence of characters enclosed in single, double, or triple quotation
marks. While in most cases single and double quotation marks are interchangeable. Triple
quotation marks are used for multi-line strings. It is an immutable data type, i.e. its values cannot
be updated. String is represented by string class.

 Lists in Python

Lists are an ordered sequence of one or more types of values. They are just like arrays in other
languages. They are mutable, i.e. their items can be modified.

 Tuples in Python

Like lists, Tuples are also an ordered sequence of one or more types of values except that they
are immutable, i.e their values can't be modified. They are represented by the tuple class.

3. Dictionaries in Python

In Python, Dictionaries are an unordered collection of key-value pairs (key: value). This helps in
retrieving the data and makes the retrieval highly optimized, especially in cases of high volume
data. Keys can't be repeated in a dictionary while values can be repeated. Dictionaries are
mutable, i.e. their values can be modified.

[Link] in Python

Boolean is a data type that has one of two possible values, True or False. They are mostly used in
creating the control flow of a program using conditional statements.

5. Sets in Python

Sets in Python are an unordered collection of elements. They contain only unique elements. They
are mutable, i.e. their values can be modified.

The primary standard scalar data types in Python are:


Integers (int):
Floating-point numbers (float):
Complex numbers (complex)
Booleans (bool)
NoneType (None):

Type Casting
Type Casting is the method to convert the Python variable datatype into a certain data type in
order to perform the required operation by users. In this article, we will see the various
techniques for typecasting. There can be two types of Type Casting in Python:
 Python Implicit Type Conversion

Dr. Amit Vyas sir WEBSOL 9214525215 BCA SEM - 4 Page 8


BCA 2 PYTHON WEBSOL UNIT - 1

 Python Explicit Type Conversion

Implicit Type Conversion in Python


In this, method, Python converts the datatype into another datatype automatically. Users don't
have to involve in this process.
# Python program to demonstrate <class 'int'>
# implicit type Casting <class 'float'>
10.0
# Python automatically converts <class 'float'>
# a to int 21.0
a=7 <class 'float'>
print(type(a))

# Python automatically converts


# b to float
b = 3.0
print(type(b))

# Python automatically converts


# c to float as it is a float addition
c=a+b
print(c)
print(type(c))

# Python automatically converts


# d to float as it is a float multiplication
d=a*b
print(d)
print(type(d))

Explicit Type Conversion in Python


In this method, Python needs user involvement to convert the variable data type into the
required data type.
Examples of Type Casting in Python
Mainly type casting can be done with these data type functions:
 Int(): Python Int() function take float or string as an argument and returns int type object.
 float(): Python float() function take int or string as an argument and return float type
object.
 str(): Python str() function takes float or int as an argument and returns string type object.

# Python program to demonstrate 5.0


# type Casting <class 'float'>

Dr. Amit Vyas sir WEBSOL 9214525215 BCA SEM - 4 Page 9


BCA 2 PYTHON WEBSOL UNIT - 1

# int variable
a=5

# typecast to float
n = float(a)

print(n)
print(type(n))

Python Statements
Python Statements In general, the interpreter reads and executes the statements line by line i.e
sequentially. Though, there are some statements that can alter this behavior like conditional
statements. Mostly, python statements are written in such a format that one statement is only
written in a single line. The interpreter considers the ‘new line character’ as the terminator of
one instruction.

Ex :

Print(“hello python”)

Multiple Statements per Line We can also write multiple statements per line, but it is not a
good practice as it reduces the readability of the code. Try to avoid writing multiple statements
in a single line. But, still you can write multiple lines by terminating one statement with the
help of ‘;’. ‘;’ is used as the terminator of one statement in this case.

a = 10; b = 20; c = b + a
print(a); print(b); print(c)

if else Statement
The if-else statement in Python is used to execute a block of code when the condition in the if
statement is true, and another block of code when the condition is false.

if-else Statement Example

Let us understand the use of if-else statements with the following example. Here,
variable age can take different values. If the expression age > 18 is true, then eligible to

Dr. Amit Vyas sir WEBSOL 9214525215 BCA SEM - 4 Page 10


BCA 2 PYTHON WEBSOL UNIT - 1

vote message will be displayed otherwise not eligible to vote message will be displayed.
Following flowchart illustrates this logic −

age=25
print ("age: ", age)
if age >=18:
print ("eligible to vote")
else:
print ("not eligible to vote")

loop
Python has two primitive loop commands:

 while loops
 for loops

The while Loop

With the while loop we can execute a set of statements as long as a condition is true.

i=1
while i < 6:
print(i)
i += 1

for loop
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or
a string).

This is less like the for keyword in other programming languages, and works more like an
iterator method as found in other object-orientated programming languages.

With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.

Dr. Amit Vyas sir WEBSOL 9214525215 BCA SEM - 4 Page 11


BCA 2 PYTHON WEBSOL UNIT - 1

fruits = ["apple", "banana", "cherry"]


for x in fruits:
print(x)

The range() Function


To loop through a set of code a specified number of times, we can use the range() function,

The range() function returns a sequence of numbers, starting from 0 by default, and increments
by 1 (by default), and ends at a specified number.

for x in range(6):
print(x)

pass
The pass statement is used as a placeholder for future code.

When the pass statement is executed, nothing happens, but you avoid getting an error when
empty code is not allowed.

Empty code is not allowed in loops, function definitions, class definitions, or in if statements.

def myfunction(): class Person:


pass pass

ternary expression
A ternary expression in Python, also known as a conditional expression, provides a concise way
to evaluate a condition and return one of two values based on whether the condition is true or
false. It serves as a single-line alternative to a traditional if-else statement for simple conditional
assignments or expressions.

Dr. Amit Vyas sir WEBSOL 9214525215 BCA SEM - 4 Page 12


BCA 2 PYTHON WEBSOL UNIT - 1

age = 20 n=5
status = "Adult" if age >= 18 else "Minor" res = "Even" if n % 2 == 0 else "Odd"
print(status) print(res)
# Output: Adult

Dr. Amit Vyas sir WEBSOL 9214525215 BCA SEM - 4 Page 13

You might also like