Tuesday, December 20, 2016

Functions in Python - Practice


Two ways of importing modules in Python

In [8]:
import random
print(random.randint(1,10))
3
In [6]:
from random import *
print(randint(1,10))
8

We can import many modules in a same line

In [2]:
import random, sys, math

Early exit : Use sys.exit() to early exit the program

In [3]:
print("Hello")
sys.exit()
print("Good bye")
Hello
An exception has occurred, use %tb to see the full traceback.

SystemExit
To exit: use 'exit', 'quit', or Ctrl-D.
In [5]:
import pyperclip

Copy and paste using pyperclip module

In [7]:
pyperclip.copy("Hello Worldss")
print(pyperclip.paste())
Hello Worldss

Print returns NONE Type

Every function has a return value. If it doesn't return anything, then probably it's return type is NONE
In [9]:
print(type(print("Arun")))
Arun
<class 'NoneType'>

Keyword arguement in functions

In [10]:
print("Hello")
print("World")
Hello
World
In [14]:
print("Hello", end = " ")
print("World")
Hello World
In [15]:
print("apple","orange","mango")
apple orange mango
In [16]:
print("apple","orange","mango", sep = "")
appleorangemango
In [17]:
print("apple","orange","mango", sep = "-")
apple-orange-mango

No comments :

Post a Comment