Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
39 views

Python Basic and Advanced-Day 6

The document provides an overview of Python basics and advanced concepts. It discusses: - Python basics like environment setup, syntax, variables, operators, decision making, loops, data types, lists, tuples, dictionaries, date/time, functions, and modules - Python advanced topics such as classes/objects, regular expressions, CGI programming, database access, networking, sending email, multithreading, XML processing, and GUI programming. - It also covers modules in more depth including how to create, use, name, and rename modules as well as built-in modules. Finally, it discusses files I/O in Python like opening, writing, reading, and closing files.

Uploaded by

Ashok Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Python Basic and Advanced-Day 6

The document provides an overview of Python basics and advanced concepts. It discusses: - Python basics like environment setup, syntax, variables, operators, decision making, loops, data types, lists, tuples, dictionaries, date/time, functions, and modules - Python advanced topics such as classes/objects, regular expressions, CGI programming, database access, networking, sending email, multithreading, XML processing, and GUI programming. - It also covers modules in more depth including how to create, use, name, and rename modules as well as built-in modules. Finally, it discusses files I/O in Python like opening, writing, reading, and closing files.

Uploaded by

Ashok Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Python Basics & Advanced

1 Python Basics
o Python – Overview
2 Python Advanced
o Python - Classes/Objects
o Python - Environment Setup
Content
o Python - Basic Syntax o Python - Reg Expressions

o Python - Variable Types o Python - CGI Programming

o Python - Basic Operators o Python - Database Access

o Python - Decision Making o Python - Networking


o Python - Loops o Python - Sending Email
o Python - Numbers o Python - Multithreading
o Python - Strings o Python - XML Processing
o Python - Lists o Python - GUI Programming
o Python - Tuples
o Python - Dictionary
o Python - Date & Time
o Python - Functions
o Python - Modules
o Python - Files I/O
o Python - Exception
Python Basics & Advanced
Quick Recap

Introduction to Python & Its characteristics Python IO and Module

01 Covered the basic details of Python and why it is so


popular. 02 Python Variables
Rules for defining the Python Variables
and how to assign values to these
05 Module and IO method
reading write and closing
files
Variables.
Python Datatype , Operators and Conditional
Statement Date & time Function in Python, Numpy

03 During this we gone through Numbers, String and Python


collections (List, Tuple, Set, Dictionary , Operator and
Conditional statement (if , elif, nested if) 04 Function, Numpy, Date and Time
o Quick recap
o Modules in Python
o How to create Modules
o How to use Modules
Day-6 o Modules Naming and renaming
Agenda o Package Vs Modules
o Python - Files I/O
o Writing to a file
o Reading a file
o Closing a file
Python Basics
Modules
A module allows you to logically organize your Python code. Grouping related code into a module makes the code
easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and
reference.
Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module
can also include runnable code.

Create a Module Use a Module

Save this code in a file named hello.py Import the module named hello, and call the
greeting function:
def hello(name):
print("Hello, " + name) import hello
hello.greeting(“BigDataFactory")
Python Basics
Modules in Python –Naming and renaming

Naming a Module
You can name the module file whatever you like, but it must have the file extension .py

Re-naming a Module
You can create an alias when you import a module, by using the as keyword:

import hello
hello.greeting(“BigDataFactory")

import hello as bdf


bdf.greeting(“BigDataFactory")
Python Basics
Modules in Python-Built-in Modules

Built-in Modules
There are several built-in modules in Python, which you can import whenever you like.

import platform

x = platform.system()
print(x)

Using the dir() Function


import platform

x = dir(platform)
print(x)
Python Basics
Modules in Python-Difference between package and modules

Differences Between Python Modules and Packages

o A module is a file containing Python code. A package, however, is like a directory that holds sub-packages

and modules.

o A package must hold the file __init__.py. This does not apply to modules.

o To import everything from a module, we use the wildcard *. But this does not work with packages.
Python Basics
Python - Files I/O

Python allows for user input. That means we are able to ask the user for input.

Python uses the input() method.

Example-1
username = input("Enter username:")
print("Username is: " + username)

Example-2
str = input("Enter your input: ")
print "Received input is : ", str

Enter your input: [x*5 for x in range(2,10,2)]


Python Basics
Python - Files I/O (open and close)
The open Function
new_file = open("testfile1", "wb")
print("Name of the file: ", new_file.name)
Syntax print("Closed or not : ", new_file.closed)
file object = open(file_name [, access_mode][, buffering]) print("Opening mode : ", new_file.mode)

Sr.No. Modes Description


1 r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.

2 rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.

3 r+ Opens a file for both reading and writing. The file pointer placed at the beginning of the file.

4 rb+ Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file.

5 w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

6 wb Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

7 w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

8 wb+ Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

9 a Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

10 ab Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

11 a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and
writing.
12 ab+ Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file
for reading and writing.
Python Basics
Python - Files I/O (open and close)
The close Function
Syntax
fileObject.close()

# Close opened file


new_file.close()

The write() Method


fileObject.write(string)
new_file = open(" new_file ", "w")
new_file.write( "Python is a great language.\nYeah its great!!\n")

# Reading a file
str=new_file.read()
print(str)
Thank you

You might also like