2 Basics of Python
2 Basics of Python
Python Programming
Using Problem Solving Approach
Reema Thareja
10
Example:
11
Example:
12
Python variable assignment is different from some of the popular languages like c, c++ and java. There is no
declaration of a variable, just an assignment statement.
Python is a dynamically typed language. It doesn’t know about the type of the variable until the code is run. So
declaration is of no use.
What it does is, It stores that value at some memory location and then binds that variable name to that memory
container.
And makes the contents of the container accessible through that variable name.
So the data type does not matter, as it will get to know the type of the value at run-time.
Example:
15
16
17
Example:
18
Example:
19
22
23
24
25
27
Identity Operators
is Operator: Returns true if operands or values on both sides of the operator point to the same object and false
otherwise. For example, if a is b returns 1, if id(a) is same as id(b).
is not Operator: Returns true if operands or values on both sides of the operator does not point to the same object
29
and false otherwise. For example, if a is not b returns 1, if id(a) is not same as id(b).
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Expressions
An expression is any legal combination of symbols (like variables, constants and operators) that represents a value. In
Python, an expression must have at least one operand (variable or constant) and can have one or more operators. On
evaluating an expression, we get a value. Operand is the value on which operator is applied.
Constant Expressions: One that involves only constants. Example: 8 + 9 – 2
Integral Expressions: One that produces an integer result after evaluating the expression. Example:
a = 10
• Floating Point Expressions: One that produces floating point results. Example: a * b / 2
• Relational Expressions: One that returns either true or false value. Example: c = a>b
• Logical Expressions: One that combines two or more relational expressions and returns a value as True or False.
Example: a>b && y! = 0
• Bitwise Expressions: One that manipulates data at bit level. Example: x = y&z
30
Examples
:
32
33
34
Examples
:
35
Example:
36
37
38
Key Characteristics
Attributes
•Objects have attributes that store data.
•In the example, name and age are attributes of the Person object.
Methods
•Objects have methods that define their behavior.
•Methods are functions associated with objects.
Encapsulation
•Objects encapsulate data and methods, promoting modular and organized code.
Core Concepts
• Classes: Blueprint for creating objects.
• Inheritance: Enables a class to inherit properties and methods from another class.
• Polymorphism: Allows objects to take on multiple forms.
Reusability
• Objects and classes promote code reuse.
• Classes can be instantiated multiple times to create different objects.
Modularity
• Objects provide a modular structure, enhancing code organization.
• Changes in one part of the code do not affect other parts, fostering maintainability.
Abstraction
• Objects allow abstraction of complex systems, making code more understandable and manageable.