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

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.

ความคิดเห็น

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

OOP W

python object oriented  Student Class Attributes Student ID First name Last name Credits GPA Behaviors Enroll() Take_examination() class Student:     def __init__(self, student_id, first_name, last_name):         self.student_id = student_id         self.first_name = first_name         self.last_name = last_name         self.credits = 0         self.GPA = 0.0     def enroll(self, credits):         self.credits += credits     def take_examination(self, grade_points):         # Perform examination logic and calculate GPA based on grades         # For simplicity, let's assume grade_points are added to GPA directly         total_points = self.GPA * self.credits         self.credits += 1  # Assuming 1 credit for the examination         ...

OOP 22023

 https://github.com/omiejung01/PythonOO2_2023/tree/master01 https://github.com/omiejung01/PythonOO2_2023/tree/master02 Master 1 PythonOO2_2023/walletapp/data.py class Account:     def __init__(self,account_name,opening_balance):         self.account_name = account_name         self.balance = opening_balance     #Constructor     def display(self):         # Balance Inquiry         return self.account_name + ' ' + f"{self.balance:,.2f}"     def setAccountName(self, new_name):         self.account_name = new_name class Character:     def __init__(self, character_name, vision, weapon_type):         self.__character_name = character_name         self.__vision = vision         self.__weapon = None         self.__weapon_type = weapon_type     def displa...

Web Class note

https://github.com/omiejung01/Nuxt3WD2_2023/tree/master02 https://docs.google.com/document/d/1qikYS-WSpRosjIlUZEoOhzguigWx2-Mqamfyxcs67aE/edit Web Design https://drive.google.com/drive/folders/1kJvORymDwvcDtGoliUOHifsza3CdWsmH DBM Course https://drive.google.com/drive/folders/119ZWerKRLYNwf7G7dEA7ZKcBx9rOQoqV Web Design - Class note 13 Nov 2023 Course Syllabus Please go to https://tinyurl.com/webdesign002 And go to slides at  https://tinyurl.com/dbmcourse001 Slide: 01-How computer works Slide: 02-Programming Languages Slide #11: Try "Create game using PHP" Change PHP to C++ Change PHP to Unity Let's student try some queries <Solution> + <Languages Tools> Answer in “Student Interactive Board” Google Doc for your ATTN Todo: Check Visual Studio Code in your machine Goto Search Box type → VS Code Create new file Save file as hello.html Hello World in HTML Hello World in JavaScript Next Class - Slide: 03-Coding 101 Class: 20 Nov 2023 Slide: 03-Coding 101 Slide #19 ...