0% found this document useful (0 votes)
5 views3 pages

Python Programs for Beginners

Uploaded by

mrnmjskvq7
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)
5 views3 pages

Python Programs for Beginners

Uploaded by

mrnmjskvq7
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

11

Module33 —– Program list


Module
1. Writea
Write a program (WAP) to multiplyaa user inputted number with 4. Give
to multiply
appropriate single line comment also.
# The fo1Io ng is an example of single 11ne cDmment
num - input("Please enter your number:”)
answer = int(mm) * d
print("The answer is:", answer)
2. WAP
WAP to
to concatenate the user inputted text with the text “Hello”. Give
appropriate single line comment also.
# The follot ing code 1s to exp Iain the user input
name = input("tJho a re you? ")
print( 'setcome', name)
first name = input("What is your first name? ”)
print('Hello '+ first name)

3. WAP
WAP to add two
to add two user inputted numbers. Give appropriate multi line
comment also.

This program is written by Mr. Nithin sebaszian


Date: 27— 18 — 2824
This is a program to find the sum of two numbers

n1 = input("Enzer your first number:“)


n2 = input("Enzer your second number:”)
ansuer=int(n1) + int(n2)
print("Your answer is”, answer)
4. WAP
WAP to
to find the
the type of variable having different datatypes with the
maximum system size number.

#Inzeger data type


a 35
print("Integer:”, a)
prinz("Type of a:“, type(a))

import sys
num = [Link]
print("\n Maximum size number is:“,num)

# Float data type


b 20.5
prim("\n Float:”, b)
prim("Type of b:“, type(b))

Prepared By Mr. Nithin Sebastian AIS, Ruwais


22

s.
5. WAP to perform the string operations.
WAP to operations.
strg = ”Hello grade8 students"
pr1nt(strg) JI Pr1nts the conp1ete str1ng
print(strg[0]) # Prints the first character Df the string
pr1nt(strg[2 :4]) # Pr1nts characters fnoo 2nd to 3rd
pr1nt(strg[6:]) # Pr1nts str1ng starting fron6
pr1nt((strge ”\n”)’3) # Pr1nts str1ng3 tines
pr1nt(strg+ Welcome”) # Pr1nts concatenated str1ng
print(5trg [-1:]) # Prints last character Df the string
print(strg [-10:]) # Prints 10th character from the end of the string
print(strg [:-1]) # Prints the complete string
print(strg [::-1]) # Prints reverse order of string
print(strg [::2]) # Prints alternate characters
print(strg [::3]) # Prints 3rd alternate characters
print(strg[1:8:2]) # Slicing

# The following are the string methods in python


print(len(strg)) # Prints the length of the string
print([Link]()) # Prints all characters to lowercase
print([Link]()) # Prints all characters to uppercase
cs 400
print(str(cs))

6. WAP to perform they


WAP to the list operations.
ions.
listl = [1, ”Amal”, 13, ’Physics’]
tinylist = [ Manju , 33]
print(list1)
print(list1[0])
print(tiny1ist[ ])
print(list1[ :3])
print(list1[z:])
print(tiny1ist*3)
print(list1 tinylist)
bank 11st = []
for Item In Ost1:
pr1nt(1ten)
7. WAP to perform the tuple operations.
WAP to

Prepared By Mr. Nithin Sebastian AIS, Ruwais


33

tuplel = (1, ”Amal”, 13, ”Physics”)


tinytuple = ('Manju', 3B)
print(tuple1)
print(tuple1[0])
print(tinytuple[1])
print(tuple1[1:3])
print(tuple1[2: ])
print(tinytuple*2)
print(tuple1 + tinytuple)
blank_tuple = ()
for item in tinytuple:
print(item)
8. WAP
WAP to
to show theworking ofaa dictionary.
the working of
dictl = {'Name':
dict1 {'Name': 'Godson',
'Godson', 'Age':
'Age': 33,
33, 'Grade'
'Grade' :: 88 }}
print(dictl)
print(dict1)
print([Link]())
print([Link]())
print([Link]())
print([Link]())
tinydict = {}
{}
tinydict['seven'] = 'This
'This is seven'
seven'
tinydict [1] = 11
tinydict[1]
print(tinydict)
del tinydict['seven']
[Link]() #clears the content of the
dictionary, but the memory reference is not deleted
dictionary,

Prepared By Mr.
M r. Nithin Sebastian AIS, Ruwais

Common questions

Powered by AI

In Python, you can access list elements using indices, such as 'list1[0]' to access the first element. Slicing is performed using 'list1[:3]' to get the first three elements or 'list1[2:]' from the third element onwards. Lists can be concatenated using '+' like 'list1 + tinylist'. Multiplying a list by an integer, such as 'tinylist * 3', repeats its elements .

Tuples are immutable sequences used for ordered data, differing from lists which are mutable. They are defined using parentheses, e.g., 'tuple1 = (1, "Amal", 13)'. Unlike lists, their content cannot be changed after creation. Dictionaries are unordered collections of key-value pairs created with braces, e.g., 'dict1 = {"Name": "Godson", "Age": 33}'. While lists and tuples are indexed by position, dictionaries use keys for access, providing unique identifiers for their values .

Tuples are preferable when you need an immutable collection of ordered elements, such as fixed configurations, coordinate points, or constant parameters, where data integrity is critical. Unlike lists, tuples provide stability and protection against unintended changes. Operational benefits include optimizations in storage and access time due to their immutability and lack of dynamic resizing overhead .

Dictionaries are advantageous for structuring data with unique identifiers or keys, allowing quick access to data through these keys rather than relying on index positions. This makes them ideal for applications needing fast lookups, such as user profiles or caches. For example, dict1 uses {'Name': 'Godson', 'Age': 33} to provide constant-time complexity for searching values by keys, compared to the more sequential approach required with list searching .

To implement this, you request user input, convert the input into an integer, and then multiply it by 4. The operation should be documented with a comment, such as: # The following is an example of a single line comment. The code is: num = input("Please enter your number:") answer = int(num) * 4 print("The answer is:", answer).

You can demonstrate Python's string slicing by using a string like 'strg = "Hello grade8 students"'. Various examples include: strg[:] for the entire string, strg[::-1] for reverse order, strg[1:8:2] for slicing with a step of 2, strg[::3] for every third character, and strg[6:] for everything from the 7th character onwards. All these use different starting points, directions, and steps .

Single-line comments in Python are prefixed with a '#' character and are ideal for short explanations or annotations, e.g., # This is a single-line comment . Multi-line comments utilize triple quotes and are used for more extensive explanations or when the comment spans multiple lines, such as when documenting the author, purpose, and date of a program: '''This program is written by Mr. Nithin sebaszian Date: 27-18-2824 This is a program to find the sum of two numbers''' .

Python uses the sys library to identify the maximum size a number can take in a system, useful for appropriately handling very large integers. You can retrieve this with 'sys.maxsize', which returns a platform-dependent size limit for the largest possible integer to be used. This could determine how calculations and storage are mapped with respect to hardware capabilities .

Python provides several methods for string case transformation, including lower() for converting all characters to lowercase and upper() for uppercase. These can be useful in classroom applications for standardizing input, such as storing student names in a consistent case format in a database, allowing for more reliable analysis and comparison of inputs .

Python determines the type of a variable dynamically when the value is assigned, and you can use the type() function to check this. For example, with 'a = 35', type(a) would return <class 'int'>, indicating an integer. Similarly, for 'b = 20.5', type(b) would return <class 'float'>, indicating a floating-point number .

You might also like