What is Python?
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.
What can Python do?
•Python can be used on a server to create web applications.
•Python can be used alongside software to create workflows.
•Python can connect to database systems. It can also read and modify files.
•Python can be used to handle big data and perform complex mathematics.
•Python can be used for rapid prototyping, or for production-ready software development.
Python (in general)
PEP. ‘Python Enhancement Proposal’. This is how Python evolves.
BDFL. ‘Benevolent Dictator for Life’. A friendly way to refer to Guido van Rossum, creator of Python.
IDE. ‘Integrated Development Environment’. A program that helps you getting your (coding) job done.
IDLE. Recursive acronym for ‘Integrated Development and Learning Environment’ with is Monty Python
related as one of the members is called Eric Idle. That’s the IDE that comes distributed with the
standard Python distribution.
Spyder. The anaconda IDE it itself an acronym: ‘Scientific PYthon Development EnviRonment’. It will give
you a lot of the scientific Python libraries.
Venv. ‘Virtual ENVironments’. Isolation tools. It’s were you build your programs to avoid any change it
the outer environment screws your code without you being able to figure how and why.
Python Variables
In Python, a Variable is a named memory location that stores and manipulates data. Variables are created when a
value is assigned to them, and there is no command to declare a variable. Technically, the variable acts as an
address for where the data is stored in memory. A Python variable may be assigned a value of one type and then
later re-assigned a value of a different type.
For example,
x=5
y = "John" 5
print(x) John
print(y)
Case-Sensitive
Variable names are case-sensitive.
Example
This will create two variables:
a=4
4
A = "Sally"
#A will not overwrite a
Sally
Important Functions in Python
The key function for working with files in Python is the open() function.
The open() function takes two parameters; filename, and mode.
Example : To open a file for reading it is enough to specify the name of the file:
f = open(“mydemofile.txt")
The print() function prints the specified message to the screen, or other standard output device.
The message can be a string, or any other object, the object will be converted into a string before
written to the screen.
Example : Print more than one object:
Hello how are you?
print("Hello", "how are you?")
The input() function allows user input.
Example
Use the prompt parameter to write a message before the input:
x = input('Enter your name:') Enter your name:
print('Hello, ' + x) Hello, Seema
# this is to take string input from the user. (in this Seema is the input from the user)
X = int (input (”Enter number-")) # this is for taking input in number from the user
Enter number-5
( in this number 5 is the input from the user)
One example
The following code will take input in number from the user and then add and display the result.
num1= int (input (“enter no.1-”))
num 2= int (input( ”enter no.2-”))
result = num1 + num2
print(result)
Python Comments
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.
Comments starts with a #, and Python will ignore them:
Example : Hello, Students!
print("Hello, Students!") #This is just a comment to test
Important Operators in Python
Relational Operators used for comparison in Python:
Arithmetic Operators in Python :
Assignment Operators in Python :
Some important functions and their syntax in Python :
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a
string).
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.
Example : Print i as long as i is less than 6:
1
i=1 2
while i < 6: 3
print(i) 4
i += 1 5
The break Statement
With the break statement we can stop the loop even if the while condition is true:
Example – Exit the loop when i is 3:
i=1
while i < 6: 1
print(i) 2
if i == 3: 3
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 1
while i < 6: 2
i += 1 4
if i == 3: 5
continue 6
print(i) # Note that number 3 is missing in the result
The else Statement
With the else statement we can run a block of code once when the condition no longer is true:
Example- Print a message once the condition is false:
i=1 1
while i < 6: 2
print(i) 3
i += 1 4
else: 5
print("i is no longer less than 6") i is no longer less than 6
The For Loops
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a
string).
This is less like the for keyword in other programming languages, and works more like an iterator
method as found in other object-orientated programming languages.
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:
apple
fruits = ["apple", "banana", "cherry"] banana
for x in fruits: cherry
print(x)
The for loop does not require an indexing variable to set beforehand.
Looping Through a String
Even strings are iterable objects, they contain a sequence of characters: b
Example - Loop through the letters in the word "banana": a
n
for x in "banana": a
print(x) n
a
The break Statement
With the break statement we can stop the loop before it has looped through all the items:
Example - Exit the loop when x is "banana":
fruits = ["apple", "banana", "cherry"]
for x in fruits: apple
print(x) banana
if x == "banana":
break
Exit the loop when x is "banana", but this time the break comes before the print:
fruits = ["apple", "banana", "cherry"] apple
for x in fruits:
if x == "banana":
break
print(x)
The continue Statement
With the continue statement we can stop the current iteration of the loop, and continue with the next:
Example - Do not print banana:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
apple
if x == "banana":
cherry
continue
print(x)
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.
Example :
a = 33
b = 200
if b > a: b is greater than a
print("b is greater than a")
Elif
The elif keyword is Python's way of saying "if the previous conditions were not true, then try this
condition".
Example :
a = 33
b = 33
if b > a:
print("b is greater than a") a and b are equal
elif a == b:
print("a and b are equal")
Else
The else keyword catches anything which isn't caught by the preceding conditions.
Example :
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal") a is greater than b
else:
print("a is greater than b")
You can also have an else without the elif:
Example :
a = 200
b = 33
if b > a:
print("b is greater than a") b is not greater than a
else:
print("b is not greater than a")
Some codes and their results using
different operators and functions
Q 1. Write a code to multiply 2 with 5.
print (2*5) Output will be – 10
Q 2. Write a code to multiply 2 with 5 and add 4.
Print (2*5+4) Output will be – 14
(while solving keep BODMAS in mind).
Q 3. Write a code to compare 2 numbers using if…else.
X=5
Y=10
if x>y:
print("x is greater")
else: Output will be - x is less than or equal to 10
print("x is less than or equal to 10")
Q 4. Write a code to print all even numbers from 1 to 12 using while loop.
i=2
while i <=12:
Output will be – 2 4 6 8 10
print(i)
i += 2
Q 6. Write a code to find the sum of numbers using while loop.
i=1
sum=0
while i<=10: # means we are taking 1-10 numbers
sum=sum + i
Output will be – the sum is-55
i = i+1
print("the sum is-", sum)
Q 5. Write a code to check whether the number is positive, negative or equal to zero.
num= int ( input (“enter number-”))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Q6. Write a program to take a number input from the user and compare using two commands.
num=int(input(“enter a number-”))
If num > 0 and num <100:
print(“it’s a positive number upto 100”)
else:
print(“its not a positive number between 0-100”)
Q7. write a code to print a pattern of increasing stars.
i=1
star= "*“
while i <= 3:
*
j=1
while j <= i:
print(star, end=" ")
j += 1
* *
print() * * *
i += 1