Python Basic and Advanced-Day 6
Python Basic and Advanced-Day 6
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
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")
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)
x = dir(platform)
print(x)
Python Basics
Modules in Python-Difference between package and modules
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.
Example-1
username = input("Enter username:")
print("Username is: " + username)
Example-2
str = input("Enter your input: ")
print "Received input is : ", str
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()
# Reading a file
str=new_file.read()
print(str)
Thank you