0% found this document useful (0 votes)
6 views18 pages

Python unit1

The document provides an overview of identifiers in Python, including rules for naming them and examples of valid and invalid identifiers. It also discusses various types of operators in Python, such as arithmetic, logical, and bitwise operators, along with their usage. Additionally, the document covers Python data types, including numeric, sequence, boolean, set, and dictionary types, explaining their characteristics and how to create them.

Uploaded by

kngp5405
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)
6 views18 pages

Python unit1

The document provides an overview of identifiers in Python, including rules for naming them and examples of valid and invalid identifiers. It also discusses various types of operators in Python, such as arithmetic, logical, and bitwise operators, along with their usage. Additionally, the document covers Python data types, including numeric, sequence, boolean, set, and dictionary types, explaining their characteristics and how to create them.

Uploaded by

kngp5405
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/ 18

Identifiers in Python

Identifier is a user-defined name given to a variable, function, class,

module, etc. The identifier is a combination of character digits and an

underscore. They are case-sensitive i.e., 'num' and 'Num' and 'NUM' are

three different identifiers in python. It is a good programming practice to

give meaningful names to identifiers to make the code understandable.

We can also use the Python string isidentifier() method to check whether a

string is a valid identifier or not.

Rules for Naming Python Identifiers

● It cannot be a reserved python keyword.

● It should not contain white space.

● It can be a combination of A-Z, a-z, 0-9, or underscore.

● It should start with an alphabet character or an underscore ( _ ).

● It should not contain any special character other than an

underscore ( _ ).

Examples of Python Identifiers

Valid identifiers:
● var1

● _var1

● _1_var

● var_1

Invalid Identifiers

● !var1

● 1var

● 1_var

● var#1

● var 1

Python Keywords and Identifiers Examples

Example 1: Example of and, or, not, True, False keywords.

print("example of True, False, and, or, not keywords")

# compare two operands using and operator


print(True and True)

# compare two operands using or operator


print(True or False)

# use of not operator


print(not False)

Output
example of True, False, and, or, not keywords
True
True
True

Example 2: Example of a break, continue keywords and identifier.

# execute for loop


for i in range(1, 11):

# print the value of i


print(i)

# check the value of i is less than 5


# if i lessthan 5 then continue loop
if i < 5:
continue

# if i greater than 5 then break loop


else:
break

Output
1
2
3
4
5

Python Operators
In Python programming, Operators in general are used to perform
operations on values and variables. These are standard symbols used for
logical and arithmetic operations. In this article, we will look into different
types of Python operators.
● OPERATORS: These are the special symbols. Eg- + , * , /, etc.

● OPERAND: It is the value on which the operator is applied.

Types of Operators in Python

Arithmetic Operators in Python

Python Arithmetic operators are used to perform basic mathematical

operations like addition, subtraction, multiplication and division.

In Python 3.x the result of division is a floating-point while in Python 2.x

division of 2 integers was an integer. To obtain an integer result in Python

3.x floored (// integer) is used.

Example of Arithmetic Operators in Python:

# Variables
a = 15
b = 4

# Addition
print("Addition:", a + b)

# Subtraction
print("Subtraction:", a - b)

# Multiplication
print("Multiplication:", a * b)

# Division
print("Division:", a / b)

# Floor Division
print("Floor Division:", a // b)

# Modulus
print("Modulus:", a % b)

# Exponentiation
print("Exponentiation:", a ** b)

Output
Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3.75
Floor Division: 3
Modulus: 3
Exponentiation: 50625

Comparison of Python Operators

In Python Comparison of Relational operators compares the values. It

either returns True or False according to the condition.

Logical Operators in Python

Python Logical operators perform Logical AND, Logical OR and Logical

NOT operations. It is used to combine conditional statements.

The precedence of Logical Operators in Python is as follows:


1. Logical not

2. logical and

3. logical or

Bitwise Operators in Python

Python Bitwise operators act on bits and perform bit-by-bit operations.

These are used to operate on binary numbers.

Bitwise Operators in Python are as follows:

1. Bitwise NOT

2. Bitwise Shift

3. Bitwise AND

4. Bitwise XOR

5. Bitwise OR

Assignment Operators in Python

Python Assignment operators are used to assign values to the variables.

This operator is used to assign the value of the right side of the expression

to the left side operand.


Example of Assignment Operators in Python:

a = 10
b = a
print(b)
b += a
print(b)
b -= a
print(b)
b *= a
print(b)
b <<= a
print(b)

Output
10
20
10
100
102400

Identity Operators in Python

In Python, is and is not are the identity operators both are used to check if

two values are located on the same part of the memory. Two variables

that are equal do not imply that they are identical.

is True if the operands are identical


is not True if the operands are not identical

Expressions in Python
An expression is a combination of operators and operands that is

interpreted to produce some other value. In any programming language,

an expression is evaluated as per the precedence of its operators. So that

if there is more than one operator in an expression, their precedence

decides which operation will be performed first. We have many different

types of expressions in Python. Let's discuss all types along with some

exemplar codes :

1. Constant Expressions: These are the expressions that have constant

values only.

Example:

# Constant Expressions
x = 15 + 1.3

print(x)

Output

16.3

2. Arithmetic Expressions: An arithmetic expression is a combination of

numeric values, operators, and sometimes parenthesis. The result of this

type of expression is also a numeric value. The operators used in these

expressions are arithmetic operators like addition, subtraction, etc. Here

are some arithmetic operators in Python:


Operator Synta
Functioning
s x

+ x+y Addition

- x-y Subtraction

* x*y Multiplication

/ x/y Division

// x // y Quotient

% x%y Remainder

** x ** y Exponentiati
on

3. Integral Expressions: These are the kind of expressions that produce

only integer results after all computations and type conversions.

Example:

# Integral Expressions
a = 13
b = 12.0

c = a + int(b)
print(c)

Output

25

4. Floating Expressions: These are the kind of expressions which produce

floating point numbers as result after all computations and type

conversions.

Example:

# Floating Expressions
a = 13
b = 5

c = a / b
print(c)
Output

2.6

5. Relational Expressions: In these types of expressions, arithmetic

expressions are written on both sides of relational operator (> , < , >= ,

<=). Those arithmetic expressions are evaluated first, and then compared

as per relational operator and produce a boolean output in the end. These

expressions are also called Boolean expressions.

Example:

# Relational Expressions
a = 21
b = 13
c = 40
d = 37

p = (a + b) >= (c - d)
print(p)

Output

True

6. Logical Expressions: These are kinds of expressions that result in either

True or False. It basically specifies one or more conditions. For example,

(10 == 9) is a condition if 10 is equal to 9. As we know it is not correct, so

it will return False. Studying logical expressions, we also come across

some logical operators which can be seen in logical expressions most

often. Here are some logical operators in Python:


Operato Synta
Functioning
r x

P and It returns true if both P and Q are true otherwise


and
Q returns false

or P or Q It returns true if at least one of P and Q is true

not not P It returns true if condition P is false

7. Bitwise Expressions: These are the kind of expressions in which

computations are performed at bit level.

Example:

# Bitwise Expressions
a = 12

x = a >> 2
y = a << 1

print(x, y)

Output
3 24

8. Combinational Expressions: We can also use different types of

expressions in a single expression, and that will be termed as

combinational expressions.

Example:

# Combinational Expressions
a = 16
b = 12

c = a + (b >> 1)
print(c)

Output

22

But when we combine different types of expressions or use multiple

operators in a single expression, operator precedence comes into play.

Python Data Types


Python Data types are the classification or categorization of data items. It

represents the kind of value that tells what operations can be performed

on a particular data. Since everything is an object in Python programming,

Python data types are classes and variables are instances (objects) of

these classes. The following are the standard or built-in data types in

Python:

● Numeric - int, float, complex


● Sequence Type - string, list, tuple

● Mapping Type - dict

● Boolean - bool

● Set Type - set, frozenset

● Binary Types - bytes, bytearray, memoryview

DataTypes

This code assigns variable 'x' different values of few Python data types -

int, float, list, tuple and string. Each assignment replaces the previous

value, making 'x' take on the data type and value of the most recent

assignment.

1. Numeric Data Types in Python

The numeric data type in Python represents the data that has a numeric

value. A numeric value can be an integer, a floating number, or even a

complex number. These values are defined as Python int, Python float and

Python complex classes in Python.

● Integers - This value is represented by int class. It contains

positive or negative whole numbers (without fractions or

decimals). In Python, there is no limit to how long an integer

value can be.

● Float - This value is represented by the float class. It is a real

number with a floating-point representation. It is specified by a


decimal point. Optionally, the character e or E followed by a

positive or negative integer may be appended to specify scientific

notation.

● Complex Numbers - A complex number is represented by a

complex class. It is specified as (real part) + (imaginary part)j . For

example - 2+3j

2. Sequence Data Types in Python

The sequence Data Type in Python is the ordered collection of similar or

different Python data types. Sequences allow storing of multiple values in

an organized and efficient fashion. There are several sequence data types

of Python:

● Python String

● Python List

● Python Tuple

String Data Type

Python Strings are arrays of bytes representing Unicode characters. In

Python, there is no character data type Python, a character is a string of

length one. It is represented by str class.


Strings in Python can be created using single quotes, double quotes or

even triple quotes. We can access individual characters of a String using

index.

List Data Type

Lists are just like arrays, declared in other languages which is an ordered

collection of data. It is very flexible as the items in a list do not need to be

of the same type.

Creating a List in Python

Lists in Python can be created by just placing the sequence inside the

square brackets[].

Access List Items

In order to access the list items refer to the index number. In Python,

negative sequence indexes represent positions from the end of the array.

Instead of having to compute the offset as in List[len(List)-3], it is enough

to just write List[-3]. Negative indexing means beginning from the end, -1

refers to the last item, -2 refers to the second-last item, etc.

Tuple Data Type


Just like a list, a tuple is also an ordered collection of Python objects. The

only difference between a tuple and a list is that tuples are immutable.

Tuples cannot be modified after it is created.

Creating a Tuple in Python

In Python Data Types, tuples are created by placing a sequence of values

separated by a ‘comma’ with or without the use of parentheses for

grouping the data sequence. Tuples can contain any number of elements

and of any datatype (like strings, integers, lists, etc.).

3. Boolean Data Type in Python

Python Data type with one of the two built-in values, True or False.

Boolean objects that are equal to True are truthy (true), and those equal to

False are falsy (false). However non-Boolean objects can be evaluated in a

Boolean context as well and determined to be true or false. It is denoted

by the class bool.

4. Set Data Type in Python

In Python Data Types, Set is an unordered collection of data types that is

iterable, mutable, and has no duplicate elements. The order of elements in

a set is undefined though it may consist of various elements.

Create a Set in Python


Sets can be created by using the built-in set() function with an iterable

object or a sequence by placing the sequence inside curly braces,

separated by a ‘comma’. The type of elements in a set need not be the

same, various mixed-up data type values can also be passed to the set.

5. Dictionary Data Type

A dictionary in Python is a collection of data values, used to store data

values like a map, unlike other Python Data Types that hold only a single

value as an element, a Dictionary holds a key: value pair. Key-value is

provided in the dictionary to make it more optimized. Each key-value pair

in a Dictionary is separated by a colon : , whereas each key is separated by

a ‘comma’.

You might also like