Python Basic
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)
• 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
2. If the number is greater than 150, then skip it and move to the following number