0% menganggap dokumen ini bermanfaat (0 suara)
22 tayangan

Python Has A Set of Built

List dan array merupakan struktur data yang sering digunakan untuk menyimpan koleksi item. List bersifat fleksibel karena dapat menyimpan berbagai tipe data secara bersamaan, sementara array hanya dapat menyimpan satu tipe data. List lebih mudah dioperasikan secara langsung sedangkan array membutuhkan modul tambahan. Keduanya memiliki kelebihan tergantung pada jenis dan kompleksitas data yang disimpan.

Diunggah oleh

nathanalvino080
Hak Cipta
© © All Rights Reserved
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai DOCX, PDF, TXT atau baca online di Scribd
0% menganggap dokumen ini bermanfaat (0 suara)
22 tayangan

Python Has A Set of Built

List dan array merupakan struktur data yang sering digunakan untuk menyimpan koleksi item. List bersifat fleksibel karena dapat menyimpan berbagai tipe data secara bersamaan, sementara array hanya dapat menyimpan satu tipe data. List lebih mudah dioperasikan secara langsung sedangkan array membutuhkan modul tambahan. Keduanya memiliki kelebihan tergantung pada jenis dan kompleksitas data yang disimpan.

Diunggah oleh

nathanalvino080
Hak Cipta
© © All Rights Reserved
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai DOCX, PDF, TXT atau baca online di Scribd
Anda di halaman 1/ 7

Python has a set of built-in methods that you can use on lists/arrays.

Method Description

append() Adds an element at the end of the list

clear() Removes all the elements from the list

copy() Returns a copy of the list

count() Returns the number of elements with the specified value

extend() Add the elements of a list (or any iterable), to the end of the current list

index() Returns the index of the first element with the specified value

insert() Adds an element at the specified position

pop() Removes the element at the specified position

remove() Removes the first item with the specified value

reverse() Reverses the order of the list

sort() Sorts the list

Array merupakan struktur data yang dapat menyimpan berbagai macam item yang bersifat
tidak unik, yang artinya boleh terdapat dua data yang sama di dalam satu array. Array
sebenarnya menjadi sangat mirip dengan list karena keduanya sama sama bisa bisa diurutkan,
kemudian juga bisa diubah isinya, dan diapit oleh dua kurung siku ( [ ] ). Sayangnya untuk
menyimpan berbagai tipe data yang berbeda tidak semudah yang dibayangkan, karena
tergantung dari jenis array yang digunakan.
Modul array digunakan untuk data yang memiliki tipe data yang sama, dan kita juga perlu
mendefinisikan tipe datanya. Sedangkan array yang berada di package numpy
memungkinkan kita menginputkan beberapa jenis tipe data dalam dalam satu array
Sama seperti Array Python, list merupakan struktur data yang dibangun ke dalam Python
untuk menyimpan sekumpulan item yang diapit oleh dua kurung siku. List bersifat mutable
atau dapat diubah, dimana kita dapat menambah atau menghapus elemennya. List juga dapat
diurutkan sehingga kita dapat menggunakan indeks ketika ingin merujuk ke elemen tertentu.
Elemen dari list tidak harus bersifat unik, yang artinya diperbolehkan terdapat duplikasi, serta
dapat terdiri dari tipe data yang berbeda dalam satu list, kita bisa menggabungkan string,
integer, dan objek dalam list yang sama.

# creating a list containing elements


# belonging to different data types
sample_list = [1, "Yash", ['a', 'e']]
print(type(sample_list))
print(sample_list)

# importing "array" for array creations


import array as arr

# creating an array with integer type


a = arr.array('i', [1, 2, 3])
print(type(a))
for i in a:
print(i, end=" ")

List Array

Can consist of elements belonging to Only consists of elements belonging to the


different data types same data type

No need to explicitly import a module for Need to explicitly import the array module
the declaration for declaration

Cannot directly handle arithmetic


Can directly handle arithmetic operations
operations

Preferred for a shorter sequence of data


Preferred for a longer sequence of data items
items

Greater flexibility allows easy Less flexibility since addition, and deletion
List Array

modification (addition, deletion) of data has to be done element-wise

The entire list can be printed without any A loop has to be formed to print or access
explicit looping the components of the array

Consume larger memory for easy addition Comparatively more compact in memory
of elements size

Nested lists can be of variable size Nested arrays has to be of same size.

Can perform direct operations using


functions like:
count() – for counting a particular
element in the list
sort() – sort the complete list
max() – gives maximum of the list
min() – gives minimum of the list
sum() – gives sum of all the elements in
list for integer list Need to import proper modules to perform
index() – gives first index of the element these operations.
specified
append() – adds the element to the end of
the list
remove() – removes the element specified
No need to import anything to use these
functions.
and many more…

Example:
Example:
import array
my_list = [1, 2, 3, 4]
arr = array.array(‘i’, [1, 2, 3])

Argument: a value that is passed into a function


Procedure: a pre-defined data structure that can be called upon at any point in a program. It
can take arguments but does not return a value.
Function: a pre-defined data structure that can be called upon at any point in a program. It
can take parameters and does return a value.
def my_function(**kid):
print("His last name is " + kid["lname"])

my_function(fname = "Tobias", lname = "Refsnes")

def my_function(food):
for x in food:
print(x)

fruits = ["apple", "banana", "cherry"]

my_function(fruits)
def my_function(x):
return 5 * x

function(argument){
switch(argument) {
case 0:
return "This is Case Zero";
case 1:
return " This is Case One";
case 2:
return " This is Case Two ";
default:
return "nothing";
};
};

#Switch Statement
def SwitchExample(argument):
switcher = {
0: " This is Case Zero ",
1: " This is Case One ",
2: " This is Case Two ",
}
return switcher.get(argument, "nothing")
if __name__ == "__main__":
argument = 1
print SwitchExample(argument)

count = 0
while (count < 3):
count = count + 1
print("Hello Geek")
else:
print("In Else Block")
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)

mycursor = mydb.cursor()

mycursor.execute("SHOW DATABASES")

for x in mycursor:
print(x)
CREATE TABLE Users(
name VARCHAR(128),
email VARCHAR(128)
)
INSERT INTO Users (name, email) VALUES ('Kristin', '[email protected]')

DELETE FROM Users WHERE email='[email protected]'


UPDATE Users SET name='Charles' WHERE email='[email protected]'
SELECT * FROM Users
SELECT * FROM Users WHERE email='[email protected]'
SELECT * FROM Users ORDER BY email
SELECT * FROM
Users ORDER BY
name DESC
select Track.title,
Artist.name,
Album.title,
Genre.name from
Track join Genre join
Album join Artist on Track.genre_id = Genre.id and Track.album_id = Album.id and
Album.artist_id = Artist.id
SELECT User.name, Member.role, Course.title
FROM User JOIN Member JOIN Course
ON Member.user_id = User.id AND
Member.course_id = Course.id
ORDER BY Course.title, Member.role DESC, User.name

Anda mungkin juga menyukai