Python Tutorial: Introduction for Beginners (Part 2)

CtechF - Python Tutorial Part 2

Python Tutorial: Introduction for Beginners (Part 2)

1) What is Python?

Python is a scripting language and that is used for creating engineering-analysis tools, web development (server-side), software development, system scripting, and animation software. It designed by Guido van Rossum and first released in 1991. It’s an interpreted, High-level. General-purpose language. Python runs on Windows, Mac OS, and Linux. In this tutorial. You will learn Some advanced programming concepts

Read : Python Tutorial: Introduction for beginners (Basic) – Installation and Basic Programming

2) Python Tutorial

2.1) Function

A function is a block of code. It only runs when it is called.

* Creating a Function

def func():
  print("Hello World!")

* Calling a Function

def func():
  print("Hello World!")

func()
CtechF - Python Tutorial Part 2

* Passing a Parameter

def func(name):
  print("Hello " + name)

func("World")
func("Friends")
CtechF - Python Tutorial Part 2

* Passing a Parameter with default values

If you call the function without parameter, it uses the default value to the output.

def func(name = "CtechF"):
  print("Hello" + name)

func("world")
func("Friends")
func()
CtechF - Python Tutorial Part 2

* Return Values

def func(no1, no2):
  return no1 + no2

print(func(10,25))
print(func(50,12))
CtechF - Python Tutorial Part 2

2.2) Module

A module containing a set of functions. it’s same as the code library.

* Create a Module

import datetime #Enter moduler name here 

* Using a Module

import datetime

today = datetime.datetime.now()

print(today.year)
print(today.month)
print(today.day)
CtechF - Python Tutorial Part 2

* Renaming a Module

import datetime as mymod

today = mymod.datetime.now()
print(today.year)
print(today.month)
print(today.day)
CtechF - Python Tutorial Part 2

2.3) Try Except

* Using Try Except

try:
  print(Hello) #Hello is not defined
except:
  print("error")
CtechF - Python Tutorial Part 2

* try-else-finally

try:
  print(a)
except:
  print("Error")
else:
  print("Done")
CtechF - Python Tutorial Part 2
try:
  a = 12
  print(a)
except:
  print("error")
finally:
  print("Done")
CtechF - Python Tutorial Part 2

2.4) File Handling

* File Open

“r” – Read (Opens a file for reading
“a” – Append (Opens a file for appending) – append to the end of the content
“w” – Write (Opens a file for writing) – overwrite any existing content
“x” – Create (Creates the specified file)

file = open("names.txt", "r") #You can enter location of file here

file.close()
CtechF - Python Tutorial Part 2

* Read a file

file = open("names.txt", "r") #You can enter location of file here
print(file.read())
file.close()
CtechF - Python Tutorial Part 2

* Read lines

file = open("names.txt", "r") #You can enter location of file here
print(file.readline())
print(file.readline())
print(file.readline())
file.close()
CtechF - Python Tutorial Part 2
file = open("names.txt", "r") #You can enter location of file here

for line in file:
  print(line)

file.close()
CtechF - Python Tutorial Part 2

* File Write – Append (“a”) – It will create a file, if the file does not exist

file = open("names.txt", "a")
file.write("\nNew name")
file.close()

#Read a file
fileo = open("names.txt", "r")
print(fileo.read())
fileo.close()
CtechF - Python Tutorial Part 2

* File Write – Write (“w”) – It will create a file, if the file does not exist

file = open("names.txt", "w")
file.write("New name")
file.close()

#Read a file
fileo = open("names.txt", "r")
print(fileo.read())
CtechF - Python Tutorial Part 2

* File Write – Write (“x”) – It will create a file, if the file was exist. it will return a error

f = open("myfile.txt", "x")

Hi Friends,

Hope this post (Python Tutorial) will help you to learn something, If you enjoy my work then please share my posts with your buddies and anyone who might be interested in programming and don’t forget to subscribe.

If you had any problem with this post, please do mention it in the comment section.

Like ctechf Facebook fan page : https://www.facebook.com/ctechf

Also Read :

Sources : 
https://pixabay.com/ (images)
https://docs.python.org/3.3/ (Details)

Liked it? Take a second to support CtechF on Patreon!

Leave a Reply

59 Shares
Share
Tweet
Share
Pin
Share
%d bloggers like this: