0% found this document useful (0 votes)
8 views7 pages

Python

Uploaded by

satyam96310
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)
8 views7 pages

Python

Uploaded by

satyam96310
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/ 7

python

February 7, 2024

[ ]: # from google.colab import drive


# drive.mount('/content/drive')

0.1 print() function prints the specified message to the screen, or other standard
output device
[ ]: # Say hello to everyone.

print("Hello world!")

0.2 Python’s f-strings allow you to use variables inside strings to build dynamic
messages.
• Backslash can not be used in format string directly.
[33]: first_name = "indrajeet"
last_name = "sinha"
full_name = f"{first_name} {last_name}"
print(full_name)

print(f"Hello, {full_name.title()}!")
print(f"Hello, {full_name.upper()}!")
print(f"Hello, {full_name.lower()}!")

indrajeet sinha
Hello, Indrajeet Sinha!
Hello, INDRAJEET SINHA!
Hello, indrajeet sinha!

[47]: # Prints today's date with help


# of datetime library
# import datetime as dt
from datetime import datetime as dt

# today = dt.datetime.today()
today1 = dt.now()
print(f"{today1:%B %d, %Y}")

1
# The datetime object has a method, called strftime(), for formatting date␣
↪objects into readable strings.

print(today1.strftime("%H"),"\n",today1.strftime("%M"))

January 31, 2024


10
17

0.3 A reference of all the legal format codes:


%a Weekday, short version
%A Weekday, full version
%w Weekday as a number (0-6)
%d Day of month (01-31)
%b Month name, short version
%B Month name, full version
%m Month as a number
%y Year, short version
%Y Year, full version
%H Hour 00-23
%I Hour 00-12
%p AM/PM PM
%M Minute
%S Second

0.4 Lists
• Python Lists are just like dynamically sized arrays.
• A list stores a series of items in a particular order. You access items using an index, or within
a loop.
• Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list

2
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the first item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list

[59]: motorcycles = []
bike = "honda yamaha"
motorcycles.append(bike)
motorcycles.extend(['yamaha','suzuki'])
motorcycles.append('suzuki')

print(motorcycles)
len(motorcycles)

['honda yamaha', 'yamaha', 'suzuki', 'suzuki']

[59]: 4

[ ]: motorcycles = ['honda', 'yamaha', 'suzuki']


print(motorcycles)

motorcycles[0] = 'ducati'
print(motorcycles)

motorcycles.append('ducati')
print(motorcycles)

['honda', 'yamaha', 'suzuki']


['ducati', 'yamaha', 'suzuki']
['ducati', 'yamaha', 'suzuki', 'ducati']

[ ]: motorcycles = ['honda', 'yamaha', 'suzuki']

motorcycles.insert(0, 'ducati')
print(motorcycles)

['ducati', 'honda', 'yamaha', 'suzuki']

[66]: motorcycles = ['honda', 'yamaha', 'suzuki']


print(motorcycles)

del motorcycles[0:2]
print(motorcycles)

3
['honda', 'yamaha', 'suzuki']
['suzuki']

[68]: motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']


print(motorcycles)

motorcycles.remove('yamaha')
print(motorcycles)

['honda', 'yamaha', 'suzuki', 'ducati']


['honda', 'suzuki', 'ducati']

[64]: motorcycles = ['honda', 'yamaha', 'suzuki']


print(motorcycles)

popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)
print(f"The last motorcycle I owned was a {popped_motorcycle.title()}.")

['honda', 'yamaha', 'suzuki']


['honda', 'yamaha']
suzuki
The last motorcycle I owned was a Suzuki.

[ ]: motorcycles = ['honda', 'yamaha', 'suzuki']

first_owned = motorcycles.pop(0)
print(f"The first motorcycle I owned was a {first_owned.title()}.")

The first motorcycle I owned was a Honda.

[ ]: motorcycles = ['honda', 'yamaha', 'suzuki']


print(motorcycles[2])

suzuki

[ ]: my_foods = ['pizza', 'noodles', 'cake']

# This doesn't work:


friend_foods = my_foods

my_foods.append('pastry')
friend_foods.append('ice cream')

print("My favorite foods are:")


print(my_foods)

4
print("\nMy friend's favorite foods are:")
print(friend_foods)

My favorite foods are:


['pizza', 'noodles', 'cake', 'pastry', 'ice cream']

My friend's favorite foods are:


['pizza', 'noodles', 'cake', 'pastry', 'ice cream']

[53]: my_foods = ['pizza', 'noodles', 'cake']

# This will work:


# friend_foods = my_foods[:]
friend_foods = my_foods.copy()

my_foods.append('pastry')
friend_foods.append('ice cream')

print("My favorite foods are:")


print(my_foods)

print("\nMy friend's favorite foods are:")


print(friend_foods)

My favorite foods are:


['pizza', 'noodles', 'cake', 'pastry']

My friend's favorite foods are:


['pizza', 'noodles', 'cake', 'ice cream']

0.5 Python programming language provides two types of loops:


• For and While to handle looping requirements.
• For loops are used for sequential traversal. > - Example: traversing a list or string or array
etc.
• while loop is used to execute a block of statements repeatedly until a given condition is
satisfied.

0.6 Note: Using else statement with While Loop in Python


• The else clause is only executed when your while condition becomes false. If you break out
of the loop, or if an exception is raised, it won’t be executed.

5
0.6.1 For Loop

[ ]: dimensions = (200, 50)


print(dimensions[0])
print(dimensions[1])

print("\nOriginal dimensions:")
for dimension in dimensions:
print(dimension)

200
50

Original dimensions:
200
50

[ ]: for value in range(5):


print(value)

0
1
2
3
4

[ ]: for value in range(1, 6):


print(value)

1
2
3
4
5

[ ]: ''' The range() function returns a sequence of numbers,


starting from 0 by default, and increments by 1 (by default),
and stops before a specified number. '''

numbers = range(1, 6)
print(numbers, type(numbers))

numbers= list(range(1, 6))


print(numbers)

range(1, 6) <class 'range'>


[1, 2, 3, 4, 5]

6
[70]: # The list() function is used to convert an iterable (such as a string, tuple␣
↪or dictionary) into a list.

even_numbers = list(range(2, 11, 2))


print(even_numbers)

[2, 4, 6, 8, 10]

[ ]: magicians = ['indra', 'manas', 'raghav']


for magician in magicians:
print(f"{magician.title()}, that was a great trick!")

Indra, that was a great trick!


Manas, that was a great trick!
Raghav, that was a great trick!

[74]: players = ['rohit', 'rahul', 'kohli', 'jadeja', 'shami']

print("Here are the last three players on my team:")


for player in players[-3:]:
print(player.title())

Here are the last three players on my team:


Rohit
Rahul
Kohli

[ ]: squares = []
for value in range(1, 11):
square = value ** 2
squares.append(square)

print(squares)

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

[ ]: squares = []
for value in range(1, 11):
squares.append(value**2)

print(squares)

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

[ ]:

You might also like