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

Python Programming (Unit-1)

The document provides an introduction to Python programming, covering its history, features, and basic concepts such as variables, data types, and operators. It highlights the advantages of Python, including its readability, portability, and ease of use. Additionally, it explains Python tokens, variable assignment, and various types of operators, along with examples to illustrate their usage.

Uploaded by

shankarspshukla
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)
4 views10 pages

Python Programming (Unit-1)

The document provides an introduction to Python programming, covering its history, features, and basic concepts such as variables, data types, and operators. It highlights the advantages of Python, including its readability, portability, and ease of use. Additionally, it explains Python tokens, variable assignment, and various types of operators, along with examples to illustrate their usage.

Uploaded by

shankarspshukla
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
You are on page 1/ 10

KALI CHARAN NIGAM INSTITUTE OF

TECHNOLOGY, BANDA

“PYTHON PROGRAMMING”
BCC-402

UNIT – I

Introduction to Python: Python variables, Python basic Operators,


Understanding python blocks. Python Data Types, Declaring and using
Numeric data types: int, float etc.

Compiled by
Abhishek Tiwari
(Assistant Professor)
INTRODUCTION
The programming language Python was conceived in the late 1980s, and its implementation
was started in December 1989 by Guido van Rossum at CWI in the Netherlands as a
successor to ABC capable of exception handling and interfacing with the Amoeba operating
system. Van Rossum is Python's principal author, and his continuing central role in deciding
the direction of Python is reflected in the title given to him by the Python community,
Benevolent Dictator for Life (BDFL). (However, Van Rossum stepped down as leader on July
12, 2018). Python was named after the BBC TV show Monty Python's Flying Circus.

Old Python logo, 1990s–2006 New Python logo, 2006–present

Python is a widely used general-purpose, high level programming language. It was initially
designed by “Guido van Rossum” in 1991 and developed by Python Software Foundation. It
was mainly developed for emphasis on code readability, and its syntax allows programmers to
express concepts in fewer lines of code. Python is a programming language that lets you work
quickly and integrate systems more efficiently.
There are two major Python versions- Python 2 and Python 3.
 On 16 October 2000, Python 2.0 was released with many new features.
 On 3rd December 2008, Python 3.0 was released with more testing and includes new
features.
 As of April 8, 2025, the latest Python 3 release is Python 3.13.2

Why to use Python:


The following are the primary factors to use python in day-to-day life:
1. Python is object-oriented
Structure supports such concepts as polymorphism, operation overloading and multiple
inheritance.
2. Indentation
Indentation is one of the greatest feature in python
3. It’s free (open source)
Downloading python and installing python is free and easy
4. It’s Powerful
 Dynamic typing
 Built-in types and tools
 Library utilities
 Third party utilities (e.g. Numeric, NumPy, sciPy)
 Automatic memory management
5. It’s Portable
 Python runs virtually every major platform used today
 As long as you have a compatible python interpreter installed, python programs will
run in exactly the same manner, irrespective of platform.
6. It’s easy to use and learn
 No intermediate compile
 Python Programs are compiled automatically to an intermediate form called byte
code, which the interpreter then reads.
 This gives python the development speed of an interpreter without the performance
loss inherent in purely interpreted languages.
 Structure and syntax are pretty intuitive and easy to grasp.
7. Interpreted Language
Python is processed at runtime by python Interpreter
8. Interactive Programming Language
Users can interact with the python interpreter directly for writing the programs
9. Straight forward syntax
The formation of python syntax is simple and straight forward which also makes it
popular.

PYTHON TOKENS
In python tokens are small individual unit that are used to create or write program instructions.
Python provides multiple types of tokens:
 Keywords
 Identifiers
 Literals
 Operators
 Punctuations
Keywords: Keywords are reserved words whose meaning is fixed and will never change.

Python Variable (Identifiers)


Python Variable is containers that store values. Python is not “statically typed”. We do not
need to declare variables before using them or declare their type. A variable is created the
moment we first assign a value to it. A Python variable is a name given to a memory location.
It is the basic unit of storage in a program.
 The value stored in a variable can be changed during program execution.
 A Variables in Python is only a name given to a memory location, all the operations done
on the variable effects that memory location.

Rules for Python variables


1. A Python variable name must start with a letter or the underscore character.
2. A Python variable name cannot start with a number.
3. A Python variable name can only contain alpha-numeric characters and underscores (A-
z, 0-9, and _ ).
4. Variable in Python names are case-sensitive (name, Name, and NAME are three different
variables).
5. The reserved words(keywords) in Python cannot be used to name the variable in Python.

Assigning Values to Variables:


Python variables do not need explicit declaration to reserve
memory space. The declaration happens automatically when you assign a value to a variable.
The equal sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and the operand to the right
of the = operator is the value stored in the variable.

For example –
a = 100 # An integer assignment
b = 1000.0 # A floating point
c = "John" # A string
print (a)
print (b)
print (c)
This produces the following result –
100
1000.0
John
Multiple Assignment: Python allows you to assign a single value to several variables
simultaneously.
For example:
a=b=c=1
Here, an integer object is created with the value 1, and all three variables are assigned to the
same memory location.
You can also assign multiple objects to multiple variables.
For example –
a,b,c = 1,2,"mrcet“
Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively,
and one string object with the value "john" is assigned to the variable c.
Output Variables:
The Python print statement is often used to output variables. Variables do not need to be
declared with any particular type and can even change type after they have been set.
x=5 # x is of type int
x = "mrcet " # x is now of type str
print(x)
Output: mrcet
To combine both text and a variable, Python uses the “+” character:
Example:
x = "awesome"
print("Python is " + x)
Output: Python is awesome
You can also use the + character to add a variable to another variable:
Example:
x = "Python is "
y = "awesome"
z=x+y
print(z)
Output: Python is awesome

Python basic Operators


Operators are used to perform operations on variables and values.
In Python you can implement the following operations using the corresponding tokens.

Python divides the operators in the following groups:

 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Identity operators
 Membership operators
 Bitwise operators

Arithmetic operators

Operator Name Example


+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y

Assignment operators
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3

Python Comparison Operators


Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Logical operators

Operator Description Example


and Returns True if both statements are true x < 5 and x < 10

Or Returns True if one of the statements is true x < 5 or x < 4

Not Reverse the result, returns False if the result is not(x < 5 and x < 10)
true

Identity operators

Operator Description Example


is Returns True if both variables are the same x is y
object
is not Returns True if both variables are not the x is not y
same object

Membership operators
Operator Description Example
in Returns True if a sequence with the specified value x in y
is present in the object
not in Returns True if a sequence with the specified value x not in y
is not present in the object

Bitwise operators

Operator Name Description Example


& AND Sets each bit to 1 if both bits are 1 x&y
| OR Sets each bit to 1 if one of two bits is 1 x|y
^ XOR Sets each bit to 1 if only one of two bits is x^y
1
~ NOT Inverts all the bits ~x
<< Zero fill left Shift left by pushing zeros in from the right x << 2
shift and let the leftmost bits fall off
>> Signed right Shift right by pushing copies of the x >> 2
shift leftmost bit in from the left, and let the
rightmost bits fall off

Understanding python blocks


In Python, a block of code is a group of statements that are indented together. Blocks are used
to define functions, classes, and loops. They can also be used to group related statements
together to make the code more readable.

Here are some of the rules for Python block syntax:


 Blocks must be indented by four spaces (1 Tab Spacing).
 Statements in a block must be indented by the same amount of space.
 The scope of a variable in a block is limited to the block itself.

Python Data Types


The data stored in memory can be of many types. For example, a student roll number is stored
as a numeric value and his or her address is stored as alphanumeric characters. Python has
various standard data types that are used to define the operations possible on them and the
storage method for each of them.

int:
int, or integer, is a whole number, positive or negative, without decimals, of unlimited
length.
>>> print(24656354687654+2)
24656354687656
>>> print(20)
20

>>> print(0b10)
2

>>> print(0B10)
2

>>> print(0X20)
32

>>> 20
20

>>> 0b10
2

>>> a=10
>>> print(a)
10

# To verify the type of any object in Python, use the type() function:
>>> type(10)
>>> a=11
>>> print(type(a))

<class ‘int‘>

float:
float, or "floating point number" is a number, positive or negative, containing one or
more decimals. float can also be scientific numbers with an "e" to indicate the power of 10.

>>> y=2.8
>>> y
2.8
>>> y=2.8
>>> print(type(y))
<class ‘float’>
>>> type(.4)
<class ‘float’>
>>> 2.
2.0

Example:
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))

Output:
<class ‘float’>
<class ‘float’>
<class ‘float’>

Boolean:
Objects of Boolean type may have one of two values, True or False:
>>> type(True)
>>> type(False)

String:
 Strings in Python are identified as a contiguous set of characters represented in the
quotation marks. Python allows for either pairs of single or double quotes.
 'hello' is the same as "hello".
 Strings can be output to screen using the print function.

For example:
print("hello")
>>> print("mrcet college")
mrcet college
>>> type("mrcet college")
<class ‘str’>
>>> print('mrcet college')
mrcet college
>>> " "
''
If you want to include either type of quote character within the string, the simplest way is to
delimit the string with the other type. If a string is to contain a single quote, delimit it with
double quotes and vice versa:
>>> print("mrcet is an autonomous (') college")
mrcet is an autonomous (') college
>>> print('mrcet is an autonomous (") college')
mrcet is an autonomous (") college

Suppressing Special Character:

Specifying a backslash (\) in front of the quote character in a string “escapes” it and
causes Python to suppress its usual special meaning. It is then interpreted simply as a literal
single quote character:

>>> print("mrcet is an autonomous (\') college")


mrcet is an autonomous (') college
>>> print('mrcet is an autonomous (\") college')
mrcet is an autonomous (") college

The following is a table of escape sequences which cause Python to suppress the usual
special interpretation of a character in a string:
>>> print('a\ ....b')
a....b
>>> print('a\
b\
c')
abc
>>> print('a \n b')
a
b
>>> print("mrcet \n college")
mrcet
college

You might also like