0% found this document useful (0 votes)
3 views7 pages

Unit 2 Introduction To Python

The document provides an introduction to Python programming, covering its basics, features, modes of operation, keywords, identifiers, built-in data types, and operators. It explains the differences between mutable and immutable data types and includes sample questions and answers related to Python concepts. Overall, it serves as a comprehensive guide for beginners to understand the fundamentals of Python programming.

Uploaded by

fathma462
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)
3 views7 pages

Unit 2 Introduction To Python

The document provides an introduction to Python programming, covering its basics, features, modes of operation, keywords, identifiers, built-in data types, and operators. It explains the differences between mutable and immutable data types and includes sample questions and answers related to Python concepts. Overall, it serves as a comprehensive guide for beginners to understand the fundamentals of Python programming.

Uploaded by

fathma462
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

INTRODUCTION TO PYTHON

Basics of Python programming, Python interpreter - interactive and script


mode, the structure of a program, indentation, identifiers, keywords,
constants, variables, types of operators, precedence of operators, data
types, mutable and immutable data types, statements, expressions,
evaluation and comments, input and output statements, data type
conversion, debugging.

Control Statements: if-else, for loop

Lists: list operations - creating, initializing, traversing and manipulating


lists, list methods and built-in functions.

Dictionary: concept of key-value pair, creating, initializing, traversing,


updating and deleting elements, dictionary methods and built-in functions.

Introduction :

Python is a General-Purpose object-oriented programming language, which


means that it can model real-world entities. The distinctive feature of Python is
that it is an interpreted language. The Python IDLE (Integrated Development
Environment) executes instructions one line at a time. This also lets us use it as
a calculator.

Guido van Rossum named it after the comedy group Monty Python Flying Circus
a Comedy Serial of BBC.

Python Features :
a. Easy
b. Interpreted
c. Object-Oriented
d. Free and Open Source
e. Portable
f. Large Python Library

Interpreter : It executes / converts high level code into machine code (source
to target code) line by line, and slow compare to compiler

Compiler : It executes / converts code in one go and faster compared to


Interpreter.
MODES OF PYTHON WORKING

Interactive Mode
Interactive mode is running blocks or a single line of Python code. The code
executes via the Python shell. Interactive mode is handy when you just want to
execute basic Python commands or you are new to Python programming.

Script Mode
If you need to write a long piece of Python code or your Python script spans
multiple files, interactive mode is not recommended. Script mode is the way to
go in such cases. In script mode, You write your code in a text file then save it
with a .py extension which stands for "Python". This file can be executed by
Python Interpreter later on.

PYTHON KEYWORDS
Python keywords are special reserved words that have specific meanings
and purposes and can’t be used for anything but those specific purposes. An
example of something you can’t do with Python keywords is assign something to
them.
Examples of keywords are : False, else, import, pass, in, is, True, and continue
etc.

Python Identifiers

Identifiers are names given to different entities such as constants,


variables, structures, functions, etc. Example: int, amount; True,
totalbalance; In the above example, amount and totalbalance are identifiers and
int, and double are keywords.

Rules for Identifiers

 Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A


to Z) or digits (0 to 9) or an underscore _ . ...
 An identifier cannot start with a digit. ...
 Keywords cannot be used as identifiers. ...
 We cannot use special symbols like !, @, #, $, % etc. ...
 An identifier can be of any length.

BUILT-IN 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:
Text Type: Str

Numeric Types: int, float, complex

Sequence Types: list, tuple, range

Mapping Type: dict

Set Types: Set

Boolean Type: Bool (Only True and False Value)

None Type: None

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: Python divides
the operators in the following groups:

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

PYTHON ARITHMETIC OPERATORS


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

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
PYTHON ASSIGNMENT OPERATORS
Assignment operators are used to assign values to variables:

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 LOGICAL OPERATORS


Logical operators are used to combine conditional statements:

Operator Description Example

and Returns True if both statements x < 5 and x <


are true 10

or Returns True if one of the x < 5 or x < 4


statements is true

not Reverse the result, returns not(x < 5 and


False if the result is true x < 10)

PYTHON IDENTITY OPERATORS


Identity operators are used to compare the objects, not if they are equal, but if
they are actually the same object, with the same memory location:
Operator Description Example

is Returns True if both variables are x is y


the same object

is not Returns True if both variables are x is not y


not the same object

PYTHON MEMBERSHIP OPERATORS


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

Operator Description Example

in Returns True if a sequence with the x in y


specified value is present in the
object

not in Returns True if a sequence with the x not in y


specified value is not present in the
object

PYTHON BITWISE OPERATORS

Bitwise operators are used to compare (binary) numbers:

Operator Name Description

& AND Sets each bit to 1 if both bits are 1

| OR Sets each bit to 1 if one of two bits is 1

^ XOR Sets each bit to 1 if only one of two bits is 1

~ NOT Inverts all the bits

<< Zero fill left Shift left by pushing zeros in from the right
shift and let the leftmost bits fall off

>> Signed Shift right by pushing copies of the leftmost


right shift bit in from the left, and

WHAT ARE MUTABLE AND IMMUTABLE DATA TYPES IN PYTHON?

Python Mutable data types are those whose values can be changed in place
whereas Immutable data types are those that can never change their value in
place.
Here are Mutable data types :
1. Lists
2. Dictionaries
3. Sets

And Immutable data types in Python:


1. Integers
2. Floating-Point numbers
3. Booleans
4. Strings
5. Tuples
SAMPLE QUESTION AND ANSWERS

[Link] developed python programming language? Write any two name(s) of IDE
for python language.

Ans: Guido Van Rossum in 1990 developed python programming language

[Link] Names of any two Python IDE

Ans. IDE- Spyder, Pycharm, IDLE

3. Identify the valid identifiers from following-


f@name , as , _tax , roll_no , 12class , totalmarks , addr1
Ans: _tax, roll_no, totalmarks, add1

4. What is difference between equality (==) and identity (is) operator?


Ans: Equality (==) compares values while identity (is) compares memory
address of variables/objects.

5. Write python code to convert the time given in minutes into hours and
minutes.
Ans:
min=int(input(“Enter time in minutes &”))
h=min//60
m=min%60
print(“Hours=”, h)
print(“Minutes=”, m)

6. Evaluate (to true or false)each of the following expression:


14<=14, 14<14, -14 > -15, -15 >=15
1. True, False, True, False
2. False, False , True, True
3. True, False, True, True
4. False, True, False, False
Ans: True, False, True, True

7 What will be the output of the following python statements?


a,b,c = 10,40,20
a,c,b = b+10, a+20, c-10
print(a,b,c)
print(a+b//c**2)
Ans: 50 10 30 50

You might also like