Class - VIII
Chapter - 9 and 10
PYTHON LANGUAGE
NOTES:
Python is a popular programming language.
Python can be used on a server to create web applications.
Python is a popular programming language. It was created by Guido van Rossum, and released
in 1991.
It is used for:
web development (server-side),
software development,
mathematics,
system scripting.
>>> print("Hello, World!")
Hello, World!
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code.
Creating a Comment
Comments starts with a #, and Python will ignore them:
>>> #This is a comment
print("Hello, World!")
x=5
y = "John"
print(x)
print(y)
x = str(3)
y = int(3)
z = float(3)
print(x)
print(y)
print(z)
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume). Rules for Python variables:
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and _ )
Variable names
are case-sensitive (age, Age and AGE are three different variables)
myvar = "John"
my_var = "John"
print(myvar)
print(10 > 9)
print(10 == 9)
print(10 < 9)
Python divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Python Conditions and If statements
Python supports the usual logical conditions from mathematics:
Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
These conditions can be used in several ways, most commonly in "if statements" and loops.
An "if statement" is written by using the if keyword.
a = 33
b = 200
if b > a:
print("b is greater than a")
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Python Loops
Python has two primitive loop commands:
while loops
for loops
The while Loop
With the while loop we can execute a set of statements as long as a condition is true.
WHILE LOOP
i=1
while i < 6:
print(i)
i += 1
The break Statement
With the break statement we can stop the loop even if the while condition is true:
i=1
while i < 16:
print(i)
if (i == 10):
break
i += 1
The continue Statement
With the continue statement we can stop the current iteration, and continue with the next:
Example
Continue to the next iteration if i is 3:
i=0
while i < 16:
i += 1
if i == 12:
continue
print(i)
Python For Loops
A for loop is used for iterating over a sequence.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
Example
Print each fruit in a fruit list:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
_____________________________________________________