Python Introduction
Python Introduction
Programs
05
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 the ABC
programming language capable of exception handling and interfacing with the Amoeba
operating system.
Literals in python are nothing but a succinct way of representing the data types. In simpler
words, it is the way to represent a fixed value in our source code.
Ex:
Literals are numbers (or) strings (or)
78 # Integer literal
characters that appears directly in a
21.98 # Floating point literal
program. A list of some literals in
“Q” # Character literal
python is as follows.
“Hello” # String literal
Numeric Literal: (c): Complex >> a= 7+8j
(b):Float >> print(78.256)
(a): Integer >> x=2586 >> b= 5j
Output: 78.256
>> y= -9856 print(a)
print(x,y) (d):long >> x = 037467 Print(b)
Output: 2586,-9856 print(x) Output: (7+8j)
Output: 5j
BASICS OF PYTHON PROGRAMMING AND CONTROL STATEMENTS
2.
Literal Constant
String Literals
i. Single-line string ii. Multi-line string
String literals that are enclosed within single A collection of characters (or) a string that goes on
quotes (‘ ‘) are knows as single line string for multiple lines is a multi line string.
This kind of string can be done in two ways
Ex:
(a) Adding backslash at the end of every line
# string literals
# single line literals (b) Using triple quotes
Ex:
Single_quotes_string = ‘ Scaler Academy ’
# string literals
Double_quotes_string = “ Hello world “ Output:
# multi line literals
Prinf(string_quotes_string) Welcome
Str = ”””
Print(double_quotes_string) to
Welcome
Output: scaler
to
scaler Academy Academy
scaler
Hello world
Academy ”””
Operators are symbols, such as +, –, =, >, and < that perform certain mathematical or logical operation
to manipulate data values and produce a result based on some rules. An operator manipulates the
data values called operands
4 Logical Operators
5 Bitwise Operators
Multiplication
The following
Assignment operators are either Simple assignment is done with the equal sign (=) and simply
simple assignment operator or assigns the value of its right operand to the variable on the left.
compound assignment operators. For example,
Ex:
1 >>> x = 5 In ➀ you assign an integer value of 5 to variable x.
2 In ➁ an integer value of 1 is added to the variable x on the right side and the
>>> x = x + 1
value 6 after the evaluation is assigned to the variable x.
Compound assignment
Compound assignment operators support shorthand notation for avoiding the repetition of the left-side
variable on the right side. Compound assignment operators combine assignment operator with another
operator with = being placed at the end of the original operator.
For example, the statement
>>> x = x + 1
can be written in a compactly form as shown below.
>>> x += 1
2 Assignment Operators
Ex:
>>> p = 10
>>> q = 12 >>> q **= p
>>> q += p >>> q 1024.0
>>> q 22 >>> q //= p
>>> q *= p >>> q 102.0
>>> q 220
>>> q /= p
>>> q 22.0
>>> q %= p
>>> q 2.0
For example,
1. >>>10 == 12
False
6. >>>10 >= 12
2. >>>10 != 12
False
True
7. >>> "P" < "Q“
3. >>>10 < 12
True
True
8. >>> "Aston" > "Asher"
4. >>>10 > 12
True
False
9. >>> True == True
5. >>>10 <= 12
True
True
4 Logical Operators
The logical operators are used for comparing or negating the logical values of their operands and to return the
resulting logical value. The values of the operands on which the logical operators operate evaluate to either True or
False. The result of the logical operator is always a Boolean value, True or False.
Logical AND Performs AND operation and the result is True p and q results in False
and
when both operands are True
Operators
not Logical NOT Reverses the operand state not p results in False
5 Bitwise Operators
Variables can hold values of different types called data types, thus it need different data types to store
different types of values in the variables.
For example:
A person’s age is stored is a number,
His name is made of only characters and
His address is a mixture of numbers and characters.
Follow the below-mentioned rules for creating legal variable names in Python.
Variable names can consist of any number of letters, underscores and digits.
Variable should not start with a number.
In Python Keywords are not allowed as variable names.
Variable names are case-sensitive.
For example, computer and Computer are different variables.
variable_name = expression
For example:
>>> number =100
>>> number
100
>>> century = 100
>>> century
100
An identifier is a name given to a variable, function, class or module. Identifiers may be one or more
characters in the following format:
Keywords are a list of reserved words that have predefined meaning. Keywords are special vocabulary
and cannot be used by programmers as identifiers for variables, functions, constants or with any
identifier name. Attempting to use a keyword as an identifier name will cause an error.
Python makes use of input() function. The input() function prompts the user to provide
some information on which program can work and give the result.
Block 1
Block 2
Block 3
Block 2, continuation
Block 1, continuation
Decision Control Flow it is necessary should know the control flow statements in program that are
Statements to be executed. There are three fundamental methods for control flow in a
programming language i.e.,
i. if – statements
ii. if-else statements
conditional
Selection control flow statements iii. Nested if statements branching
iv. Multi-way if-elif-else statements
i. while
ii. for Iterative
Loop control flow statements
iii. Nested Loops statements
iv. else with for and while
if condition:
statement1
The Syntax for ‘if’ statement is;
………
indentation statement n
Statement x
Start
Program to increment a number if it is positive
Output:
Statement x
x = 11
Program 1.1: Write a program to determine Program 1.2: Write a program to determine the
whether a person is eligible to vote. character entered by the user.
character”)
Output
press any key: 10
The user has entered a digit
Remember:
(a) The statement(s) must be indented at least one space right of the if statement.
(b) In case there is more than one statement after the if condition, then each statement
must be indented using the same number of spaces to avoid indentation errors.
(c) The statement(s) within the if block are executed if the Boolean expression evaluates
to true.
statement block 1
Program 1.3: Write a program to determine whether a person is eligible to vote or not. If he is not eligible ,
display how many years are left to be eligible.
Statement3 Output
Enter any number, 0-30: 25
It is in the range 20-30
If-elif-else statements are used to test additional conditions apart from the initial test expression.
Program 1.5: Write a program to test whether a number is positive, negative , or equal to zero
Note: in while loop, the condition is tested before any of statements in statement block is executed.
Statement x
while(condition):
statement block
Statement y
Statement X
Example program
i=0
Update the condition While(i<=10):
expression print(i,end=“ “)
Condition
i=i+1
Output:
True
Statement block False
Statement Y
The for loop provides a mechanism to repeat a task until particular condition is true.
Statement block 1
False
Statement block 2
The range() is a in-built function python that is used to iterate over a sequence of
numbers. The system of range () is Range( beg, end, [step])
beg
Example: Example: step
for i in range(1,5) for i in range(1,10,5)
print(i, end=“ “) print(i, end=“ “) end
Output: Output:
Print the numbers
1234 in same line 13579
Key points:
range(10) is equal to writing range(0,10) [ with values from 0 to argument-1]
range(0,10) is called two arguments
range(1,10,2) is called three arguments
Nested loop are used to place the condition inside other loops. This feature will work with
any loop like while loop as well as for loop.
for i in range(1,6):
print(“pass”,i,”- “, end=‘ ‘)
for j in range(1,6):
print(j,end=‘ ‘)
To print the following pattern
print()
Output:
pass 1- 1 2 3 4 5
pass 2- 1 2 3 4 5
pass 3- 1 2 3 4 5
pass 4- 1 2 3 4 5
pass 5- 1 2 3 4 5
Break’ in Python is a loop control statement. It is used to control the sequence of the loop.
No
Remaining Body of
loop
Program
i =1
While i<=10:
print(i,end=” “)
if i==5:
break
i=i+1
print(“\n Done”)
Output:
1 2 3 4 5
Done
False
Loop
Control
No
Remaining Body of
loop
i =0
for i in range(9):
While i<10:
i+=1 if i == 3:
if i==3: continue
continue print(i)
print(i, end=“”)
Output:
Output:
0 1 2 4 5 6 7 8 9
1 2 4 5 6 7 8 9