0% found this document useful (0 votes)
4 views15 pages

Python Basic

The document provides an overview of basic Python concepts, including operators, data types, lists, loops, tuples, sets, and dictionaries. It also covers the use of PIP for package management and includes assignments for practical application of the concepts learned. Key operations and methods for each data structure are highlighted, along with examples and instructions for user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

Python Basic

The document provides an overview of basic Python concepts, including operators, data types, lists, loops, tuples, sets, and dictionaries. It also covers the use of PIP for package management and includes assignments for practical application of the concepts learned. Key operations and methods for each data structure are highlighted, along with examples and instructions for user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Python Basic

Notes
• In python ‘/’ operator gives floating point value -> 3/2 =
1.5
• For Integer division we use ‘//’ operator -> 3//2 = 1
• To change the type in python ->
• str(x) to change into string
• int(x) to change into integer
• float(x) to change into float
• To take the input from user we use input() function ->
Python PIP
• PIP is a package manager for Python packages, or modules if you like.
• A package contains all the files you need for a module.
• Modules are Python code libraries you can include in your project.
• Navigate your command line to the location of Python's script directory, and type the following: C:\Users\
YourName\AppData\Local\Programs\Python\Python36-32\Scripts>pip –version
• If you do not have PIP installed, you can download and install it from this page: https://pypi.org/project/pip/
• Note: If you have Python version 3.4 or later, PIP is included by default.
• If you have pip install packages using command: pip install package_name
• pip install jupyter -> jupyter notebook / python –m notebook
Lists
• Ordered sequence of values
• Written as a sequence of comma-separated values
between square brackets
• Values can be of different types
• usually the items all have the same type
More Operations on Lists
•L.append(x) • L.pop()
•L.extend(seq) • L.index(x)
•L.insert(i, x)
• L.count(x)
•L.remove(x)
•L.pop(i) • L.sort()
• L.reverse()
x is any value, seq is a sequence value (list, string, tuple, …), i is
an integer value
For Loop
• For loop can be used in two different ways:
1. Using Range ->rsum=0.0# the reciprocal sum
# the for loop
for i in range(1,101):
rsum = rsum + 1.0/i
print ('sum is', rsum)

2. Using List -> l=[10, ‘ball’, 4.7]# list

# the for loop


for i in l:
print (l)
Tuples
• A tuple consists of a number of values separated by
commas

• Nested tuple
Unpacking Sequences
• Strings and Tuples are examples of sequences
• Indexing, slicing, concatenation, repetition operations
applicable on sequences
• Sequence Unpacking operation can be applied to
sequences to get the components
• Multiple assignment statement
• LHS and RHS must have equal length
Unpacking Sequences
Sets
• An unordered collection with no duplicate elements
Dictionaries
• Unordered set of key:value pairs,
• Keys have to be unique and immutable
• Key:value pairs enclosed inside curly braces {...}
• Empty dictionary is created by writing {}
• Dictionaries are mutable
• add new key:value pairs,
• change the pairing
• delete a key (and associated value)
Operations on Dictionaries
Operations on Dictionaries
Operations on Dictionaries

# Remember: for ... in iterates over keys only


# Sort values in a list
Assignment
1. Write a program to calculate the factorial of a given number using loops. The number should be entered by
the user, and the program should handle invalid input (negative numbers).
2. Write a Python program to create a dictionary named student_data with keys
'Name', 'Age', and 'Marks', where 'Marks' is another dictionary containing subject-
wise marks for 'Math', 'Science', and 'English'. Add a new key 'Grade' with a value
of 'A', update the 'Age' key to 21, and display the value of 'Name' and 'Math'
marks. Iterate through the dictionary to print all keys and values, remove the key
'Grade' from the dictionary, and check if the key 'Age' exists, printing its value if it
does.
3. Write a Python program to display only those numbers from a list that satisfy the following conditions:
1. The number must be divisible by five

2. If the number is greater than 150, then skip it and move to the following number

3. If the number is greater than 500, then stop the loop

You might also like