Python 3.2 sample programs
You can import specific names from a module without importing the module as a whole. Here is an example. You can use open function to open a file. In order to write into a file in Python, we need to open it in write 'w' , append 'a' or exclusive creation 'x' mode. Here, we have used with statement to open a file.
This ensures that the file is closed when the block inside with is exited. There are various methods available for this purpose. We can use the read size method to read in size number of data. A directory or folder is a collection of files and sub directories. Python has the os module , which provides many useful methods to work with directories and files. Visit Python Directory to learn more.
Errors that occur at runtime are called exceptions. They occur, for example, when a file we try to open does not exist FileNotFoundError , dividing a number by zero ZeroDivisionError etc. Visit this page to learn about all built-in exceptions in Python.
If exceptions are not handled, an error message is spit out and our program come to a sudden, unexpected halt. In Python, exceptions can be handled using try statement. When exceptions are caught, it's up to you what operator to perform. To learn about catching specific exceptions and finally clause with try statement, visit Python exception handling.
Also, you can create user-defined exceptions in Python. For that, visit Python Custom Exceptions. Everything in Python is an object including integers, floats, functions, classes, and None.
Let's not focus on why everything in Python is an object. For that, visit this page. Rather, this section focuses on creating your own classes and objects. Object is simply a collection of data variables and methods functions that act on data. And, class is a blueprint for the object. As soon as you define a class, a new class object is created with the same name.
This class object allows us to access the different attributes as well as to instantiate new objects of that class. You may have noticed the self parameter in function definition inside the class but, we called the method simply as ob. It still worked. This is because, whenever an object calls its method, the object itself is passed as the first argument. So, ob. This method is automatically called when an object is instantiated.
Visit Python Class and Object to learn more. Inheritance refers to defining a new class with little or no modification to an existing class. Let's take an example:. Notice that we are able to call method of base class displayMammalFeatures from the object of derived class d.
To learn more about inheritance and method overriding, visit Python Inheritance. We also suggest you to check multiple inheritance and operator overloading if you are interested. Iterator in Python is simply an object that can be iterated upon. An object which will return data, one element at a time.
An object is called iterable if we can get an iterator from it. Most of built-in containers in Python like: list, tuple, string etc. To learn more about infinite iterators and how to create custom iterators, visit: Python Iterators.
Learn more about Python Generators. The criteria that must be met to create closure in Python are summarized in the following points. Visit Python closures to learn more about closures and when to use them.
This is also called metaprogramming as a part of the program tries to modify another part of the program at compile time. To learn about decorators in detail, visit Python Decorators. Course Index Explore Programiz. Python if Statement. Python Lists. Dictionaries in Python. Popular Examples Add two numbers. Check prime number. Find the factorial of a number. Print the Fibonacci sequence. Check leap year. Reference Materials Built-in Functions. List Methods.
Dictionary Methods. String Methods. Start Learning Python. Explore Python Examples. Python Exception Handling Using try, except and finally statement. Python Input, Output and Import. Python 3 Tutorial Python is a powerful programming language ideal for scripting and rapid application development.
Python for Beginners If you are a programming newbie, we suggest you to visit: Python Programming - A comprehensive guide on what's Python, how to get started in Python, why you should learn it, and how you can learn it. Python Tutorials - Follow sidebar links one by one. Python Examples - Simple examples for beginners to follow. What's covered in this tutorial? Numbers 3. Strings 3. About Unicode 3.
Lists 3. First Steps Towards Programming 4. More Control Flow Tools 4. The range Function 4. Defining Functions 4. More on Defining Functions 4. Default Argument Values 4. Keyword Arguments 4.
Arbitrary Argument Lists 4. Unpacking Argument Lists 4. Lambda Forms 4. Documentation Strings 4. Function Annotations 4. Intermezzo: Coding Style 5. Data Structures 5.
More on Lists 5. Using Lists as Stacks 5. Using Lists as Queues 5. List Comprehensions 5. Nested List Comprehensions 5. The del statement 5. Tuples and Sequences 5. Sets 5. Dictionaries 5. Looping Techniques 5.
More on Conditions 5. Comparing Sequences and Other Types 6. Modules 6. More on Modules 6. Executing modules as scripts 6. The Module Search Path 6. Standard Modules 6. The dir Function 6. Packages 6. Intra-package References 6. Packages in Multiple Directories 7. Input and Output 7. Fancier Output Formatting 7. Old string formatting 7. Reading and Writing Files 7. Methods of File Objects 7. Since from next article, the series of Python programming examples starts.
But for now, let's see some programs written in Python over here to get more interest in Python. Important - If you feel uncomfortable while reading the code given below, don't worry on it, continue the series from next article. You'll get to know everything about Python, one by one. This is the simplest Python program, that uses print to print the value passed as its argument or inside its braces. In above program, there are two lines of code, the first line is a comment.
Anything starts with hash symbol treated as a comment in Python. The compiler ignores or doesn't executes anything written after. And the second line uses print to output the thing written inside its braces.
For example, the following Python program:. In above program, the second line gets commented using. That is, I've added before second line's code, so that compiler ignores it and this line of code doesn't gets executed. Now the last two lines is used to output the same thing as of previous program but in different way.
That is, instead of directly printing the string anything inside single ' or double " quote treated as a string , I've used a variable named text. The string gets initialized to this variable, and I've printed the value of this variable using print. Note - While putting the variable text inside the braces of print , I've not used double quote "".
Because if I include double quote, then text variable gets treated as a string, instead of a variable. And the output you will get, should be text , not Welcome to the World of Python! Here is the output you'll see:. In above program, using print , a message Enter Your Name: gets printed on output. And using input , I've received the name of user as input and whatever the name entered by user, gets initialized to name variable.
0コメント