Python Tutorial
❮ Home Next ❯
Learn Python
Python is a popular programming language.
Python can be used on a server to create web applications.
Start learning Python now »
Learning by Examples
With our "Try it Yourself" editor, you can edit Python code and view the result.
Example
print["Hello, World!"]
Try it Yourself »
Click on the "Try it Yourself" button to see how it works.
Python File Handling
In our File Handling section you will learn how to open, read, write, and delete files.
Python File Handling
Python Database Handling
In our database section you will learn how to access and work with MySQL and MongoDB databases:
Python MySQL Tutorial
Python MongoDB Tutorial
Python Exercises
Test Yourself With Exercises
Exercise:
Insert the missing part of the code below to output "Hello World".
["Hello World"]
Start the Exercise
Python Examples
Learn by examples! This tutorial supplements all explanations with clarifying examples.
See All Python Examples
Python Quiz
Test your Python skills with a quiz.
Python Quiz
My Learning
Track your progress with the free "My Learning" program here at W3Schools.
Log into your account, and start earning points!
This is an optional feature, you can study W3Schools without using My Learning.
Python Reference
You will also find complete function and method references:
Reference Overview
Built-in Functions
String Methods
List/Array Methods
Dictionary Methods
Tuple Methods
Set Methods
File Methods
Python Keywords
Python Exceptions
Python Glossary
Random Module
Requests Module
Math Module
CMath Module
Download Python
Download Python from the official Python web site: //python.org
Kickstart your career
Get certified by completing the course
Get certified
w3schoolsCERTIFIED.2022❮ Home Next ❯
File handling is an important part of any web application.
Python has several functions for creating, reading, updating, and deleting files.
File Handling
The key function for working with files in Python is the open[]
function.
The open[]
function takes two parameters; filename, and mode.
There are four different methods [modes] for opening a file:
"r"
- Read - Default value. Opens a file for
reading, error if the file does not exist
"a"
- Append - Opens a file for appending, creates the file if it does not exist
"w"
- Write - Opens a file for writing, creates the file if it does not exist
"x"
- Create - Creates the specified file, returns an error if the file exists
In addition you can specify if the file should be handled as binary or text mode
"t"
- Text - Default value. Text mode
"b"
- Binary - Binary mode
[e.g. images]
Syntax
To open a file for reading it is enough to specify the name of the file:
The code above is the same as:
f = open["demofile.txt", "rt"]
Because "r"
for read, and "t"
for text are the default values, you do not need to specify them.
Note: Make sure the file exists, or else you will get an error.
Hướng dẫn switch python
Đã đăng vào thg 3 22, 2019 8:46 SA 1 phút đọc 1 Giới thiệu.Khi mình tìm hiểu python ta đã thấy một điều quái lạ là vì sao python không định nghĩa các function ...
Chương trình php đơn giản
Webserver với PHPWampServer trên WindowsChương trình PHP đầu tiênCấu trúc chương trình PHPCài thêm các Extension cho Visual Studio CodeTìm hiểu Webserver, PHPĐể chạy ...
Hướng dẫn dom event javascript
Bắt sự kiện trực tiếp trên phần tử HTMLBạn có thể viết code JavaScript thi hành một tác vụ nào đó có một sự kiện xảy ra trên DOM [các phần tử HTML], ví ...
How do i test php code in visual studio?
Visual Studio Code is a great editor for PHP development. You get features like syntax highlighting and bracket matching, IntelliSense [code completion], and snippets out of the box and you can add ...
Hướng dẫn dom trong javascript
Javascript là một ngôn ngữ được sử dụng trong các trình duyệt Browser nên nó đóng một vai trò khá quan trọng trong các ứng dụng website. Và nhiệm vụ của ...
Hướng dẫn dùng no maches trong PHP
Nếu bạn theo dõi serie học Regular Expression từ đầu cho tới giờ thì hàm preg_match[] không còn xa lạ gì nữa, nhưng nếu bạn là khách viếng thăm và đây là bài ...
Hướng dẫn build php website
TechblogTool supportTrong bài viết ngày hôm này, Bizfly Cloud sẽ hướng dẫn bạn cách nhanh nhất để compile source PHP từ source.Điều kiện tiên quyếtBạn sẽ ...
Hướng dẫn xml stylesheet
Tổng quan về mô-đunChào mừng bạn đến với mô-đun Style Sheet. Mô-đun này giới thiệu cho bạn các bảng định kiểu. Bài viết này cũng nói về cách sử ...
Hướng dẫn get element xml python
Source code: Lib/xml/etree/ElementTree.pyThe xml.etree.ElementTree module implements a simple and efficient API for parsing and creating XML data.Changed in version 3.3: This module will use a fast ...
Hướng dẫn dùng fopen php trong PHP
Hàm fopen[] sẽ mở một file dựa vào đường dẫn truyền vào. Nếu truyền vào một URL, hàm sẽ sẽ tìm kiếm một trình xử lý giao thức[wrapper] cho URL đó, ...
Hướng dẫn r trong html
Trang chủTham khảoHTML SymbolsĐịnh nghĩa và sử dụngHTML symbols là những đoạn mã thay thế cho các ký tự, chèn vào văn bản HTML.Mã ký tự chữMã ký tự số Mã ...
Hướng dẫn ẩn hiện password html
Đã đăng vào thg 10 10, 2020 8:48 SA 2 phút đọc Vanilla JS: Ẩn hiện mật khẩu 1. Yêu cầuYêu cầu bài toán là người dùng nhập password vào ô input, khi click vào ...
Hướng dẫn dùng rip ajax trong PHP
Vietnamese [Tiếng Việt] translation by Dai Phong [you can also view the original English article] Nội dung chínhMã đánh dấu của ModalTạo ra dữ liệu trả về JSONCài đặt yêu ...
Hướng dẫn try catch php w3schools
What is an Exception?An exception is an object that describes an error or unexpected behaviour of a PHP script. Exceptions are thrown by many PHP functions and classes.User defined functions and ...
Hướng dẫn dùng nodejs介绍 JavaScript
Nhóm phát triển của chúng tôi vừa ra mắt website langlearning.net học tiếng Anh, Nga, Đức, Pháp, Việt, Trung, Hàn, Nhật, ... miễn phí cho tất cả mọi người. Là ...
Hướng dẫn php domdocument
Bài viết này mình sẽ hướng dẫn các bạn cách thao tác với XML DOM trong PHP. Bài viết có 2 phần: Đọc file XML Ghi file XMLChúng ta sẽ đi ngay vào phần 1 Phần 1: ...
Hướng dẫn dùng get code trong PHP
Khi các bạn truy cập sử dụng website thì bạn chính là một Client. Khi các bạn đăng nhập hay đăng ký tài khoản, hoặc là các bạn đăng comment thì dữ liệu sẽ ...
Hướng dẫn html dom là gì
Javascript là một ngôn ngữ được sử dụng trong các trình duyệt Browser nên nó đóng một vai trò khá quan trọng trong các ứng dụng website. Và nhiệm vụ của ...
Hướng dẫn for javascript
Danh sách các bài viết trong chuyên mục Học Javascript, đây là những bài viết mới nhất được cập nhật trong mục Javascript.Xin giới thiệu với các bạn tổng ...
Vẽ biểu đồ đường html
Mở đầuVẽ đồ thị hay biểu đồ là một một công việc không hề dễ dàng, đôi khi nó khiến cho các nhà thiết kế website phải bực mình vì quá lằng nhằng ...
Hướng dẫn dùng python isset trong PHP
Hàm isset[] trong PHP mình cũng đã sử dụng khá nhiều lần trong series này rồi nhưng vẫn có một số bạn mail hỏi sự khác nhau giữa hàm isset[] và ...
Hướng dẫn dùng f write trong PHP
Các hàm tạo file, mở - đọc - ghi - đóng fileHàm fopen và fclose - mở và đóng fileHàm fwrite / fputs - ghi nội dung vào fileVí dụ - ghi thêm nội dung vào fileHàm fread, ...
Hướng dẫn dùng loadhtml trong PHP
Trong bài này chúng ta cùng tìm hiểu thêm một cách nữa để lấy dữ liệu từ trang khác thông qua việc sử dụng thư viện. Có một số thư viện hỗ trợ việc ...
Hướng dẫn switch case python
Cafedev chia sẻ cho ace cách tạo một swicth case đơn giản trong python cực đơn giản…Sự thay thế của Switch Case trong Python là gì?Không giống như mọi ngôn ngữ ...
Hướng dẫn dùng si__y trong PHP
[PHP 4, PHP 5, PHP 7, PHP 8]rand — Génère une valeur aléatoireDescriptionrand[]: intrand[int $min, int $max]: int Appelée sans les options min et max, rand[] retourne un nombre pseudoaléatoire ...
Hướng dẫn php tutorial w3schools
Learn PHPPHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.PHP is a widely-used, free, and efficient alternative to competitors such as Microsofts ...
Hướng dẫn dùng php emulator trong PHP
Đã đăng vào thg 10 25, 2020 5:42 SA 5 phút đọc Mỗi một ngôn ngữ lập trình đều có công cụ hỗ trợ lập trình riêng biệt và PHP cũng vậy. Bài viết này sẽ ...
Hướng dẫn dùng jsonkey JavaScript
Nội dung chính của chủ đề:Khái niệm về JSON.JSON.parse[]JSON.stringify[]1. JSON là gì?Nhu cầu trao đổi dữ liệu giữa các nền tảng và công nghệ khác nhau ngày ...
Hướng dẫn permutations in python w3schools
Random Permutations of ElementsA permutation refers to an arrangement of elements. e.g. [3, 2, 1] is a permutation of [1, 2, 3] and vice-versa.The NumPy Random module provides two methods for this: ...
Hướng dẫn dùng themomdom JavaScript
Javascript là một ngôn ngữ được sử dụng trong các trình duyệt Browser nên nó đóng một vai trò khá quan trọng trong các ứng dụng website. Và nhiệm vụ của ...
Hướng dẫn chạy php trên vscode
Visual Studio Code [VS Code] là một trình soạn thảo được phát triển bởi Microsoft, với ưu điểm nhẹ nhàng, dễ sử dụng, dễ dàng mở rộng với nhiều extension ...
Hướng dẫn dùng mongodb w3schools trong PHP
Creating a DatabaseTo create a database in MongoDB, start by creating a MongoClient object, then specify a connection URL with the correct ip address and the name of the database you want to ...
Hướng dẫn all event javascript
Hướng dẫn dùng sevents JavaScriptTrong bài này mình sẽ giới thiệu các sự kiện trong Javascript. Sự kiện hay còn lại là event, là một tác động nào đó lên các ...
Hướng dẫn dùng dom.con JavaScript
Javascript là một ngôn ngữ được sử dụng trong các trình duyệt Browser nên nó đóng một vai trò khá quan trọng trong các ứng dụng website. Và nhiệm vụ của ...
Hướng dẫn dùng ngôn trong PHP
PHP là tên gọi ngôn ngữ lập trình dạng kịch bản [ngôn ngữ script] chuyên dùng để viết thành phần server cho ứng dụng webđược phát triển bởi Rasmus Lerdorf ...
Hướng dẫn dùng schoops JavaScript
Lập trình hàm [Functional programming] không còn là cái tên lạ lẫm trong thời gian gần đây, nhiều lập trình viên đã và đang dần chuyển đổi theo mô hình lập ...
Hướng dẫn dùng geeksforgeeks python python
Python is a general-purpose high-level programming language and is widely used among the developers’ community. Python was mainly developed for emphasis on code readability, and its syntax allows ...