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.
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 ...
Comments
Post a Comment