0% found this document useful (0 votes)
10 views

python_notes_DS_IanDomel

A Python compiler translates high-level programming languages into machine code, enabling execution by computers. The document outlines various types of Python compilers and discusses Python's built-in data types, including integers, floats, strings, and functions, emphasizing their importance in programming. Additionally, it explains the advantages of functions, differentiating between built-in and user-defined functions, and provides examples of their usage.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

python_notes_DS_IanDomel

A Python compiler translates high-level programming languages into machine code, enabling execution by computers. The document outlines various types of Python compilers and discusses Python's built-in data types, including integers, floats, strings, and functions, emphasizing their importance in programming. Additionally, it explains the advantages of functions, differentiating between built-in and user-defined functions, and provides examples of their usage.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

What is Python Compiler?

A compiler is a program that translates a high-level language into a low-level language


that assembly can understand and interpret into logical input.
It also represents, the special program that converts the source code of a programming
language into machine code, bytecode, or another programming language. Source code is
typically written in a human-readable, high-level language such as Java or C++.

Types of Python Compilers?

Figure 1.0 – Latest and Various Compilers as of Today


1. Programiz
2. PyDev
3. PyCharm
4. Sublime Text
5. Thonny
6. Visual Studio Code
7. Jupyter Notebook
8. Vim
9. Atom
10. Spyder
11. IDLE
12. Eric
13. GNU/Emacs

Writing Pane on
Python Codes

Output/Result or
Comments/Error Pane

Figure 1.1 – Thonny Python Compiler ver. 4.0.1


Python Data Types
Data types are kinds of input data accepted by programming languages to define,
declare, store and execute math and logical values/operations. In Python uses a number of data
types to handle common program developer operations on input data. Commonly used data
types include numbers for numbers, strings for single or character ranges, tuples for
combinations of different data types, and lists for collections of values.

Python is an object oriented so its data types are classes. In Python, the data type is
set when you assign a value to a variable. Some of the commonly used data types in
Python are:

1. Integer (int): It can store positive or negative whole numbers (without fraction or
decimal). In Python there is no limit to how long an integer value can be. For example:

counter=10000
print(counter)
goals=3
print(goals)

2. Float (float): It can store realnumbers. It is specified by a decimal point. For example:

cgpa=2.95
print(cgpa)

3. Complex Number: It is specified as (real part) + (imaginary part)j. For example:

z = 2 + 4j
print(z)

4. String: strings are arrays of bytes representing Unicode characters. A string is a


collection of one or more characters put in a single quote, double-quote or triple
quote. Strings can be in multiple lines as well. For example:

str='welcome to new engineering'


print(str)

str1="welcome to python"
print(str1)

str2='''welcome to
Python'''
print(str2)
Standard/Common Python Built-In Data Types:

Descriptive/Categories Python default Examples


function
Text Type Str x = “This Python Programming”
print(x)

Numeric Types int, float, complex x=5 x = 3j


print(int(x)) print(complex(x))
x= 5.10
print(float(x))
Sequence Types list, tuple, range x = ["soda", "milk", "juice"]
print(list(x))
x = ("soda", "milk", "juice")
print(tuple(x))
x= range(10, 15)
print(x)

Mapping dict person = dict({"name": "Mohammed Hamed",


"country": "Oman", "telephone": 98123456})
print(person)
Set Type set, frozenset food_set = set(("Mutton Biryani", "Falafel and
Masalah Chicken", "Fish Biryani"))
print(food_set)

Boolean Type bool x = 25


y = 20
z=x>y
print(z) #true or false
Table 1.1
Examples:
1.

Figure 1.2 – Using Input, Process, Output in Python Programming Data Types

2.

Figure 1.3 – Using str data type for Output and New line.
Figure 1.3 – Using Conditional Operator in Python Programming

Functions in Python
A function in Python is a set of related statements that carry out a particular action.
In the program it is broken up into smaller, modular chunks thanks to functions. Functions
help the program stay organized and manageable as it gets bigger and bigger.
Additionally, it keeps the code from being repeated and makes it usable.

Advantages of using Functions:

1. The program can be divided into small parts that are easy to understand, debug
and modify.
2. Repetition of code can be avoided. A function can be written once and called
many times.
3. Functions are reusable. A function can be used in many programs.
Types of Functions:
In python language functions are of two types:
1. Built-in (Library) functions.
2. User Defined Functions

Built-In (Library) Math Functions:

In python there are large number of built-in functions that can be used to
perform different operations like math operations and operations on strings, lists
and files.
In this chapter a number of built-in math library functions will be introduced.
In Python programs a number of mathematical operations can be
performed with ease by importing a module named “math” which defines various
functions which makes our programming tasks easier such as sqrt(x). Below are
some of these built-in math functions.

Function Description

pow(x, y) Returns x raised to the power y

math.sqrt(x) Returns the square root value of x. x must be a


positive number.
math.pi Returns the value of the constant π= 3.1415

min() Returns the lowest value among some numbers.

max() Returns the highest value among some numbers.

abs() Returns the absolute (positive) value of a number.


ceil() It rounds a number upwards to its nearest integer.

floor() It rounds a number downwards to its nearest


integer.
Table 1.1 – Common Math Built-in Functions

Example 1:
import math This is the library to be included math functions
print("3 power 2 is=", pow(3,2))
print("Square root of 5 is=", math.sqrt(5))
x=min(10,23,2,8)
print("Minimum is=",x)
y=max(10,23,2,8)
print("Maximum is=",y)
z = abs(-7.25)
print("Absolute value is=",z)

Output:
3 power 2 is= 9.0
Square root of 5 is= 2.23606797749979
Minimum is= 2
Maximum is= 23
Absolute value is= 7.25

Example 2: Rounding off decimals


import math
num1 = math.ceil(3.6)
num2 = math.floor(3.5)
print(num1)
print(num2)
Output:
4
3

User Defined Functions:

These functions are written by programmers for their own usage in their programs.
Writing a user defined function consists of two steps:
1. Defining the function.
2. Calling the function.

Syntax of defining a function in Python:

def function_name (parameter_1, parameter_2,...... parameter_n):


# body of function
return

Definition of a function must be before using (calling) the function and it consists of the
following components:
1.Keyword def that marks the start of the function header.
2.A function_name to uniquely identify the function. Function name is given by the
programmer and the naming follows the same rules of writing identifiers in Python.
3.Parameters (arguments) through which we pass values to a function. They are
optional.
4.A colon (:) to mark the end of the function header.
5.One or more valid python statements that make up the function body. Statements must
have the same indentation level (usually 4 spaces).
6.An optional return statement to return a value from the function.
The statement return [expression] exits a function and it optionally passing back the
value of an expression to the caller. A return statement with no arguments is the same as
return none.
Syntax of Calling a Function:

function_name (parameter_1, parameter_2, parameter_n)


Example:
def multiplication():
n1=eval(input("Enter first number: "))
n2=eval(input("Enter second number: "))
print("Their multiplication= ", n1*n2)
return
multiplication ()

Program without Function User Defined Function


n1 = int(input(“Enter first number: “)) def numb_add(n1, n2):
sum = n1 + n2
n2 = int(input(“Enter second number:
return sum
“))
num1 = int(input("Enter the first number: "))
sum = n1 + n2 num2 = int(input("Enter the second number:
print("Sum of two numbers is: ", sum) "))
print("Sum of two numbers is: ", sum)

You might also like