0% found this document useful (0 votes)
5 views4 pages

June 7

The document explains number systems in Python, detailing how decimal, binary, octal, and hexadecimal values are represented and converted using built-in functions like bin(), oct(), and hex(). It also discusses the concept of functions, their necessity in programming, and the difference between functions and methods, along with examples of user-defined functions. Additionally, it outlines the structure of functions, their parameters, return values, and various ways to define them.

Uploaded by

pp14221422
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

June 7

The document explains number systems in Python, detailing how decimal, binary, octal, and hexadecimal values are represented and converted using built-in functions like bin(), oct(), and hex(). It also discusses the concept of functions, their necessity in programming, and the difference between functions and methods, along with examples of user-defined functions. Additionally, it outlines the structure of functions, their parameters, return values, and various ways to define them.

Uploaded by

pp14221422
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Number Systems in Python:

=> Any number is by default treated as a decimal value


Eg: 100, -125, 0

=> Any number which is preceded by "0b" is treated as a binary value


Eg: 0b10101100, 0b10010110

=> Any number which is preceded by "0o" is treated as an octal value


Eg: 0o725, 0o123, ...

=> Any number which is preceded by "0x" or "0X" is treated as a hexadecimal value
Eg: 0x12cd, 0X12CD...

Number System conversion functions in Python:


=> Any number is by default represented as a decimal value 18
=> To convert a value of any number system into its equalent binary value, bin()
function is used.
=> To convert a value of any number system into its equalent octal value, oct()
function is used.
=> To convert a value of any number system into its equalent hexa-decimal value,
hex() function is used.
Eg:
# Number Systems in Python

a = 100 # decimal
b = 0b100 # binary
c = 0o100 # octal
d = 0X100 # hexa-decimal
------------------------------------------------
# Number Systems in Python

a = 100 # decimal
b = 0b100 # binary
c = 0o100 # octal
d = 0X100 # hexa-decimal

print("value of a in Decimal number system : ",a)


print("value of b in Decimal number system : ",b)
print("value of c in Decimal number system : ",c)
print("value of d in Decimal number system : ",d)

print("value of a in binary number system : ",bin(a))


print("value of b in binary number system : ",bin(b))
print("value of c in binary number system : ",bin(c))
print("value of d in binary number system : ",bin(d))

print("value of a in octal number system : ",oct(a))


print("value of b in octal number system : ",oct(b))
print("value of c in octal number system : ",oct(c))
print("value of d in octal number system : ",oct(d))

print("value of a in Hexa Decimal number system : ",hex(a))


print("value of b in Hexa Decimal number system : ",hex(b))
print("value of c in Hexa Decimal number system : ",hex(c))
print("value of d in Hexa Decimal number system : ",hex(d))

===================================================================================
======
Object:
== Properties(data)
== Behavior(methods)
Eg:
Employee:Object
== Properties => emp_id,emp_name,salary....
== Behavior => read(), print(), update() ....
Eg:
Account:Object
== Properties => acc_id, acc_type, balance....
== Behavior => create(), withdraw(), showBalance() ....
-------------------------------------------
Functions vs Methods:
Functions:
== Not a member of any object
== Call be called directly
Eg: print(), int(), eval(), input() ....

Methods:
== Member of some object
== Can be called only through objects
Eg: str => lower(), upper(), title()....
list => append(), extend(), ....
------------------------------------------
Functions in Python :
=====================
=>A function is a self-contained block of statements designed to perform a specific
task. They are also called "sub-programs".

Need for Functions :


-> In many programs,we need to execute the same set of statements in several parts
of a program. Writing the statements again and again will have the following
limitations:

1) It increases the programming time.


2) The program consumes more memory space.
3) The chances of errors will increase.

-> To overcome the above limitations,we can use functions. Using functions, the
statements to be executed are written only once, but can be "called and executed"
wherever required.

-> Functions can be classified into two types:


Built-In functions and User-defined functions.

Steps for creating a user-defined function:


syntax:
======
def function-name(paramters,if any):
=======
=======
return

=> in order to execute the function, we need to call the function


syntax:
function-name(parameters, if any)

NOTE :
1) Parameters are the values that are passed as an input to a function. They are
also called "Arguments".
2) A function can accept any no. of parameters,but can return only one value at a
time.
3) If a function does not accept any parameters,we can keep the braces empty

Functions can be declared in 4 ways:


-> Functions with Parameters and with return value
-> Functions with parameters and without return value
-> Functions without parameters and with return value
-> Functions without parameters and without return value
----------------------------------------------------
What happens actually when a function is called(Invoked)?
-> Whenever a function is called, control transfers from calling function to called
function,executes the statements and after execution returns to the calling
function and completes the remaining statements of it.
----------------------------------------------------------------

# creating a function with parameters and with return value

def add(a,b):
c = a + b
return c

#add(100,200) #no output


#print("sum of 10,20 : ",add(10,20))
z = add(20,40) # function call
print("sum of 20,40 : ",z)
----------------------------------------------------------------
# creating a function with parameters and without return value

def add(a,b):
c = a + b
print("Additon : ",c)

add(10,20)
add(100,200)
add(12.5,22.5)
--------------------------------------
# creating a function without parameters and with return value

def add():
a = int(input("Enter first value : "))
b = int(input("Enter second value : "))
c = a + b
return c

z = add()
print("sum : ",z)
----------------------------------------
# creating a function without parameters and without return value

def add():
a = int(input("Enter first value : "))
b = int(input("Enter second value : "))
c = a + b
print("Sum : ",c)

add()
------------------------------------------
# creating a function without parameters and without return value

def display():
print("I am from Display Function")

display()
display()
------------------------------------------------

You might also like