2 Python
2 Python
Mr.M.Yaseen Pasha ,
Assistant Professor,
CVR College Of Engineering,
How to Install & Write
Python Code?
Installation
https://www.python.org/downloads/
How to Run Python Code ?
Now, What is Code
Environment ?
There are 3 main types of environments:
Text Editors
Full IDEs
Notebook Environments
Text Editors
www.anaconda.com/distribution/
3.8 Version
What & Why are
- 34.6
- “Python FDP”
Your basic building bricks when constructing huge
Program
Name Integers
Type int
Numbers in Python!
1.) Types of Numbers in Python
2.) Basic Arithmetic
3.) Differences between classic division
and floor division
4.) Object Assignment in Python
• Python has various "types" of numbers (numeric
literals). We'll mainly focus on integers and floating
point numbers.
• Integers are just whole numbers, positive or
negative. For example: 2 and -2 are examples of
Types of integers.
• Floating point numbers in Python are notable
numbers because they have a decimal point in them, or
use an exponential (e) to define the number.
For example 2.0 and -2.1 are examples of
floating point numbers. 4E2 (4 times 10 to the
power of 2) is also an example of a floating
point number in Python.
Rules for variable names
•names can not start with a number
Assignmen
:'",<>/?|\!@#%^&*~-+
•it's considered best practice (PEP8) that names are lowercase with
underscores
•avoid using Python built-in keywords like list and str
Earlier when discussing strings we introduced the concept of a sequence in Python. Lists
can be thought of the most general version of a sequence in Python. Unlike strings, they
are mutable, meaning the elements inside a list can be changed!
In this section we will learn about:
1.) Creating lists
2.) Indexing and Slicing Lists
3.) Basic List Methods
4.) Nesting Lists
5.) Introduction to List Comprehensions
Lists are constructed with brackets [] and commas separating every element in the list.
Let's go ahead and see how we can construct lists!
Assignment
• # 1. Create a list with certain values
• # 2. Append 10 to the created list - use append() function
• # 3. Delete the last value in the list - use pop() function
• # 4. Delete the 4th index in the list - use pop() function
• # 5. Add ['what', 'is', 'your', 'name'] to the list - use extend() function
• # 6. Access the 5th index in the list
• # 6. Access the last index in the list
• # 7. Create a list with multiple values and a) reverse it using REVERSE
function - use reverse() function, b) reverse it Without using the reverse
function
Dictionaries
We've been learning about sequences in Python but now we're going to switch gears and learn
about mappings in Python. If you're familiar with other languages you can think of these
Dictionaries as hash tables.
This section will serve as a brief introduction to dictionaries and consist of:
1.) Constructing a Dictionary
2.) Accessing objects from a dictionary
3.) Nesting Dictionaries
4.) Basic Dictionary Methods
So what are mappings? Mappings are a collection of objects that are stored by a key, unlike a
sequence that stored objects by their relative position. This is an important distinction, since
mappings won't retain order since they have objects defined by a key.
A Python dictionary consists of a key and then an associated value. That value can be almost any
Python object.
Assignment
• # 1. Create a dictionary with keys and values
• # 2. Add a new key with KEY NAME as 'new_key' and VALUE as 'This_is_ a_new_key'
• # 3. Delete 'new_key'
• # 4. Acccess all the keys in the created dictionary
• # 5. Access all the values in the created dictionary
• # 6. Create a empty dictionary as d
• # 7 .Create the following variables in the dictionary d
• # d['animal'] = ['Dog','cat']
• # d['animal1'] = 'Dog'
• # d['animal2'] = 'Dog','cat'
• # d['answer'] = 42
• # 8. Access the value 'Dog' in the key 'animal'
Tuples
In Python tuples are very similar to lists, however, unlike lists they are immutable meaning
they can not be changed. You would use tuples to present things that shouldn't be changed,
such as days of the week, or dates on a calendar.
In this section, we will get a brief overview of the following:
1.) Constructing Tuples
2.) Basic Tuple Methods
3.) Immutability
4.) When to Use Tuples
You'll have an intuition of how to use tuples based on what you've learned about lists. We
can treat them very similarly with the major distinction being that tuples are immutable.
Set and Booleans
Sets
Sets are an unordered collection of unique elements. We can construct
them by using the set() function. Let's go ahead and make a set to see
how it works
Booleans
Python comes with Booleans (with predefined True and False displays
that are basically just the integers 1 and 0). It also has a placeholder
object called None. Let's walk through a few quick examples of
Booleans (we will dive deeper into them later in this course).
Assignment
• # Questions on Tuple,set:
• # Tuple:
• # Create an empty tuple
• # Create a tuple with multiple data types
• # Access the first two elements of the tuple
• # Pop the last value
• # Create a tuple:(1,2,3,4,5,5,4,6,7,8,9,10). Count number of 5 in the tuple
• # set:
• # create an empty set as s
• # Add 1 to s, add 2 to s, add 1 to s
• # Create a list [1,2,3,4,5,5,4,6,7,8,9,10]
• # Return the set value of the above list as set1
Start with Files Handling ( Why, How and
What ?)
Why File Handling ?
- In general file handling is used for storing your files, or creating a new
file.
- “ r “, for reading.
- “ w “, for writing.
- “ a “, for appending.
- “ r+ “, for both reading and writing
Create File
# Python code to create a file
file = open(‘fcs.txt','w')
file.write("This is the write command")
file.write("It allows us to write in a particular file")
file.close()
Read file
# Python code to illustrate read() mode
file = open("fcs.txt", "r")
print file.read()
file.close()
Append File
# Python code to illustrate append() mode
file = open(‘emp.txt','a')
file.write("This will add this line")
file.close()
Using with() function
# Python code to illustrate with()
with open("fcs.txt") as file:
data = file.read()
# do something with data
Split function with Python -
# Python code to illustrate split() function