- 70. Differentiate between a package and a module in python.
- 71. What are some of the most commonly used built-in modules in Python?
- 72. What are lambda functions?
- 73. How can you generate random numbers?
- 74. Can you easily check if all characters in the given string is alphanumeric?
- 75. What are the differences between pickling and unpickling?
- 76. Define GIL.
- 77. Define PYTHONPATH.
- 78. Define PIP.
- 79. Are there any tools for identifying bugs and performing static analysis in python?
- 80. Differentiate between deep and shallow copies.
- 81. What is main function in python? How do you invoke it?
- 82. Write python function which takes a variable number of arguments.
- 83. WAP [Write a program] which takes a sequence of numbers and check if all numbers are unique.
- 84. Write a program for counting the number of every character of a given text file.
- 85. Write a program to check and return the pairs of a given array A whose sum value is equal to a target value N.
- 86. Viết chương trình cộng hai số nguyên >0 mà không dùng toán tử cộng.
- 87. Viết chương trình giải phương trình đã cho với giả thiết a,b,c,m,n,o là các hằng số.
- 88. Write a Program to match a string that has the letter ‘a’ followed by 4 to 8 'b’s.
- 89. Viết chương trình chuyển đổi ngày từ định dạng yyyy-mm-dd sang định dạng dd-mm-yyyy.
- 90. Viết chương trình kết hợp hai từ điển khác nhau. Trong khi kết hợp, nếu bạn tìm thấy các khóa giống nhau, bạn có thể thêm các giá trị của các khóa giống nhau. Xuất từ điển mới
- 91. Bạn sẽ truy cập tập dữ liệu của bảng tính được chia sẻ công khai ở định dạng CSV được lưu trữ trong Google Drive bằng cách nào?
Giới thiệu về Python
Python được phát triển bởi Guido van Rossum và được phát hành lần đầu tiên vào ngày 20 tháng 2 năm 1991. Nó là một trong những ngôn ngữ lập trình được yêu thích và sử dụng rộng rãi nhất và được giải thích một cách tự nhiên do đó mang lại sự linh hoạt trong việc kết hợp ngữ nghĩa động. Nó cũng là một ngôn ngữ mã nguồn mở và miễn phí với cú pháp rất đơn giản và rõ ràng. Điều này giúp các nhà phát triển dễ dàng tìm hiểu python. Python cũng hỗ trợ lập trình hướng đối tượng và được sử dụng phổ biến nhất để thực hiện lập trình đa năng.
Do tính chất đơn giản và khả năng đạt được nhiều chức năng với ít dòng mã hơn, mức độ phổ biến của python đang tăng lên rất nhiều. Python cũng được sử dụng trong Học máy, Trí tuệ nhân tạo, Phát triển web, Quét web và nhiều lĩnh vực khác do khả năng hỗ trợ tính toán mạnh mẽ bằng các thư viện mạnh mẽ. Do đó, có nhu cầu rất lớn đối với các nhà phát triển python ở Ấn Độ và trên toàn thế giới. Các công ty sẵn sàng cung cấp các đặc quyền và lợi ích tuyệt vời cho các nhà phát triển này.
Trong bài viết này, chúng ta sẽ xem các câu hỏi và câu trả lời phỏng vấn về trăn thường gặp nhất sẽ giúp bạn trở nên xuất sắc và nhận được những lời mời làm việc tuyệt vời
Chúng tôi đã phân loại chúng thành các phần sau
- Câu hỏi phỏng vấn Python dành cho người mới
- Câu hỏi phỏng vấn Python dành cho người có kinh nghiệm
- Câu hỏi phỏng vấn Python OOPS
- Câu hỏi phỏng vấn Python Pandas
- Câu hỏi phỏng vấn NumPy
- Python Libraries Interview Questions
- Python Programming Examples
Bảng cheat Python. Khái niệm cơ bản đến nâng cao
Hoàn thành cuộc phỏng vấn công nghệ tiếp theo của bạn với sự tự tin
Tham gia một cuộc phỏng vấn giả miễn phí, nhận phản hồi và đề xuất ngay lập tức⚡️💡
Sự kiện . Được cung cấp bởi
- Nhà phát triển phần mềm
- Khoa học dữ liệu
- Tất cả sự kiện
- sự kiện của tôi
Không còn sự kiện nào để hiển thị
Xem tất cả
Xem tất cảKhông còn sự kiện nào để hiển thị
Xem tất cả
Xem tất cảKhông còn sự kiện nào để hiển thị
Xem tất cả
Xem tất cảKhông còn sự kiện nào để hiển thị
Xem tất cả
Xem tất cảCâu hỏi phỏng vấn Python dành cho người mới
1. Trăn là gì?
Python là ngôn ngữ lập trình đa năng, thông dịch, cấp cao. Là một ngôn ngữ có mục đích chung, nó có thể được sử dụng để xây dựng hầu hết mọi loại ứng dụng với các công cụ/thư viện phù hợp. Ngoài ra, python hỗ trợ các đối tượng, mô-đun, luồng, xử lý ngoại lệ và quản lý bộ nhớ tự động giúp mô hình hóa các vấn đề trong thế giới thực và xây dựng các ứng dụng để giải quyết các vấn đề này
Lợi ích của việc sử dụng Python
- Python là ngôn ngữ lập trình đa năng có cú pháp đơn giản, dễ học, nhấn mạnh khả năng đọc và do đó giảm chi phí bảo trì chương trình. Hơn nữa, ngôn ngữ này có khả năng viết kịch bản, hoàn toàn là nguồn mở và hỗ trợ các gói của bên thứ ba khuyến khích mô đun hóa và tái sử dụng mã
- Cấu trúc dữ liệu cấp cao của nó, kết hợp với kiểu gõ động và liên kết động, thu hút một cộng đồng lớn các nhà phát triển để triển khai và Phát triển ứng dụng nhanh
2. Ngôn ngữ gõ động là gì?
Trước khi hiểu một ngôn ngữ gõ động, chúng ta nên tìm hiểu về gõ là gì. Đánh máy đề cập đến kiểm tra loại trong ngôn ngữ lập trình. Trong một ngôn ngữ được gõ mạnh, chẳng hạn như Python, "1" + 2 sẽ dẫn đến lỗi loại vì các ngôn ngữ này không cho phép "ép buộc kiểu" [chuyển đổi kiểu dữ liệu ngầm]. Mặt khác, một ngôn ngữ gõ yếu, chẳng hạn như Javascript, sẽ chỉ xuất ra kết quả là "12"
Kiểm tra loại có thể được thực hiện ở hai giai đoạn -
- Tĩnh - Kiểu dữ liệu được kiểm tra trước khi thực hiện
- Động - Các kiểu dữ liệu được kiểm tra trong quá trình thực thi
Python là một ngôn ngữ được thông dịch, thực thi từng dòng câu lệnh theo từng dòng và do đó, việc kiểm tra kiểu được thực hiện nhanh chóng trong quá trình thực thi. Do đó, Python là một ngôn ngữ được gõ động
3. Ngôn ngữ thông dịch là gì?
Một ngôn ngữ được giải thích thực thi từng dòng câu lệnh của nó. Các ngôn ngữ như Python, Javascript, R, PHP và Ruby là những ví dụ điển hình của ngôn ngữ Thông dịch. Các chương trình được viết bằng ngôn ngữ thông dịch chạy trực tiếp từ mã nguồn, không có bước biên dịch trung gian
Bạn có thể tải xuống phiên bản PDF của Câu hỏi phỏng vấn Python
tải PDF
Tải xuống theo yêu cầu của bạn đã sẵn sàng.
Nhấp vào đây để tải xuống.
4. PEP 8 là gì và tại sao nó quan trọng?
PEP là viết tắt của Đề xuất cải tiến Python. PEP là một tài liệu thiết kế chính thức cung cấp thông tin cho cộng đồng Python hoặc mô tả một tính năng mới cho Python hoặc các quy trình của nó. PEP 8 đặc biệt quan trọng vì nó ghi lại các nguyên tắc về kiểu dáng cho Mã Python. Rõ ràng việc đóng góp cho cộng đồng mã nguồn mở Python yêu cầu bạn tuân theo các nguyên tắc về phong cách này một cách chân thành và nghiêm túc
5. Phạm vi trong Python là gì?
Mọi đối tượng trong Python hoạt động trong một phạm vi. Phạm vi là một khối mã trong đó một đối tượng trong Python vẫn có liên quan. Không gian tên xác định duy nhất tất cả các đối tượng bên trong một chương trình. Tuy nhiên, các không gian tên này cũng có một phạm vi được xác định cho chúng, nơi bạn có thể sử dụng các đối tượng của chúng mà không cần bất kỳ tiền tố nào. Một vài ví dụ về phạm vi được tạo trong quá trình thực thi mã trong Python như sau
- Phạm vi cục bộ đề cập đến các đối tượng cục bộ có sẵn trong chức năng hiện tại
- Phạm vi toàn cầu đề cập đến các đối tượng có sẵn trong suốt quá trình thực thi mã kể từ khi bắt đầu
- Phạm vi cấp mô-đun đề cập đến các đối tượng toàn cầu của mô-đun hiện tại có thể truy cập trong chương trình
- Phạm vi ngoài cùng đề cập đến tất cả các tên tích hợp có thể gọi được trong chương trình. Các đối tượng trong phạm vi này được tìm kiếm lần cuối để tìm tên được tham chiếu
Ghi chú. Các đối tượng phạm vi cục bộ có thể được đồng bộ hóa với các đối tượng phạm vi toàn cầu bằng cách sử dụng các từ khóa như toàn cầu
6. danh sách và bộ dữ liệu là gì?
Danh sách và Bộ dữ liệu đều là các kiểu dữ liệu chuỗi có thể lưu trữ một tập hợp các đối tượng trong Python. Các đối tượng được lưu trữ trong cả hai chuỗi có thể có các kiểu dữ liệu khác nhau. Danh sách được biểu diễn bằng dấu ngoặc vuông ________ 022, trong khi bộ dữ liệu được biểu diễn bằng dấu ngoặc đơn ________ 023.
Nhưng sự khác biệt thực sự giữa hai điều này là gì? . Điều này có nghĩa là các danh sách có thể được sửa đổi, nối thêm hoặc cắt khi đang di chuyển nhưng các bộ dữ liệu không đổi và không thể sửa đổi theo bất kỳ cách nào. Bạn có thể chạy ví dụ sau trên Python IDLE để xác nhận sự khác biệt.
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
7. Các kiểu dữ liệu tích hợp phổ biến trong Python là gì?
Có một số kiểu dữ liệu tích hợp trong Python. Mặc dù, Python không yêu cầu các loại dữ liệu phải được xác định rõ ràng trong quá trình khai báo biến, lỗi loại có thể xảy ra nếu kiến thức về các loại dữ liệu và khả năng tương thích của chúng với nhau bị bỏ qua. Python cung cấp các hàm
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
24 và def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
25 để kiểm tra kiểu của các biến này. Các loại dữ liệu này có thể được nhóm thành các loại sau-- Không có loại.
______026 từ khóa đại diện cho các giá trị null trong Python. Thao tác đẳng thức Boolean có thể được thực hiện bằng các đối tượng NoneType này.
- Kiểu số.
Có ba loại số riêng biệt - số nguyên, số dấu phẩy động và số phức. Ngoài ra, booleans là một kiểu con của số nguyên.
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
27 và def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
28boolLưu trữ giá trị boolean [Đúng hoặc Sai]Ghi chú. Thư viện tiêu chuẩn cũng bao gồm các phân số để lưu trữ các số hữu tỷ và số thập phân để lưu trữ các số dấu phẩy động với độ chính xác do người dùng xác định
- Các loại trình tự.
Theo Tài liệu Python, có ba Loại trình tự cơ bản - danh sách, bộ dữ liệu và đối tượng phạm vi. Các loại trình tự có các toán tử
29 vàdef myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
30 được xác định để duyệt qua các phần tử của chúng. Các toán tử này có cùng mức độ ưu tiên như các phép toán so sánh.def myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
Ghi chú. Thư viện chuẩn cũng bao gồm các loại bổ sung để xử lý.
1. Dữ liệu nhị phân như
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
31 def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
32 và2. Chuỗi văn bản chẳng hạn như
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
33. - Các loại ánh xạ
Một đối tượng ánh xạ có thể ánh xạ các giá trị có thể băm thành các đối tượng ngẫu nhiên trong Python. Các đối tượng ánh xạ có thể thay đổi và hiện tại chỉ có một loại ánh xạ tiêu chuẩn, từ điển
Tên lớp Mô tảdictLưu trữ danh sách khóa được phân tách bằng dấu phẩy. cặp giá trị- Đặt loại.
Hiện tại, Python có hai loại tập hợp tích hợp sẵn - tập hợp và tập hợp cố định. loại thiết lập có thể thay đổi và hỗ trợ các phương thức như
34 vàdef myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
35. loại freezeset là bất biến và không thể sửa đổi sau khi tạo.def myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
Ghi chú.
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
36 có thể thay đổi và do đó không thể được sử dụng làm khóa cho từ điển. Mặt khác, def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
37 là bất biến và do đó, có thể băm được và có thể được sử dụng làm khóa từ điển hoặc làm thành phần của tập hợp khác- Mô-đun.
Mô-đun là một loại tích hợp bổ sung được hỗ trợ bởi Trình thông dịch Python. Nó hỗ trợ một hoạt động đặc biệt, tôi. e. , truy cập thuộc tính.
38, trong đódef myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
39 là một mô-đun và myobj tham chiếu đến tên được xác định trong bảng ký hiệu của m. Bảng ký hiệu của mô-đun nằm trong một thuộc tính rất đặc biệt của mô-đun __dict__, nhưng việc gán trực tiếp cho mô-đun này là không thể và cũng không được khuyến nghị.def myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
- Các loại có thể gọi.
Các loại có thể gọi được là các loại có thể áp dụng lệnh gọi hàm. Chúng có thể là các hàm do người dùng định nghĩa, các phương thức cá thể, hàm tạo và một số hàm, phương thức và lớp tích hợp khác.
Tham khảo tài liệu tại docs. con trăn. org để xem chi tiết các loại có thể gọi được.
8. Vượt qua trong Python là gì?
The
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
40 keyword represents a null operation in Python. Nó thường được sử dụng cho mục đích lấp đầy các khối mã trống có thể thực thi trong thời gian chạy nhưng vẫn chưa được viết. Nếu không có câu lệnh pass trong đoạn mã sau, chúng ta có thể gặp một số lỗi trong quá trình thực thi mãdef myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
9. Các mô-đun và gói trong Python là gì?
Gói Python và mô-đun Python là hai cơ chế cho phép lập trình mô-đun trong Python. Mô-đun hóa có một số lợi thế -
- Sự đơn giản. Làm việc trên một mô-đun duy nhất giúp bạn tập trung vào một phần tương đối nhỏ của vấn đề hiện tại. Điều này làm cho việc phát triển dễ dàng hơn và ít bị lỗi hơn
- khả năng bảo trì. Các mô-đun được thiết kế để thực thi các ranh giới logic giữa các miền vấn đề khác nhau. Nếu chúng được viết theo cách làm giảm sự phụ thuộc lẫn nhau, thì ít có khả năng các sửa đổi trong một mô-đun có thể ảnh hưởng đến các phần khác của chương trình
- khả năng tái sử dụng. Các chức năng được xác định trong một mô-đun có thể dễ dàng được sử dụng lại bởi các phần khác của ứng dụng
- phạm vi. Các mô-đun thường xác định một không gian tên riêng biệt, giúp tránh nhầm lẫn giữa các mã định danh từ các phần khác của chương trình
Nói chung, các mô-đun chỉ đơn giản là các tệp Python với một. phần mở rộng py và có thể có một tập hợp các hàm, lớp hoặc biến được xác định và triển khai. Chúng có thể được nhập và khởi tạo một lần bằng cách sử dụng câu lệnh
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
41. Nếu cần một phần chức năng, hãy nhập các lớp hoặc hàm cần thiết bằng cách sử dụng def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
42Các gói cho phép cấu trúc phân cấp của không gian tên mô-đun bằng cách sử dụng ký hiệu dấu chấm. Vì, các mô-đun giúp tránh xung đột giữa các tên biến toàn cục, theo cách tương tự, các gói giúp tránh xung đột giữa các tên mô-đun.
Việc tạo một gói rất dễ dàng vì nó sử dụng cấu trúc tệp vốn có của hệ thống. Vì vậy, chỉ cần nhét các mô-đun vào một thư mục và bạn có nó ở đó, tên thư mục là tên gói. Nhập mô-đun hoặc nội dung của mô-đun từ gói này yêu cầu tên gói làm tiền tố cho tên mô-đun được nối bằng dấu chấm.
Ghi chú. Về mặt kỹ thuật, bạn cũng có thể nhập gói, nhưng than ôi, nó không nhập các mô-đun trong gói vào không gian tên cục bộ, do đó, thực tế nó vô dụng
10. Các thuộc tính toàn cầu, được bảo vệ và riêng tư trong Python là gì?
- Biến toàn cục là biến công khai được định nghĩa trong phạm vi toàn cầu. Để sử dụng biến trong phạm vi toàn cục bên trong một hàm, chúng ta sử dụng từ khóa
43def myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
- Các thuộc tính được bảo vệ là các thuộc tính được xác định bằng tiền tố gạch dưới cho mã định danh của chúng, ví dụ:. _sara. Chúng vẫn có thể được truy cập và sửa đổi từ bên ngoài lớp mà chúng được định nghĩa nhưng một nhà phát triển có trách nhiệm không nên làm như vậy
- Thuộc tính riêng tư là các thuộc tính có tiền tố gạch dưới kép cho mã định danh của chúng, vd. __ansh. Chúng không thể được truy cập hoặc sửa đổi trực tiếp từ bên ngoài và sẽ dẫn đến AttributeError nếu một nỗ lực như vậy được thực hiện
11. Việc sử dụng bản thân trong Python là gì?
Bản thân được sử dụng để đại diện cho thể hiện của lớp. Với từ khóa này, bạn có thể truy cập các thuộc tính và phương thức của lớp trong python. Nó liên kết các thuộc tính với các đối số đã cho. bản thân được sử dụng ở những nơi khác nhau và thường được coi là một từ khóa. Nhưng không giống như trong C++, self không phải là một từ khóa trong Python
12. Có cái gì bên trong__?
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
44 là một phương thức khởi tạo trong Python và được tự động gọi để cấp phát bộ nhớ khi một đối tượng/thể hiện mới được tạo. Tất cả các lớp đều có phương thức __init__ được liên kết với chúng. Nó giúp phân biệt các phương thức và thuộc tính của một lớp với các biến cục bộmy_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
513. Break, Continue và Pass trong Python là gì?
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
614. Các bài kiểm tra đơn vị trong Python là gì?
- Kiểm tra đơn vị là một khung kiểm tra đơn vị của Python
- Kiểm tra đơn vị có nghĩa là kiểm tra các thành phần khác nhau của phần mềm một cách riêng biệt. Bạn có thể nghĩ về lý do tại sao thử nghiệm đơn vị lại quan trọng không? . Bây giờ, giả sử phần mềm của bạn bị hỏng tại một thời điểm. Làm thế nào bạn sẽ tìm thấy thành phần nào chịu trách nhiệm phá vỡ phần mềm? . Có thể có nhiều kết hợp như vậy
- Đây là lý do tại sao cần phải kiểm tra đúng từng thành phần để chúng tôi biết thành phần nào có thể chịu trách nhiệm cao cho sự cố của phần mềm
15. Chuỗi doc trong Python là gì?
- Chuỗi tài liệu hoặc chuỗi tài liệu là một chuỗi nhiều dòng được sử dụng để ghi lại một đoạn mã cụ thể
- Chuỗi tài liệu sẽ mô tả chức năng hoặc phương thức làm gì
16. cắt lát trong Python là gì?
- Như tên cho thấy, 'cắt lát' đang tham gia vào
- Cú pháp để cắt là [bắt đầu. dừng lại. bươc]
- start là chỉ mục bắt đầu từ nơi để cắt một danh sách hoặc bộ dữ liệu
- dừng lại là chỉ số kết thúc hoặc nơi để sop
- bước là số bước để nhảy
- Giá trị mặc định cho bắt đầu là 0, dừng là số mục, bước là 1
- Cắt lát có thể được thực hiện trên chuỗi, mảng, danh sách và bộ dữ liệu
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
717. Giải thích làm thế nào bạn có thể làm cho Tập lệnh Python có thể thực thi được trên Unix?
- Tệp tập lệnh phải bắt đầu bằng #. /usr/bin/env con trăn
18. Sự khác biệt giữa Mảng Python và danh sách là gì?
- Mảng trong python chỉ có thể chứa các phần tử có cùng kiểu dữ liệu. e. , kiểu dữ liệu của mảng phải đồng nhất. Nó là một trình bao bọc mỏng xung quanh các mảng ngôn ngữ C và tiêu tốn ít bộ nhớ hơn nhiều so với các danh sách
- Danh sách trong python có thể chứa các phần tử thuộc các kiểu dữ liệu khác nhau. e. , kiểu dữ liệu của danh sách có thể không đồng nhất. Nó có nhược điểm là tốn bộ nhớ lớn
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
8Câu hỏi phỏng vấn Python dành cho người có kinh nghiệm
19. Bộ nhớ được quản lý như thế nào trong Python?
- Quản lý bộ nhớ trong Python được xử lý bởi Trình quản lý bộ nhớ Python. Bộ nhớ được cấp phát bởi trình quản lý ở dạng không gian heap riêng dành riêng cho Python. Tất cả các đối tượng Python được lưu trữ trong heap này và ở chế độ riêng tư, lập trình viên không thể truy cập được. Mặc dù vậy, python cung cấp một số chức năng API cốt lõi để hoạt động trên không gian heap riêng tư
- Ngoài ra, Python có bộ sưu tập rác tích hợp để tái chế bộ nhớ không sử dụng cho không gian heap riêng tư
20. Không gian tên Python là gì?
Không gian tên trong Python đảm bảo rằng tên đối tượng trong chương trình là duy nhất và có thể được sử dụng mà không có bất kỳ xung đột nào. Python triển khai các không gian tên này dưới dạng từ điển với 'tên là khóa' được ánh xạ tới một 'đối tượng là giá trị' tương ứng. This allows for multiple namespaces to use the same name and map it to a separate object. Một vài ví dụ về không gian tên như sau
- Không gian tên cục bộ bao gồm các tên cục bộ bên trong một hàm. không gian tên được tạo tạm thời cho một lệnh gọi hàm và bị xóa khi hàm trả về
- Không gian tên toàn cầu bao gồm các tên từ các gói/mô-đun đã nhập khác nhau đang được sử dụng trong dự án hiện tại. Không gian tên này được tạo khi gói được nhập vào tập lệnh và tồn tại cho đến khi thực thi tập lệnh
- Không gian tên tích hợp bao gồm các chức năng tích hợp sẵn của Python lõi và tên tích hợp cho nhiều loại ngoại lệ khác nhau
Vòng đời của một không gian tên phụ thuộc vào phạm vi của các đối tượng mà chúng được ánh xạ tới. Nếu phạm vi của một đối tượng kết thúc, vòng đời của không gian tên đó sẽ kết thúc. Do đó, không thể truy cập các đối tượng không gian tên bên trong từ một không gian tên bên ngoài
21. Độ phân giải phạm vi trong Python là gì?
Đôi khi các đối tượng trong cùng một phạm vi có cùng tên nhưng hoạt động khác nhau. Trong những trường hợp như vậy, độ phân giải phạm vi sẽ tự động phát huy tác dụng trong Python. Một vài ví dụ về hành vi như vậy là
- Các mô-đun Python cụ thể là 'math' và 'cmath' có rất nhiều chức năng chung cho cả hai -
45,def myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
46,def myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
47, v.v. Để giải quyết sự mơ hồ này, cần thêm tiền tố vào mô-đun tương ứng của chúng, nhưdef myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
48 vàdef myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
49def myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
- Xem xét mã bên dưới, một đối tượng tạm thời đã được khởi tạo thành 10 trên toàn cầu và sau đó là 20 khi gọi hàm. Tuy nhiên, lệnh gọi hàm không thay đổi giá trị của tạm thời trên toàn cầu. Ở đây, chúng ta có thể quan sát thấy rằng Python vạch ra một ranh giới rõ ràng giữa các biến toàn cục và biến cục bộ, coi các không gian tên của chúng là các danh tính riêng biệt
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
4Hành vi này có thể được ghi đè bằng cách sử dụng từ khóa
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
43 bên trong hàm, như trong ví dụ saudef myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
622. Trình trang trí trong Python là gì?
Trình trang trí trong Python về cơ bản là các hàm bổ sung chức năng cho một hàm hiện có trong Python mà không thay đổi cấu trúc của chính hàm đó. Chúng được đại diện là
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
51 trong Python và được gọi theo kiểu từ dưới lên. Ví dụdef myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
8Vẻ đẹp của các bộ trang trí nằm ở chỗ bên cạnh việc thêm chức năng vào đầu ra của phương thức, chúng thậm chí có thể chấp nhận các đối số cho các hàm và có thể sửa đổi thêm các đối số đó trước khi chuyển nó cho chính hàm đó. Hàm lồng bên trong, i. e. chức năng 'trình bao bọc', đóng một vai trò quan trọng ở đây. Nó được triển khai để thực thi đóng gói và do đó, giữ cho chính nó ẩn khỏi phạm vi toàn cầu
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
923. Dict và List hiểu là gì?
Khả năng hiểu của Python, giống như trình trang trí, là các cấu trúc đường cú pháp giúp xây dựng các danh sách, từ điển hoặc tập hợp đã thay đổi và được lọc từ một danh sách, từ điển hoặc tập hợp nhất định. Sử dụng khả năng hiểu giúp tiết kiệm rất nhiều thời gian và mã có thể dài hơn đáng kể [chứa nhiều dòng mã hơn]. Hãy xem một số ví dụ, trong đó việc hiểu có thể thực sự có lợi
- Thực hiện các phép toán trên toàn bộ danh sách
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
0- Thực hiện các thao tác lọc có điều kiện trên toàn bộ danh sách
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
1- Kết hợp nhiều danh sách thành một
Khả năng hiểu cho phép nhiều trình vòng lặp và do đó, có thể được sử dụng để kết hợp nhiều danh sách thành một.
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
2- Làm phẳng danh sách nhiều chiều
Một cách tiếp cận tương tự của các trình lặp lồng nhau [như trên] có thể được áp dụng để làm phẳng một danh sách đa chiều hoặc hoạt động trên các phần tử bên trong của nó.
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
3Ghi chú. Khả năng hiểu danh sách có tác dụng tương tự như phương pháp bản đồ trong các ngôn ngữ khác. Chúng tuân theo ký hiệu trình tạo tập hợp toán học hơn là các hàm lọc và bản đồ trong Python
24. Lambda trong Python là gì?
Lambda là một hàm ẩn danh trong Python, có thể chấp nhận bất kỳ số lượng đối số nào, nhưng chỉ có thể có một biểu thức duy nhất. Nó thường được sử dụng trong các tình huống yêu cầu chức năng ẩn danh trong một khoảng thời gian ngắn. Các hàm lambda có thể được sử dụng theo một trong hai cách
- Gán các hàm lambda cho một biến
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
4- Wrapping lambda functions inside another function
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
525. Làm thế nào để bạn sao chép một đối tượng trong Python?
Trong Python, câu lệnh gán [toán tử ______052] không sao chép các đối tượng. Thay vào đó, nó tạo ra một ràng buộc giữa đối tượng hiện có và tên biến mục tiêu. Để tạo các bản sao của một đối tượng trong Python, chúng ta cần sử dụng mô-đun copy. Ngoài ra, có hai cách tạo bản sao cho đối tượng đã cho bằng cách sử dụng mô-đun sao chép -
Shallow Copy là bản sao bit của một đối tượng. Đối tượng được sao chép được tạo có một bản sao chính xác của các giá trị trong đối tượng ban đầu. Nếu một trong hai giá trị là tham chiếu đến các đối tượng khác, thì chỉ các địa chỉ tham chiếu cho cùng một giá trị được sao chép.
Deep Copy sao chép đệ quy tất cả các giá trị từ nguồn sang đối tượng đích, i. e. nó thậm chí còn sao chép các đối tượng được tham chiếu bởi đối tượng nguồn.
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
626. Sự khác biệt giữa xrange và phạm vi trong Python là gì?
xrange[] và range[] khá giống nhau về chức năng. Cả hai đều tạo ra một chuỗi các số nguyên, với sự khác biệt duy nhất là
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
53 trả về một danh sách Python, trong khi đó, def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
54 trả về một đối tượng xrangeVì vậy, làm thế nào mà làm cho một sự khác biệt? . Kỹ thuật này thường được sử dụng với một trình tạo kiểu đối tượng và được gọi là "hiệu suất"
Năng suất là rất quan trọng trong các ứng dụng mà bộ nhớ là một hạn chế. Tạo một danh sách tĩnh như trong phạm vi [] có thể dẫn đến
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
55 trong các điều kiện như vậy, trong khi, xrange [] có thể xử lý nó một cách tối ưu bằng cách sử dụng bộ nhớ vừa đủ cho trình tạo [so với ít hơn đáng kể]def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
7Ghi chú. xrange đã không còn được dùng kể từ Python 3. x. Bây giờ,
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
56 thực hiện chính xác những gì mà def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
57 đã từng làm trong Python 2. x, vì sử dụng xrange[] tốt hơn nhiều so với hàm range[] ban đầu trong Python 2. x27. Pickling và unpickling là gì?
Thư viện Python cung cấp một tính năng - tuần tự hóa ngay lập tức. Tuần tự hóa một đối tượng đề cập đến việc chuyển đổi nó thành một định dạng có thể được lưu trữ, để có thể giải tuần tự hóa nó, sau này, để lấy đối tượng ban đầu. Ở đây, mô-đun dưa chua phát huy tác dụng
muối chua
- Pickling là tên của quy trình tuần tự hóa trong Python. Bất kỳ đối tượng nào trong Python đều có thể được tuần tự hóa thành luồng byte và được kết xuất dưới dạng tệp trong bộ nhớ. Quá trình ngâm được nén nhưng đối tượng ngâm có thể được nén thêm. Hơn nữa, pickle theo dõi các đối tượng mà nó đã đánh số thứ tự và việc đánh số thứ tự có thể di chuyển qua các phiên bản
- Hàm được sử dụng cho quá trình trên là
58def myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
Bóc vỏ
- Unpickling là nghịch đảo hoàn toàn của pickling. Nó giải tuần tự hóa luồng byte để tạo lại các đối tượng được lưu trữ trong tệp và tải đối tượng vào bộ nhớ
- Hàm được sử dụng cho quá trình trên là
59def myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
Ghi chú. Python has another, more primitive, serialization module called marshall, which exists primarily to support . pyc trong Python và khác biệt đáng kể so với dưa chua
28. Máy phát điện trong Python là gì?
Trình tạo là các hàm trả về một tập hợp các mục có thể lặp lại, mỗi lần một mục, theo một cách đã định. Nói chung, các trình tạo được sử dụng để tạo các trình vòng lặp với cách tiếp cận khác. Họ sử dụng từ khóa
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
60 thay vì def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
61 để trả về một đối tượng trình tạo. Hãy thử và xây dựng một trình tạo cho các số fibonacci -
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
829. PYTHONPATH trong Python là gì?
PYTHONPATH là một biến môi trường mà bạn có thể đặt để thêm các thư mục bổ sung nơi Python sẽ tìm kiếm các mô-đun và gói. Điều này đặc biệt hữu ích trong việc duy trì các thư viện Python mà bạn không muốn cài đặt ở vị trí mặc định chung
30. Việc sử dụng các hàm help[] và dir[] là gì?
help[] trong Python được sử dụng để hiển thị tài liệu về các mô-đun, lớp, hàm, từ khóa, v.v. Nếu không có tham số nào được chuyển đến hàm
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
62, thì tiện ích trợ giúp tương tác sẽ được khởi chạy trên bảng điều khiển. hàm dir[] cố gắng trả về một danh sách các thuộc tính và phương thức hợp lệ của đối tượng mà nó được gọi. Nó hoạt động khác với các đối tượng khác nhau, vì nó nhằm mục đích tạo ra dữ liệu phù hợp nhất, thay vì thông tin đầy đủ.
- Đối với các đối tượng Mô-đun/Thư viện, nó trả về một danh sách tất cả các thuộc tính, có trong mô-đun đó
- Đối với Đối tượng lớp, nó trả về danh sách tất cả các thuộc tính hợp lệ và thuộc tính cơ sở
- Không có đối số nào được truyền, nó trả về một danh sách các thuộc tính trong phạm vi hiện tại
31. Đâu là sự khác biệt giữa. py và. tập tin pyc?
- tệp py chứa mã nguồn của chương trình. Nhưng trái lại,. tệp pyc chứa mã byte của chương trình của bạn. Chúng tôi nhận được mã byte sau khi biên dịch. tệp py [mã nguồn]. . các tệp pyc không được tạo cho tất cả các tệp bạn chạy. Nó chỉ được tạo cho các tệp bạn nhập
- Trước khi thực hiện chương trình python, trình thông dịch python sẽ kiểm tra các tệp đã biên dịch. Nếu tệp có mặt, máy ảo sẽ thực thi nó. Nếu không tìm thấy, nó sẽ kiểm tra. tập tin py. Nếu tìm thấy, biên dịch nó thành. pyc và sau đó máy ảo python thực thi nó
- Đang có. pyc giúp bạn tiết kiệm thời gian biên dịch
32. Làm thế nào Python được giải thích?
- Python là một ngôn ngữ không được giải thích hoặc biên dịch. Giải thích hoặc biên dịch là tài sản của việc thực hiện. Python là một mã byte [tập hợp các hướng dẫn có thể đọc được của trình thông dịch] được diễn giải chung
- Mã nguồn là một tệp có. phần mở rộng py
- Python biên dịch mã nguồn thành một bộ hướng dẫn cho máy ảo. Trình thông dịch Python là một triển khai của máy ảo đó. Định dạng trung gian này được gọi là “bytecode”
- mã nguồn py được biên dịch đầu tiên để cung cấp. pyc là mã byte. Mã byte này sau đó có thể được giải thích bởi CPython hoặc JIT chính thức [trình biên dịch Just in Time] do PyPy biên dịch
33. Các đối số được truyền theo giá trị hoặc theo tham chiếu trong python như thế nào?
- Vượt qua giá trị. Bản sao của đối tượng thực tế được thông qua. Changing the value of the copy of the object will not change the value of the original object
- Chuyển qua tham chiếu. Tham chiếu đến đối tượng thực tế được thông qua. Thay đổi giá trị của đối tượng mới sẽ thay đổi giá trị của đối tượng ban đầu
Trong Python, các đối số được truyền theo tham chiếu, tôi. e. , tham chiếu đến đối tượng thực tế được thông qua
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
934. Trình vòng lặp trong Python là gì?
- Một iterator là một đối tượng
- Nó nhớ trạng thái của nó tôi. e. , vị trí của nó trong quá trình lặp lại [xem mã bên dưới để biết cách thực hiện]
- Phương thức __iter__[] khởi tạo một iterator
- Nó có phương thức __next__[] trả về mục tiếp theo trong lần lặp và trỏ đến phần tử tiếp theo. Khi đến cuối đối tượng có thể lặp lại __next__[] phải trả về ngoại lệ StopIteration
- Nó cũng có thể tự lặp lại
- Trình lặp là các đối tượng mà chúng ta có thể lặp qua các đối tượng có thể lặp lại như danh sách, chuỗi, v.v.
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
5035. Giải thích cách xóa một tệp trong Python?
Sử dụng lệnh os. xóa [file_name]
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
5136. Giải thích hàm split[] và join[] trong Python?
- Bạn có thể sử dụng hàm split[] để tách một chuỗi dựa trên dấu phân cách thành danh sách các chuỗi
- Bạn có thể sử dụng hàm join[] để nối danh sách các chuỗi dựa trên dấu phân cách để đưa ra một chuỗi
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
5237. *args và **kwargs nghĩa là gì?
* lập luận
- *args là một cú pháp đặc biệt được sử dụng trong định nghĩa hàm để truyền các đối số có độ dài thay đổi
- “*” có nghĩa là độ dài thay đổi và “args” là tên được sử dụng theo quy ước. Bạn có thể sử dụng bất kỳ khác
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
53**kwargs
- **kwargs là một cú pháp đặc biệt được sử dụng trong định nghĩa hàm để truyền các đối số từ khóa có độ dài thay đổi
- Ngoài ra, ở đây, "kwargs" chỉ được sử dụng theo quy ước. Bạn có thể sử dụng bất kỳ tên nào khác
- Đối số được gắn từ khóa có nghĩa là một biến có tên khi được truyền cho một hàm
- Nó thực sự là một từ điển về tên biến và giá trị của nó
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
5438. Chỉ mục tiêu cực là gì và tại sao chúng được sử dụng?
- Chỉ mục phủ định là các chỉ mục từ cuối danh sách hoặc bộ hoặc chuỗi
- Arr[-1] có nghĩa là phần tử cuối cùng của mảng Arr[]
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
55Câu hỏi phỏng vấn Python OOPS
39. Làm thế nào để bạn tạo một lớp trong Python?
Để tạo class trong python chúng ta sử dụng từ khóa “class” như ví dụ bên dưới
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
56Để khởi tạo hoặc tạo một đối tượng từ lớp đã tạo ở trên, chúng ta làm như sau
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
57Để truy cập thuộc tính name, chúng ta chỉ cần gọi thuộc tính bằng toán tử dấu chấm như hình bên dưới
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
58Để tạo các phương thức bên trong lớp, chúng ta gộp các phương thức thuộc phạm vi của lớp như hình bên dưới
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
59Tham số self trong init và các hàm giới thiệu biểu thị tham chiếu đến thể hiện của lớp hiện tại được sử dụng để truy cập các thuộc tính và phương thức của lớp đó. Tham số self phải là tham số đầu tiên của bất kỳ phương thức nào được định nghĩa bên trong lớp. Phương thức của lớp InterviewbitEmployee có thể được truy cập như hình bên dưới
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
60Chương trình tổng thể sẽ như thế này
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
6140. Tính kế thừa hoạt động như thế nào trong python?
Kế thừa trao quyền cho một lớp để truy cập tất cả các thuộc tính và phương thức của lớp khác. Nó hỗ trợ khả năng sử dụng lại mã và giúp nhà phát triển duy trì các ứng dụng mà không cần mã dự phòng. Lớp kế thừa từ lớp khác là lớp con hay còn gọi là lớp dẫn xuất. Lớp mà từ đó một lớp con dẫn xuất các thành viên được gọi là lớp cha hoặc lớp cha
Python hỗ trợ các loại kế thừa khác nhau, chúng là
- Thừa kế đơn. Lớp con dẫn xuất các thành viên của một lớp cha
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
62- Kế thừa đa cấp. Các thành viên của lớp cha, A, được kế thừa bởi lớp con, lớp này sau đó được kế thừa bởi lớp con khác, B. Các tính năng của lớp cơ sở và lớp dẫn xuất được tiếp tục kế thừa vào lớp dẫn xuất mới, C. Ở đây, A là lớp ông của lớp C
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
63- Đa thừa kế. Điều này đạt được khi một lớp con dẫn xuất các thành viên từ nhiều hơn một lớp cha. Tất cả các tính năng của lớp cha được kế thừa trong lớp con
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
64- Kế thừa phân cấp. Khi một lớp cha được dẫn xuất bởi nhiều hơn một lớp con, nó được gọi là kế thừa phân cấp
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
6541. Làm thế nào để bạn truy cập các thành viên cha mẹ trong lớp con?
Sau đây là những cách sử dụng mà bạn có thể truy cập các thành viên của lớp cha trong một lớp con
- Bằng cách sử dụng tên lớp Parent. Bạn có thể sử dụng tên của lớp cha để truy cập các thuộc tính như trong ví dụ bên dưới
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
66- Bằng cách sử dụng siêu[]. Các thành viên của lớp cha có thể được truy cập trong lớp con bằng từ khóa super
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
6742. Các chỉ định truy cập có được sử dụng trong python không?
Python không sử dụng các chỉ định truy cập cụ thể như riêng tư, công khai, được bảo vệ, v.v. Tuy nhiên, nó không lấy được điều này từ bất kỳ biến nào. Nó có khái niệm bắt chước hành vi của các biến bằng cách sử dụng một dấu gạch dưới đơn [được bảo vệ] hoặc dấu gạch dưới kép [riêng tư] làm tiền tố cho tên biến. Theo mặc định, các biến không có tiền tố gạch dưới là công khai
Thí dụ
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
6843. Có thể gọi lớp cha mà không cần tạo cá thể của nó không?
Có, có thể nếu lớp cơ sở được khởi tạo bởi các lớp con khác hoặc nếu lớp cơ sở là một phương thức tĩnh
44. Làm thế nào là một lớp trống được tạo ra trong python?
Một lớp trống không có thành viên nào được định nghĩa trong đó. Nó được tạo bằng cách sử dụng từ khóa pass [lệnh pass không làm gì trong python]. Chúng ta có thể tạo các đối tượng cho lớp này bên ngoài lớp.
Ví dụ-
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
69Đầu ra.
Tên được tạo = Phỏng vấn
45. Phân biệt giữa công cụ sửa đổi mới và ghi đè
Công cụ sửa đổi mới được sử dụng để hướng dẫn trình biên dịch sử dụng triển khai mới chứ không phải chức năng của lớp cơ sở. Công cụ sửa đổi Ghi đè rất hữu ích để ghi đè chức năng của lớp cơ sở bên trong lớp con
46. Tại sao hoàn thiện được sử dụng?
Phương thức hoàn thiện được sử dụng để giải phóng các tài nguyên không được quản lý và dọn dẹp trước khi phương thức thu gom rác được gọi. Điều này giúp thực hiện các tác vụ quản lý bộ nhớ
47. Phương thức init trong python là gì?
Phương thức init hoạt động tương tự như hàm tạo trong Java. Phương thức được chạy ngay khi một đối tượng được khởi tạo. Nó rất hữu ích để khởi tạo bất kỳ thuộc tính hoặc hành vi mặc định nào của đối tượng tại thời điểm khởi tạo.
Ví dụ.
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
7048. Bạn sẽ kiểm tra xem một lớp có phải là con của một lớp khác không?
Điều này được thực hiện bằng cách sử dụng phương thức gọi là issubclass[] do python cung cấp. Phương thức này cho chúng ta biết liệu có lớp nào là con của lớp khác hay không bằng cách trả về true hoặc false tương ứng.
Ví dụ.
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
71- Chúng ta có thể kiểm tra xem một đối tượng có phải là một thể hiện của một lớp hay không bằng cách sử dụng phương thức isinstance[]
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
72Câu hỏi phỏng vấn Python Pandas
49. Bạn biết gì về gấu trúc?
- Pandas là một thư viện mã nguồn mở dựa trên python được sử dụng trong các ứng dụng thao tác dữ liệu yêu cầu hiệu năng cao. Tên này bắt nguồn từ “Dữ liệu bảng” có dữ liệu đa chiều. Điều này được phát triển vào năm 2008 bởi Wes McKinney và được phát triển để phân tích dữ liệu
- Pandas rất hữu ích trong việc thực hiện 5 bước chính của phân tích dữ liệu - Tải dữ liệu, làm sạch/thao tác dữ liệu, chuẩn bị, lập mô hình và phân tích dữ liệu
50. Xác định khung dữ liệu gấu trúc
Khung dữ liệu là cấu trúc dạng bảng và có thể thay đổi 2D để biểu thị dữ liệu được gắn nhãn trục - hàng và cột.
Cú pháp tạo dataframe.
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
73ở đâu
- dữ liệu - Đại diện cho các dạng khác nhau như sê-ri, bản đồ, ndarray, danh sách, dict, v.v.
- chỉ mục - Đối số tùy chọn biểu thị chỉ mục cho nhãn hàng
- cột - Đối số tùy chọn cho nhãn cột
- Dtype - kiểu dữ liệu của mỗi cột. một lần nữa tùy chọn
51. Bạn sẽ kết hợp các khung dữ liệu gấu trúc khác nhau như thế nào?
Các khung dữ liệu có thể được kết hợp bằng cách sử dụng các phương pháp dưới đây
- phương thức chắp thêm []. Điều này được sử dụng để xếp chồng các khung dữ liệu theo chiều ngang. cú pháp
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
74- phương thức concat[]. Điều này được sử dụng để xếp chồng các khung dữ liệu theo chiều dọc. Điều này được sử dụng tốt nhất khi các khung dữ liệu có cùng các cột và các trường tương tự. cú pháp
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
75- phương thức tham gia []. Điều này được sử dụng để trích xuất dữ liệu từ các khung dữ liệu khác nhau có một hoặc nhiều cột chung
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
7652. Can you create a series from the dictionary object in pandas?
One dimensional array capable of storing different data types is called a series. We can create pandas series from a dictionary object as shown below
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
77If an index is not specified in the input method, then the keys of the dictionaries are sorted in ascending order for constructing the index. In case the index is passed, then values of the index label will be extracted from the dictionary
53. How will you identify and deal with missing values in a dataframe?
We can identify if a dataframe has missing values by using the isnull[] and isna[] methods
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
78We can handle missing values by either replacing the values in the column with 0 as follows
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
79Or by replacing it with the mean value of the column
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
8054. What do you understand by reindexing in pandas?
Reindexing is the process of conforming a dataframe to a new index with optional filling logic. If the values are missing in the previous index, then NaN/NA is placed in the location. A new object is returned unless a new index is produced that is equivalent to the current one. The copy value is set to False. This is also used for changing the index of rows and columns in the dataframe
55. How to add new column to pandas dataframe?
A new column can be added to a pandas dataframe as follows
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
8156. How will you delete indices, rows and columns from a dataframe?
To delete an Index
- Execute
63 for removing the index by namedef myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
- Alternatively, the
64 can be assigned to Nonedef myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
- For example, if you have the below dataframe
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
82- To drop the index name “Names”
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
83To delete row/column from dataframe
65 method is used to delete row/column from dataframedef myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
- The axis argument is passed to the drop method where if the value is 0, it indicates to drop/delete a row and if 1 it has to drop the column
- Additionally, we can try to delete the rows/columns in place by setting the value of inplace to True. This makes sure that the job is done without the need for reassignment
- The duplicate values from the row/column can be deleted by using the
66 methoddef myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
57. Can you get items of series A that are not available in another series B?
This can be achieved by using the
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
67 [not/negation symbol] and def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
68 method as shown belowmy_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
8458. How will you get the items that are not common to both the given series A and B?
We can achieve this by first performing the union of both series, then taking the intersection of both series. Then we follow the approach of getting items of union that are not there in the list of the intersection
The following code demonstrates this
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
8559. While importing data from different sources, can the pandas library recognize dates?
Yes, they can, but with some bit of help. We need to add the parse_dates argument while we are reading data from the sources. Consider an example where we read data from a CSV file, we may encounter different date-time formats that are not readable by the pandas library. In this case, pandas provide flexibility to build our custom date parser with the help of lambda functions as shown below
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
86Câu hỏi phỏng vấn NumPy
60. What do you understand by NumPy?
NumPy is one of the most popular, easy-to-use, versatile, open-source, python-based, general-purpose package that is used for processing arrays. NumPy is short for NUMerical PYthon. This is very famous for its highly optimized tools that result in high performance and powerful N-Dimensional array processing feature that is designed explicitly to work on complex arrays. Due to its popularity and powerful performance and its flexibility to perform various operations like trigonometric operations, algebraic and statistical computations, it is most commonly used in performing scientific computations and various broadcasting functions. The following image shows the applications of NumPy
61. How are NumPy arrays advantageous over python lists?
- The list data structure of python is very highly efficient and is capable of performing various functions. But, they have severe limitations when it comes to the computation of vectorized operations which deals with element-wise multiplication and addition. The python lists also require the information regarding the type of every element which results in overhead as type dispatching code gets executes every time any operation is performed on any element. This is where the NumPy arrays come into the picture as all the limitations of python lists are handled in NumPy arrays
- Additionally, as the size of the NumPy arrays increases, NumPy becomes around 30x times faster than the Python List. This is because the Numpy arrays are densely packed in the memory due to their homogenous nature. This ensures the memory free up is also faster
62. What are the steps to create 1D, 2D and 3D arrays?
- 1D array creation
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
87- 2D array creation
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
88- 3D array creation
my_tuple = ['sara', 6, 5, 0.97]
my_list = ['sara', 6, 5, 0.97]
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'sara'
my_tuple[0] = 'ansh' # modifying tuple => throws an error
my_list[0] = 'ansh' # modifying list => list modified
print[my_tuple[0]] # output => 'sara'
print[my_list[0]] # output => 'ansh'
89- ND array creation. This can be achieved by giving the ndmin attribute. The below example demonstrates the creation of a 6D array
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
4063. You are given a numpy array and a new column as inputs. How will you delete the second column and replace the column with a new column value?
Example.
Given array.
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
41New Column values
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
42Solution
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
4364. How will you efficiently load data from a text file?
We can use the method
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
69 which can automatically read the file’s header and footer lines and the comments if anyThis method is highly efficient and even if this method feels less efficient, then the data should be represented in a more efficient format such as CSV etc. Various alternatives can be considered depending on the version of NumPy used
Following are the file formats that are supported
- Text files. These files are generally very slow, huge but portable and are human-readable
- Raw binary. This file does not have any metadata and is not portable. But they are fast
- Pickle. These are borderline slow and portable but depends on the NumPy versions
- HDF5. This is known as the High-Powered Kitchen Sink format which supports both PyTables and h5py format
- npy. Đây là định dạng dữ liệu nhị phân riêng của NumPy cực kỳ đơn giản, hiệu quả và di động
65. Bạn sẽ đọc dữ liệu CSV thành một mảng trong NumPy như thế nào?
Điều này có thể đạt được bằng cách sử dụng phương thức genfromtxt[] bằng cách đặt dấu phân cách là dấu phẩy
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
4466. Bạn sẽ sắp xếp mảng dựa trên cột thứ N như thế nào?
For example, consider an array arr
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
45Let us try to sort the rows by the 2nd column so that we get
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
46We can do this by using the sort[] method in numpy as
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
47We can also perform sorting and that too inplace sorting by doing
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
4867. How will you find the nearest value in a given numpy array?
We can use the argmin[] method of numpy as shown below
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
4968. How will you reverse the numpy array using one line of code?
This can be done as shown in the following
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
60where arr = original given array, reverse_array is the resultant after reversing all elements in the input
69. How will you find the shape of any given NumPy array?
We can use the shape attribute of the numpy array to find the shape. It returns the shape of the array in terms of row count and column count of the array
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
61Python Libraries Interview Questions
70. Differentiate between a package and a module in python
The module is a single python file. A module can import other modules [other python files] as objects. Whereas, a package is the folder/directory where different sub-packages and the modules reside
A python module is created by saving a file with the extension of
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
70. This file will have classes and functions that are reusable in the code as well as across modulesA python package is created by following the below steps
- Create a directory and give a valid name that represents its operation
- Place modules of one kind in this directory
- Create
71 file in this directory. This lets python know the directory we created is a package. The contents of this package can be imported across different modules in other packages to reuse the functionalitydef myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
71. What are some of the most commonly used built-in modules in Python?
Python modules are the files having python code which can be functions, variables or classes. These go by . py extension. The most commonly available built-in modules are
- os
- math
- sys
- random
- re
- datetime
- JSON
72. What are lambda functions?
Lambda functions are generally inline, anonymous functions represented by a single expression. They are used for creating function objects during runtime. They can accept any number of parameters. They are usually used where functions are required only for a short period. They can be used as
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
6273. How can you generate random numbers?
Python provides a module called random using which we can generate random numbers
- We have to import a random module and call the
72 method as shown belowdef myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
- The random[] method generates float values lying between 0 and 1 randomly
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
63- To generate customised random numbers between specified ranges, we can use the
73 methoddef myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
Syntax.
74def myEmptyFunc[]: # do nothing pass myEmptyFunc[] # nothing happens ## Without the pass keyword # File "", line 3 # IndentationError: expected an indented block
For example.
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
6474. Can you easily check if all characters in the given string is alphanumeric?
This can be easily done by making use of the isalnum[] method that returns true in case the string has only alphanumeric characters
For Example -
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
65Another way is to use
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
75 method from the re [regex] module as showndef myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
6675. What are the differences between pickling and unpickling?
Pickling is the conversion of python objects to binary form. Whereas, unpickling is the conversion of binary form data to python objects. The pickled objects are used for storing in disks or external memory locations. Unpickled objects are used for getting the data back as python objects upon which processing can be done in python
Python provides a
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
76 module for achieving this. Pickling uses the def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
58 method to dump python objects into disks. Unpickling uses the def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
59 method to get back the data as python objects76. Define GIL
GIL stands for Global Interpreter Lock. This is a mutex used for limiting access to python objects and aids in effective thread synchronization by avoiding deadlocks. GIL helps in achieving multitasking [and not parallel computing]. The following diagram represents how GIL works
Based on the above diagram, there are three threads. First Thread acquires the GIL first and starts the I/O execution. When the I/O operations are done, thread 1 releases the acquired GIL which is then taken up by the second thread. The process repeats and the GIL are used by different threads alternatively until the threads have completed their execution. The threads not having the GIL lock goes into the waiting state and resumes execution only when it acquires the lock
77. Define PYTHONPATH
It is an environment variable used for incorporating additional directories during the import of a module or a package. PYTHONPATH is used for checking if the imported packages or modules are available in the existing directories. Not just that, the interpreter uses this environment variable to identify which module needs to be loaded
78. Define PIP
PIP stands for Python Installer Package. As the name indicates, it is used for installing different python modules. It is a command-line tool providing a seamless interface for installing different python modules. It searches over the internet for the package and installs them into the working directory without the need for any interaction with the user. The syntax for this is
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
6779. Are there any tools for identifying bugs and performing static analysis in python?
Yes, there are tools like PyChecker and Pylint which are used as static analysis and linting tools respectively. PyChecker helps find bugs in python source code files and raises alerts for code issues and their complexity. Pylint checks for the module’s coding standards and supports different plugins to enable custom features to meet this requirement
80. Differentiate between deep and shallow copies
- Shallow copy does the task of creating new objects storing references of original elements. This does not undergo recursion to create copies of nested objects. It just copies the reference details of nested objects
- Bản sao sâu tạo một bản sao độc lập và mới của một đối tượng và thậm chí sao chép tất cả các đối tượng lồng nhau của phần tử gốc theo cách đệ quy
81. What is main function in python? How do you invoke it?
In the world of programming languages, the main is considered as an entry point of execution for a program. But in python, it is known that the interpreter serially interprets the file line-by-line. This means that python does not provide
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
79 function explicitly. But this doesn't mean that we cannot simulate the execution of main. This can be done by defining user-defined def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
79 function and by using the def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
81 property of python file. This def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
81 variable is a special built-in variable that points to the name of the current module. This can be done as shown belowdef myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
68Python Programming Examples
82. Write python function which takes a variable number of arguments
A function that takes variable arguments is called a function prototype. Syntax
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
69For example
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
80The * in the function argument represents variable arguments in the function
83. WAP [Write a program] which takes a sequence of numbers and check if all numbers are unique
You can do this by converting the list to set by using set[] method and comparing the length of this set with the length of the original list. If found equal, return True
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
8184. Write a program for counting the number of every character of a given text file
The idea is to use collections and pprint module as shown below
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
8285. Write a program to check and return the pairs of a given array A whose sum value is equal to a target value N
This can be done easily by using the phenomenon of hashing. We can use a hash map to check for the current value of the array, x. If the map has the value of [N-x], then there is our pair
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
8386. Write a Program to add two integers >0 without using the plus operator
We can use bitwise operators to achieve this
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
8487. Write a Program to solve the given equation assuming that a,b,c,m,n,o are constants
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
85By solving the equation, we get
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
8688. Write a Program to match a string that has the letter ‘a’ followed by 4 to 8 'b’s
We can use the re module of python to perform regex pattern comparison here
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
8789. Write a Program to convert date from yyyy-mm-dd format to dd-mm-yyyy format
We can again use the re module to convert the date string as shown below
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
88You can also use the datetime module as shown below
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
8990. Write a Program to combine two different dictionaries. While combining, if you find the same keys, you can add the values of these same keys. Output the new dictionary
We can use the Counter method from the collections module
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
9091. How will you access the dataset of a publicly shared spreadsheet in CSV format stored in Google Drive?
Chúng tôi có thể sử dụng mô-đun StringIO từ mô-đun io để đọc từ liên kết Google Drive và sau đó chúng tôi có thể sử dụng thư viện pandas bằng nguồn dữ liệu thu được
def myEmptyFunc[]:
# do nothing
pass
myEmptyFunc[] # nothing happens
## Without the pass keyword
# File "", line 3
# IndentationError: expected an indented block
91Conclusion
Trong bài viết này, chúng ta đã xem các câu hỏi phỏng vấn thường gặp dành cho nhà phát triển python. These questions along with regular problem practice sessions will help you crack any python based interviews. Over the years, python has gained a lot of popularity amongst the developer’s community due to its simplicity and ability to support powerful computations. Due to this, the demand for good python developers is ever-growing. Nevertheless, to mention, the perks of being a python developer are really good. Along with theoretical knowledge in python, there is an emphasis on the ability to write good quality code as well. So, keep learning and keep practicing problems and without a doubt, you can crack any interviews