ข้ามไปที่เนื้อหาหลัก

OOPT01

 Lists:

Lists are ordered collections that can contain elements of different types. They are mutable, meaning you can change their elements.


Example:


python

Copy code

my_list = [1, 2, "apple", True]

my_list.append(5)  # Adding an element

print(my_list[2])  # Accessing an element by index

my_list.remove("apple")  # Removing an element

print(my_list)  # Printing the modified list


Dictionary:

Dictionaries are unordered collections of key-value pairs. They use keys to access their associated values and are mutable.


Example:


python

Copy code

my_dict = {"name": "Alice", "age": 25, "city": "New York"}

print(my_dict["age"])  # Accessing value by key

my_dict["occupation"] = "Engineer"  # Adding a new key-value pair

del my_dict["city"]  # Deleting a key-value pair

print(my_dict)  # Printing the modified dictionary


Ranges:

Ranges are sequences of numbers used for iterating over a sequence of numbers. They are immutable and commonly used in loops.


Example:


python

Copy code

my_range = range(5)  # Creates a range from 0 to 4

for num in my_range:

    print(num)  # Prints numbers from the range


Functions:

Functions in Python are blocks of code that perform a specific task. They can take parameters and return values.


Example:


python

Copy code

def add_numbers(a, b):

    return a + b  # Returns the sum of two numbers


result = add_numbers(3, 5)  # Calling the function

print(result)  # Printing the result

Python Built-in Functions:

Python has many built-in functions that are readily available for use without importing any modules. Examples include print(), len(), min(), max(), sum(), sorted(), enumerate(), etc.


Example:


python

Copy code

my_list = [3, 1, 7, 2, 5]

print(len(my_list))  # Prints the length of the list

print(sorted(my_list))  # Prints the sorted list

print(sum(my_list))  # Prints the sum of elements in the list

These concepts are fundamental in Python and form the backbone for many functionalities and data manipulation techniques.



----------------------------------------------------------------------------------------------------------


Lists:

Lists in Python are ordered collections of items, denoted by square brackets []. They can contain elements of different data types and are mutable.


python

Copy code

# Creating a list

my_list = [1, 2, 3, 'apple', 'banana', True]


# Accessing elements in a list

print(my_list[0])  # Output: 1

print(my_list[3])  # Output: 'apple'


# Modifying elements in a list

my_list[1] = 'orange'  # Changing the element at index 1

print(my_list)  # Output: [1, 'orange', 3, 'apple', 'banana', True]

Dictionary:

Dictionaries in Python are unordered collections of key-value pairs, enclosed in curly braces {}.


python

Copy code

# Creating a dictionary

my_dict = {'name': 'Alice', 'age': 25, 'city': 'New York'}


# Accessing values in a dictionary

print(my_dict['name'])  # Output: 'Alice'

print(my_dict.get('age'))  # Output: 25


# Modifying values in a dictionary

my_dict['city'] = 'San Francisco'  # Changing the value associated with the key 'city'

print(my_dict)  # Output: {'name': 'Alice', 'age': 25, 'city': 'San Francisco'}


Ranges:

range() in Python generates an immutable sequence of numbers within a specified range.


python

Copy code

# Creating a range

my_range = range(5)  # Generates numbers from 0 to 4

# Accessing elements in a range

print(list(my_range))  # Output: [0, 1, 2, 3, 4]


Functions:

Functions in Python are blocks of reusable code that perform a specific task. They are defined using the def keyword.


python

Copy code

# Function definition

def greet(name):

    return f"Hello, {name}!"

# Function call

print(greet('Alice'))  # Output: 'Hello, Alice!'



Python Built-in Functions:

Python provides numerous built-in functions to perform various operations. Some examples include:


len(): Returns the length of an object (e.g., list, string, dictionary).

max(): Returns the maximum value in an iterable.

min(): Returns the minimum value in an iterable.

sum(): Returns the sum of all elements in an iterable.

sorted(): Returns a new sorted list from the elements of an iterable.

python

Copy code

my_list = [3, 1, 7, 5, 2]


print(len(my_list))  # Output: 5

print(max(my_list))  # Output: 7

print(min(my_list))  # Output: 1

print(sum(my_list))  # Output: 18

print(sorted(my_list))  # Output: [1, 2, 3, 5, 7]

These fundamental elements and built-in functions are core components in Python programming, allowing you to manipulate data, create reusable code with functions, and utilize a wide range of built-in functionalities to streamline your programs.

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

Object Final

 Final Examination OBJECT You will be given the "project name" and django project name. Inheritance 1 question Polymorphism 1 question (main.py) Django database 1 question Rest API → List items 1 question Rest API → Item detail 1 question Rest API → Transaction 1 question (Wallet App) ----------------------------------------------------------------------------------------------------------------------------- pip install django django-admin startproject mysite_project py manage.py startapp catalog_app  Create the database models in terminal py manage.py makemigrations py manage.py migrate py manage.py createsuperuser Rest API pip install djangorestframework pip install markdown       pip install django-filter pip install virtualenv --------------------------------------------------------------------------------------------------------------------------- พยายามติดตั้ง  py manage.py makemigrations py manage.py migrate ทุกครั้งหลังจาก เปลี่ยนรูปแบบโค้ด ตัว...

OPPTH

 https://github.com/kongruksiamza/python-oop