0% found this document useful (0 votes)
3 views

01 Python Notes

The document provides a comprehensive introduction to Python programming, covering basic concepts such as variables, data types, operators, control structures, and functions. It includes numerous examples illustrating how to use Python for various tasks, including input handling, arithmetic operations, and data structures like lists, tuples, sets, and dictionaries. Additionally, it explains the importance of comments and the use of built-in, module, and user-defined functions.

Uploaded by

Biju Kuttan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

01 Python Notes

The document provides a comprehensive introduction to Python programming, covering basic concepts such as variables, data types, operators, control structures, and functions. It includes numerous examples illustrating how to use Python for various tasks, including input handling, arithmetic operations, and data structures like lists, tuples, sets, and dictionaries. Additionally, it explains the importance of comments and the use of built-in, module, and user-defined functions.

Uploaded by

Biju Kuttan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Python-1

> Our First Python Program

print("Hello World")

> A Key Point to know about Python


- It is a case sensitive language

> Variables

Basic Types in Python - numbers(integers, floating), boolean,


strings

Example 1:
----------

name = "shradha"
age = 22
print(name)
print(age)

> Comments

# This is a comment & useful for people reading your code


# This is another line

Example 2:
----------

# reading input from keyboard

name = input("What is your name? ")


print("Hello " + name)

Example 3:
----------

# Type Conversion

old_age = input("Enter your age : ")


new_age = int(old_age) + 2
print(new_age)

# Useful conversion functions

1. float()
2. bool()
3. str()
Python-2

4. int()

Example 4:
----------

# Sum of 2 Numbers

first_number = input("Enter first number : ")


second_number = input("Enter second number : ")
sum = float(first_number) + float(second_number)
print("Sum is : " + str(sum))

Example 5:
----------

# Strings

name = "Tony Stark"


print(name.upper())
print(name)

print(name.lower())
print(name)

print(name.find('y'))
print(name.find('Y'))
print(name.find("Stark"))
print(name.find("stark"))

print(name.replace("Tony Stark", "Ironman"))


print(name)

print("Stark" in name)
print("S" in name)
print("s" in name)

Output
------

TONY STARK
Tony Stark
tony stark
Tony Stark
3
-1
5
-1
Python-3

Ironman
Tony Stark
True
True
False

> Arithmetic Operators


-----------------------

print(5 + 2)
print(5 - 2)
print(5 * 2)
print(5 / 2)
print( 5 // 2)
print(5 % 2)
print(5 ** 2)

Output
------

7
3
10
2.5
2
1
25

> Comparison Operators


------------------------

greater than >


less than <
less than or equal to <=
greater than or euqal to >=
not equal to !=
is equal ==

> Logical Operators


--------------------

or (atleast one is true)


and (both are true)
not (reverses any value)

number = 2
Python-4

print(number > 3)
print(number < 3)
print(not number > 3)
print(not number < 3)
print(number > 3 and number > 1)
print(number > 3 or number > 1)

> Operator precedence


Python-5

> Branching statements


-----------------------

# if statement

age = 13
if age >= 18:
print("you are an adult")
print("you can vote")
elif age < 3:
print("you are a child")
else:
print("you are in school")
print("thank you")

Example
--------

# Simple Calculator

first = input("Enter first number : ")


second = input("Enter second number : ")
first = int(first)
second = int(second)
print("----press keys for operator (+,-,*,/,%)----------")
operator = input("Enter operator : ")

if operator == "+":
print(first + second)
elif operator == "-":
print(first - second)
elif operator == "*":
print(first * second)
elif operator == "/":
print(first / second)
elif operator == "%":
print(first % second)
else:
print("Invalid Operation")

> Iterative statements


-----------------------

> Range in Python


Python-6

range() function returns a range object that is a sequence of


numbers.

------------
for loop
------------

Example
--------
for i in range(5):
print(i)

Output
------
0
1
2
3
4

Example
--------

for i in range(2,5):
print(i)

Output
------
2
3
4

Example
--------

for i in range(2,5,2):
print(i)

Output
------
2
4
Python-7

while Loop
------------

Example
--------

i = 1
while(i <= 5):
print(i)
i = i + 1

Output
------
1
2
3
4
5

Example
-------

i = 1
while(i <= 5):
print(i * "*")
i = i + 1

Output
-------
*
**
***
****
*****

> List
-------
Example
--------

friends = ["amar", "akbar", "anthony"]


print(friends[0])
print(friends[1])
print(friends[-1])
print(friends[-2])
print("-------------------------------")
Python-8

friends[0] = "aman"
print(friends)
print(friends[0:2]) #returns a new list
print("-------------------------------")

for friend in friends:


print(friend)

Output
------
amar
akbar
anthony
akbar
-------------------------------
['aman', 'akbar', 'anthony']
['aman', 'akbar']
-------------------------------
aman
akbar
anthony

--------
Example
--------

marks = ["english", 95, "chemistry", 98]


marks.append("physics")
marks.append(97)
print(marks)
print("-----------------------------------")

marks.insert(0, "math")
marks.insert(1, 99)
print(marks)
print("-----------------------------------")

print("math" in marks)

print("------------------------------------")
i = 0
while i < len(marks):
print(marks[i])
print(marks[i+1])
i = i + 2
Python-9

print("------------------------------------")

print(len(marks)/2)
marks.clear()
print(marks)

Output
-------

['english', 95, 'chemistry', 98, 'physics', 97]


-----------------------------------
['math', 99, 'english', 95, 'chemistry', 98, 'physics', 97]
-----------------------------------
True
------------------------------------
math
99
english
95
chemistry
98
physics
97
------------------------------------
4.0
[]

> Break & Continue


--------------------

Example
-------

students = ["ram", "shyam", "kishan", "radha", "radhika"]

for student in students:


if(student == "radha"):
break
print(student)

print("---------------------")

for student in students:


if(student == "kishan"):
continue
print(student)
Python-10

Output
-------

ram
shyam
kishan
---------------------
ram
shyam
radha
radhika

> Tuples
---------
They are like lists (sequence of objects) but they are immutable
i.e. once they have been defined we cannot change them.
Parenthesis in tuples are optional.

Example
--------

marks = (95, 98, 97, 97)


#marks[0] = 98
print(marks.count(97))
print(marks.index(97))

Output
------
2
2

Sets
-----
Sets are a collection of all unique elements.
Indexing is not supported in sets.

Example
-------

marks = { 98, 97, 95, 95 }


print(marks)
for score in marks:
print(score)
Python-11

Output
-------
{97, 98, 95}
97
98
95

> Dictionary
-------------

Dictionary is an unordered collection of Items. Dictionary stores


a (key, value) pair.

Example
--------

marks = { "math" : 99, "chemistry" : 98, "physics" : 97 }


print(marks)
print(marks["chemistry"])

marks["english"] = 95
print(marks)

marks["math"] = 96
print(marks)

Output
------

{'math': 99, 'chemistry': 98, 'physics': 97}


98
{'math': 99, 'chemistry': 98, 'physics': 97, 'english': 95}
{'math': 96, 'chemistry': 98, 'physics': 97, 'english': 95}

> Functions in Python


-----------------------

Function is a piece of code that performs some task. (In a tv


remote, each button performs a functions, so a function is like
that button in code)

There are 3 types of functions in python :


Python-12

a. In-built functions
----------------------

# int() str() float() min() range() max()

b. Module functions
--------------------

Module is a file that contains some functions & variables which


can be imported for use in other files. Each module should contain
some related tasks.

Example : math, random, string

import math
print(dir(math))

print("---------------------------------------")

import random
print(dir(random))

print("---------------------------------------")

import string
print(dir(string))

print("---------------------------------------")

from math import sqrt


print(sqrt(4))

-----------------------------
c. User-defined functions
-----------------------------

Example
--------

def sum(a, b=4):


print(a + b)

sum(1, 2)
sum(1)
Python-13

Output
------
3
5

*** THE END ***


-------------------

You might also like