Skip to main content

Built in function in Python with example

 Python provides a rich set of built-in functions that are readily available for use without requiring any import statements. These functions are part of the Python Standard Library and cover various aspects of programming. Let's look at some common built-in functions with examples:

1. `len()` - Returns the number of items in an object:

```python
fruits = ["apple", "banana", "orange", "grape"]
print(len(fruits))  # Output: 4

text = "Hello, World!"
print(len(text))  # Output: 13
```

2. `print()` - Prints the specified message to the console:

```python
print("Hello, World!")  # Output: Hello, World!
```

3. `range()` - Generates a sequence of numbers:

```python
# Generate numbers from 0 to 9
numbers = list(range(10))
print(numbers)  # Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# Generate numbers from 5 to 14 (exclusive)
numbers = list(range(5, 15))
print(numbers)  # Output: [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

# Generate even numbers from 0 to 10
even_numbers = list(range(0, 11, 2))
print(even_numbers)  # Output: [0, 2, 4, 6, 8, 10]
```

4. `input()` - Reads user input from the console:

```python
name = input("Enter your name: ")
print("Hello,", name)
```

5. `type()` - Returns the type of an object:

```python
number = 42
print(type(number))  # Output: <class 'int'>

fruits = ["apple", "banana", "orange"]
print(type(fruits))  # Output: <class 'list'>
```

6. `int()`, `float()`, `str()` - Convert data types:

```python
x = "10"
y = int(x)
print(y)  # Output: 10

z = float(y)
print(z)  # Output: 10.0

text = str(z)
print(text)  # Output: '10.0'
```

7. `sum()` - Returns the sum of elements in an iterable:

```python
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(total)  # Output: 15
```

These are just a few examples of the many built-in functions available in Python. The Python Standard Library offers a wide range of built-in functions to handle strings, numbers, lists, dictionaries, files, and more, making Python a powerful and convenient language for various programming tasks.

Comments

Popular posts from this blog

Tokens, Identifiers, Data Types, Sequence Control, Subprogram Control, Arrays, Structures, Union, String, Pointers, Functions, File Handling, Command Line Argumaents, Preprocessors in C with example

 Let's discuss each concept briefly and provide examples for better understanding: 1. Tokens: Tokens are the smallest building blocks in C programming. They include keywords, identifiers, constants, strings, operators, and punctuators. Example: ```c #include <stdio.h> int main() {     int num = 42;  // 'int', 'main', 'return', '42', '=', ';' are tokens     printf("Hello, World!");  // 'printf', '(', ')', 'Hello, World!', ';', are tokens     return 0;  // 'return', '0', ';' are tokens } ``` 2. Identifiers: Identifiers are names used to identify variables, functions, or other user-defined entities. Example: ```c int age = 30;  // 'age' is an identifier (variable name) void displayMessage() {  // 'displayMessage' is an identifier (function name)     // function body } ``` 3. Data Types: Data types define the type of data that can be stored in ...

Place holder and control character in c language

 In the C programming language, placeholders and control characters are used to format and control the output of text in console-based programs. They are special characters or sequences of characters that have specific meanings. Here are the placeholders and control characters commonly used in C: 1. Placeholders:    - %d: Used to display signed integers.      Example: printf("The value is %d", 10);    - %u: Used to display unsigned integers.      Example: printf("The value is %u", 10);    - %f: Used to display floating-point numbers.      Example: printf("The value is %f", 3.14);    - %c: Used to display characters.      Example: printf("The character is %c", 'A');    - %s: Used to display strings (sequence of characters).      Example: printf("The string is %s", "Hello");    - %p: Used to display memory addresses (pointers)...

what is requirements for web automation in python

 To perform web automation in Python, you'll need the following requirements: 1. Python: Make sure you have Python installed on your system. You can download the latest version from the official Python website (https://www.python.org/). 2. Web Driver: Web automation involves interacting with web browsers, so you'll need the appropriate web driver for the browser you want to automate. The most commonly used web drivers are:    - Chrome: ChromeDriver (https://sites.google.com/a/chromium.org/chromedriver/downloads)    - Firefox: GeckoDriver (https://github.com/mozilla/geckodriver/releases)    - Edge: Microsoft Edge WebDriver (https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/) 3. Selenium: Selenium is a popular Python library for automating web browsers. Install it using pip: ```bash pip install selenium ``` 4. Browser: Install the web browser you want to automate (e.g., Chrome, Firefox, Edge). 5. IDE or Text Editor: Choose a code edito...