Number System:
Binary Number System
Octal Number System
Decimal Number System
Hexadecimal Number System
Binary Number System
- represents by 0 or 1.
- base or radix is 2.
- If we wanted to store a number in a python variable, that number should sharts with 0b OR 0B.
x = ob0110
print(x)
x = oB0110
print(x)
2. Octal System
- represents by 0 to 7.
- base or radix is 8.
- If we wanted to store a number in a python variable, that number should sharts with 0o OR 0O.
x = 0o0234
print(x)
x = 0O0110
print(x)
3. Decimal System
- represents by 0 to 9.
- base or radix is 10.
x = 578
print(x)
4. Hexadecimal System
- represents by 0 to 9 and a to f OR A to F .
- base or radix is 16.
- If we wanted to store a number in a python variable, that number should sharts with 0x OR 0X.
x = 0xA56
print(x)
x = 0xa56
print(x)
Input/Output in Python
- There are built in functions to perform input and output operation.
#Input = input()
#Output = print()
a = 30
str = “Hello” #This is the static assignment but when we want to assign a dynamic assignment it means runtime.
name = input("Enter the Name")
age = input("Enter the age")
place = input("Enter the place")
print("Name is",name,"\n","Age is",age,"\n","Place is",place)
print() method Syntax:
print(obj*,sep=’ ’,end=’\n’)
Syntax of the print() function
print(obj*, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
print() Parameters
objects - object to the printed. * indicates that there may be more than one object
sep - objects are separated by sep. Default value: ' '
end - end is printed at last. Default value: '\n'
file - here you can mention the file in which you would like to write or append the output of the print function. By default is sys.stdout will be used which prints objects on the screen.
flush - If True, the stream is forcibly flushed. Default value: False
#print with objects
print("C","C++","Java","Swift","Python")
#print with objectsAndSep
print("C","C++","Java","Swift","Python",sep="\n")
#print with end
str1 = "We are going"
str2 = "to learn Python"
print(str1); print(str2)
print(str1, end=" "); print(str2)
#print with file
file = open('print.txt','a+') #a,+ are the mode
print(str1,str2,file=file)
file.close()
import time
print('Please enter your email-id : ', end=' ')
#print('Please enter your email-id : ', end=' ', flush=True) #run this to see the difference and comment out the above line of code.
time.sleep(5)
'''If you run the above lines of code in the terminal, you will notice that the prompt string does not show up until the sleep timer ends and the program exits. However, if you add flush=True in the print function, you will see the prompt, and then you will have to wait for 5 seconds for the program to finish.'''
format():
a = "javascript"
b = "java"
print(a,"is a scripting language",b,"is Object oriented programming language")
print("{0} is a scripting language.{1} is a Object oriented programming language".format(a,b))
F string:
f"x = {x}, y = {y} "
Python Modules
help(“modules”)
- Module is simply a file that contains variables,functions and Classes that we can use in any other files using import.
- There are two types of modules
1. User Defined Module
Build In Module
1. User Defined Module
Create one .py file. ex. mymodule.py and write below code
def sayHelloTo(name):
print(“Hello”,name)
Create second .py file. ex. moduleexample.py and write below code
import mymodule
print(mymodule.sayHelloTo(“Chaitanya”))
Run code in terminal:
python3 moduleexample.py
o/p: Hello Chaitanya
Whenever we run the file if you check our file location there is one folder created with name __pycache__.
What is __pycache__?
When you run a program in python, the interpreter compiles it to bytecode first (this is an oversimplification) and stores it in the __pycache__ folder. If you look in there you will find a bunch of files sharing the names of the .py files in your project's folder, only their extensions will be either .pyc or .pyo. These are bytecode-compiled and optimized bytecode-compiled versions of your program's files, respectively.
As a programmer, you can largely just ignore it... All it does is make your program start a little faster. When your scripts change, they will be recompiled, and if you delete the files or the whole folder and run your program again, they will reappear (unless you specifically suppress that behavior)
you can suppress it by starting the interpreter with the -B flag, for example
python3 -B moduleexample.py
Access Variable from module
a. Create a dictionary in mymodule.py.
deatilInfo = {"name":"Chaitanya","age":"27"}
b. Access dictionary in other file moduleexample.py like below
print("Your Age is:",mymodule.deatilInfo["age"])
Alias Module
We are able to change the name of module in other file using as keyword at the time of import the module. See below
import mymodule as my
print("Your Age is:",my.deatilInfo["age"])
2. Built-in Modules
There are several build in modules available in python.
import platform
print(help("modules"))
print(platform.system()) # print darwin for mac Because the core of Mac OS X is the Darwin OS. try uname in terminal
using from .. import :
We have an option to import only some parts from a module, by using the from keyword.
from math import sqrt,
print(sqrt(25))
using dir() function.
dir() function to know the names and attributes of a module.
print(dir(mymodule))