Preview only show first 10 pages with watermark. For full document please download

Pylatex

An Introduction to Python and LaTeX Release 0.01 Pramode C.E February 18, 2010 CONTENTS 1 An introduction to the GNU/Linux Operating System 1.1 Installing GNU/Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2 Using GNU/Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.3 The GNU/Linux Command line . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started with Python 2.1 First steps - use Python as a calculator! . . . . . . 2.2 A

   EMBED


Share

Transcript

  An Introduction to Python and LaTeX Release 0.01 Pramode C.E February 18, 2010  CONTENTS 1 An introduction to the GNU/Linux Operating System2 1.1 Installing GNU/Linux. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.2 Using GNU/Linux. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.3 The GNU/Linux Command line. . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2 Getting started with Python5 2.1 First steps - use Python as a calculator!. . . . . . . . . . . . . . . . . . . . . . . 52.2 A few things about the way Python treats numbers. . . . . . . . . . . . . . . . . 62.3 Arithmetic using very large integers. . . . . . . . . . . . . . . . . . . . . . . . . 72.4 Using variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.5 Mistakes made by beginners while using variables. . . . . . . . . . . . . . . . . 82.6 Use of the space character in Python. . . . . . . . . . . . . . . . . . . . . . . . . 92.7 Exiting Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.8 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3 Using Python Lists11 3.1 Elementary list manipulation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 4 Introduction to Numeric Arrays14 4.1 Using Modules in Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144.2 Creating Numeric Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 5 Doing math with Numeric Arrays18 5.1 Plotting a sine curve. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185.2 Plotting the polar rose. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195.3 Labels and title. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205.4 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 6 Defining Functions22 6.1 Function basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226.2 What does ‘return’ do?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266.3 Boolean Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276.4 Using if-else statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 i  6.5 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 7 Functions with side effects31 7.1 Modeling a bank account. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 8 Understanding the while loop34 8.1 A simple while loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348.2 A loop which terminates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358.3 Finding HCF using Euclid’s method. . . . . . . . . . . . . . . . . . . . . . . . . 368.4 Defining a function to compute HCF of two numbers. . . . . . . . . . . . . . . . 37 9 Finding square root by Newton’s iteration38 9.1 Programming as Essay writing. . . . . . . . . . . . . . . . . . . . . . . . . . . . 389.2 Finding square roots. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399.3 Writing a square root finding program. . . . . . . . . . . . . . . . . . . . . . . . 399.4 Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 10 Strings, Tuples and Associative Arrays43 10.1 What is a string?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4310.2 What is a tuple?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4410.3 What is an Associative Array?. . . . . . . . . . . . . . . . . . . . . . . . . . . . 4510.4 Processing lists/tuples with for  loops. . . . . . . . . . . . . . . . . . . . . . . . 45 11 Programming with classes and objects47 11.1 Representing a student. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4711.2 Using classes and objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4811.3 Rewriting better_student  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4911.4 Improving the student class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 12 Exception Handling52 12.1 Syntax Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5212.2 Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5212.3 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 13 Handling data files55 13.1 Writing a message to a file. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5513.2 Writing and reading multiple lines. . . . . . . . . . . . . . . . . . . . . . . . . . 5613.3 Writing and reading numerical data. . . . . . . . . . . . . . . . . . . . . . . . . 56 14 Introduction to the LaTeX Document Preparation System58 14.1 A Simple LaTeX document. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5814.2 What does LaTeX really do?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6014.3 Sections and sub-sections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6114.4 Lists, Quotes and Quotations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6314.5 Handling errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 ii