U2 of python
U2 of python
PROGRAMMING
Introduction-Python Interpreter-Interactive
and script mode -Values and types,
variables, operators, expressions,
statements, precedence of operators,
Multiple assignments, comments, input
function, print function, Formatting
numbers and strings, implicit/explicit type
conversion.
Introduction to PYTHON
Compiled code runs faster because it's already Interpreted code runs slower because it must be
translated into machine code. translated on the fly.
Compilers generate an executable file that can Interpreters require the source code to be
be run independently. present at runtime.
Compilers display all errors at once after Interpreters display errors one at a time during
compilation. execution.
Compiled languages include C, C++, and Java. Interpreted languages include Python, Ruby, and
JavaScript.
Compilers have a longer development cycle due Interpreters have a shorter development cycle as
to the compilation step. code can be tested immediately.
1. Interactive mode
2. Script mode
Interactive mode
• Interactive Mode, as the name suggests, allows us to
interact with OS.
• When we type Python statement, interpreter displays the
result(s) immediately.
Advantages:
Python, in interactive mode, is good enough to learn,
experiment or explore.
Working in interactive mode is convenient for beginners and
for testing small pieces of code.
Drawback:
We cannot save the statements and have to retype all the
statements once again to re-run them
Script mode
Str=‘Python Program’
Str[-3]
Str[3]
Str[6]
Str[7]
Operations on string
• Indexing
• Slicing
• Concatenation
• Repetitions
• Member ship
Indexing Accessing the item in the position
• Indexing
• Slicing
• Concatenation
• Repetitions
• Updation, Insertion, Deletion
Indexing Accessing the item in the particular position
Slicing Slice operator is used to extract part of a string, or some
part of a list Python
Concatenation Adding and printing the items of two lists.
Repetitions multiple copies of the same string
Updating the list Updating the list using index value
Inserting an element Inserting an element in 2nd position
Removing an element Removing an element by giving the element directly
Indexing
Slicing
Concatenation
Repetitions
Updating the list
Inserting an element
Removing an element
Note:
Update,Insert:Nee
d to give Position
Remove:give value
alone
Tuple
• Indexing
• Slicing
• Concatenation
• Repetitions
Note:
concatenate
tuple (not "int")
to tuple
Dictionaries
• Lists are ordered sets of objects, whereas
dictionaries are unordered sets.
• Dictionary is created by using curly brackets. i,e.
{}
• Dictionaries are accessed via keys and not via
their position.
Variables
• Rules:
• A variable allows us to store a value by assigning it to a
name, which can be used later.
• Named memory locations to store values.
• Programmers generally choose names for their variables
that are meaningful.
• It can be of any length. No space is allowed.
• We don't need to declare a variable before using it. In
Python, we simply assign a value to a variable and it will
exist.
Assigning a single value to several variables simultaneously:
a=b=c=100
print(a)
100
print(c)
100
• Statements:
• Instructions that a Python interpreter can executes are called
statements.
• A statement is a unit of code like creating a variable or displaying
>>> n = 17
a value.
>>> print(n)
• Expressions:
• a=2
An expression is a combination of values, variables, and operators.
a+3+2
7
str=("Good"+"Morning")
print(str)
GoodMorning
Multiple assignments
==,<
>
<=
>=
!=
Assignment Operators
print (expression/constant/variable)
print("Hello")
Hello
print(2+4)
6
print(6)
6
Type Casting(Type Conversion)
a=10
b=12.0
c=a+b
print(c)
22.0
Explicit Type Conversion
a="5"
print(int(a))
5
Formatting numbers and strings
price = 49
txt ="The price is {price} dollars"
print(txt)
Output:
The price is {price} dollars
price = 49
txt =f"The price is {price} dollars"
print(txt)
Output:
The price is 49 dollars
• Celsius to Fahrenheit:
F = [(C*9/5) +32]
• convert kilometers to miles
Miles=0.62*km