Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Assignment

Uploaded by

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

Assignment

Uploaded by

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

Assignment – 1

Task – 2
Python is a high-level scripting language which can be used for a wide variety of text
processing, system administration and internet-related tasks. Unlike many similar languages,
it’s core language is very small and easy to mas ter, while allowing the addition of modules to
perform a virtually limitless variety of tasks. Python is a true object-oriented language, and is
available on a wide variety of platforms. There’s even a python interpreter written entirely in
Java, further enhancing python’s position as an excellent solution for internet-based problems.
Python was developed in the early 1990’s by Guido van Rossum, then at CWI in Amsterdam,
and currently at CNRI in Virginia. In some ways, python grew out of a project to design a
computer language which would be easy for beginners to learn, yet would be powerful enough
for even advanced users. This heritage is reflected in python’s small, clean syntax and the thor
oughness of the implementation of ideas like object-oriented programming, without
eliminating the ability to program in a more traditional style. So python is an excellent choice
as a first programming language without sacri f icing the power and advanced capabilities that
users will eventually need. Although pictures of snakes often appear on python books and
websites, the name is derived from Guido van Rossum’s favorite TV show, “Monty Python’s
Flying Circus”. For this reason, lots of online and print documen tation for the language has a
light and humorous touch. Interestingly, many experienced programmers report that python has
brought back a lot of the fun they used to have programming, so van Rossum’s inspiration may
be well expressed in the language itself.

Function
In Pyt;hon, **functions** and **modules** are fundamental building blocks for organizing
code into manageable and reusable components.
### Functions
A **function** in Python is a block of reusable code that performs a specific task. Functions
help in modularizing and reducing code repetition.
#### Creating a Function
A function is defined using the `def` keyword, followed by the function name and parentheses.
Here's a simple example:
```python
def greet(name):
print(f"Hello, {name}!")
```
#### Calling a Function
To call the function, you simply use its name followed by parentheses, passing any required
arguments:
```python
greet("Alice")
# Output: Hello, Alice!
```
#### Parameters and Arguments
- **Parameters** are variables defined in the function signature (e.g., `name` in the example
above).
- **Arguments** are the actual values passed when calling the function (e.g., `"Alice"` in the
example).
#### Return Statement
Functions can return values using the `return` statement:
```python
def add(a, b):
return a + b
result = add(5, 3)
print(result) # Output: 8
```

Modules:
A **module** is a file containing Python code (functions, classes, variables, etc.) that can be
imported and used in other programs. Python has a rich standard library of modules, and you
can also create your own.
#### Importing a Module
Modules are imported using the `import` statement. You can import built-in modules, third-
party modules, or your own.

```python
import math

print(math.sqrt(16)) # Output: 4.0


```
#### Importing Specific Functions from a Module
Instead of importing the whole module, you can import specific functions:
```python
from math import sqrt
print(sqrt(25)) # Output: 5.0
```
#### Creating a Module
To create your own module, simply save a Python file (e.g., `mymodule.py`) with the functions
and variables you want to include:
```python
# mymodule.py
def say_hello():
print("Hello from my module!")
```
Then, you can import and use this module in another script:
```python
import mymodule
mymodule.say_hello() # Output: Hello from my module!
```### Built-in Python Modules
Some popular built-in modules include:
- **`os`**: Provides functions for interacting with the operating system.
- **`sys`**: Used to manipulate Python runtime environment.
- **`math`**: Provides mathematical functions like square root, trigonometry, etc.
- **`random`**: Contains functions to generate random numbers.
- **`datetime`**: For working with dates and times.
### Example Using Custom Function and Module
#### mymodule.py
```python
# File: mymodule.py
def welcome_message(name):
return f"Welcome, {name}!"
```
#### main.py
```python
# File: main.py
import mymodule
message = mymodule.welcome_message("John")
print(message) # Output: Welcome, John!
This helps you organize code and maintain a clean, modular structure.

You might also like