Chapter 1

Chapter 1

CHAPTER ONE Introduction to Python Python is a cross platform object-oriented programming language created by Guido van Rossum in 1991 runs on all major hardware platforms and operating systems, including Windows, Linux/UNIX, and Macintosh can also be used in procedural and functional styles

currently has three implementations, known as CPython, Jython, and IronPython Cpython is the most widely used implementation Python in the job market is the most popular programming language for 2017 Key features of python

Python is easy to learn. It has easier to read syntax It uses white space for indentation Python is free has a large number of libraries included Can be integrated with other languages (mysql) Python is an interpreted language good for web development (also networking, gaming) provides for efficient memory management (garbage collection)

supports exception handling Installing python Depends on OS Versions compatible with the textbook are Python 3.4 and PyQt4 Do not download the versions on the Unisa software disk For Python 3.4 go to : https://www.python.org/downloads/release/python-340/ For PyQt4 got to : https://sourceforge.net/projects/pyqt/files/PyQt4/PyQt-4.11.4/ Can watch video here : https://www.youtube.com/watch?v=O_xBNq833bM

Interacting with python Can work with Python using: Command Line Mode select Start > All Programs > Python 3.** IIDLE IDE select Start > All Programs > Python 3.** > IDLE Interacting with python In Command Line mode

you type Python instructions one line at a time and a history of the commands given is maintained. You can use the up and down arrows, as well as the Home,End, Page Up, and Page Down keys, to scroll through the commands used previously. Since whatever you type in command line mode is not saved, it is better to save code in a file and then execute it. Interacting with python

IDLE (Integrated Development Environment) IDLE combines an interactive interpreter with code editing, debugging tools, and several specialized browsers/viewers built into it. provides automatic indentation and colors to the code based on Python syntax types, making your program more readable. Also, you can navigate to the previously given commands that are in the buffer using the mouse, arrow keys, or Page Up and Page Down keys.

Data types in python Summary The operations that are applicable on an object depend on its data type Data types in python Summary of data types Integers: Integers are 32 bits long ( 2,147,483,648 to 2,147,483,647). Long Integers: It has unlimited precision, subject to the memory limitations of the computer.

Floating Point Numbers: Floating-point numbers are also known as doubleprecision numbers and use 64 bits. Boolean: It can hold only one of two possible values: True or False. Data types in python Summary of data types Complex Number: A complex number has a real and an imaginary component, both represented by float types in Python. An imaginary number is a multiple of the square root of minus one, and is denoted by j. For instance, 2+3j is a complex number, where 3 is the

imaginary component and is equal to 3 1. Strings: Sequences of Unicode characters. Lists: Ordered sequences of values. Tuples: Ordered, immutable sequences of values. Sets: Unordered collections of values. Dictionaries: Unordered collections of key-value pairs. Basic elements of a python program

Literals, variables and keywords Every program consists of certain basic elements, a collection of literals, variables, and keywords. Basic elements of a python program Literals Literal - a number or string that does not change

Basic elements of a python program Variables Variable - used for storing data in a program. To set a variable, you choose a name for your variable, and then use the equals sign followed by the data that it stores. Variables can be letters, numbers, or words literal

variable Num1 = 15 Basic elements of a python program Keywords Keywords - are identifiers that Python reserves for special use. Keywords contain lowercase letters only. You cannot use keywords as regular identifiers

Basic elements of a python program Comments Comments are the lines that are for documentation purposes and are ignored by the interpreter. The comments inform the reader what the program is all about. A comment begins by a hash sign (#). All characters after the # and up to the physical line end are part of the comment. For Example.

# This program computes area of rectangle a=b+c # values of b and c are added and stored in a Basic elements of a python program Continuation lines A physical line is a line that you see in a program. A logical line is a single statement in Python terms. In Python, the end of a physical line marks the end of most statements, unlike in other languages, where usually a

semicolon (;) is used to mark the end of statements. When a statement is too long to fit on a single line, you can join two adjacent physical lines into a logical line by ensuring that the first physical line has no comment and ends with a backslash (\). Besides this, Python also joins adjacent lines into one logical line if an open parenthesis ( ( ), bracket ( [ ), or brace ( { ) is not closed. The lines after the first one in a logical line are known as continuation lines. The indentation is not applied to continuation lines but only to the first physical line of each logical line.

Basic elements of a python program Printing For printing messages and results of computations, the print() function is used with the following syntax: print(message,variable list) where message is the text string enclosed either in single or double quotes The variable list may be one or more variables containing the result of computation

Basic elements of a python program Printing Concatenation Joining 2 strings Using + sign print('Hello World!'+'It might rain today') output - Hello World!It might rain today (Note no space between) Using , print('Hello World!', 'It might rain today')

output - Hello World! It might rain today (space between) Basic elements of a python program Printing Using end= end= suppresses printing on a new line It adds a space rather print on a new line print(I love python,, end= ) print(the language, not the snake)

Output - I love python, the language, not the snake Basic elements of a python program Printing Using (\) The \ operator adds a space rather print on a new line print('Hello World! \ It might rain today. \ Tomorrow is Sunday.')

Output - Hello World! It might rain today. Tomorrow is Sunday. Basic elements of a python program Printing Using format codes - (%) used for substituting values in the variables at the desired place in the message Notes

Recently Viewed Presentations

  • April 18, 2017

    April 18, 2017

    7.10B Describe how biodiversity contributes to the sustainability of an ecosystem. 6 th Grade DOL I will complete written STAAR type questions over biodiversity via a DOL quiz.
  • México, AmÉrica Central, y el caribe

    México, AmÉrica Central, y el caribe

    Standards: Michigan World Language Standards. 2.2.N.G.a Identify countries, their capital and major cities in which the language is . spoken. National Technology (ISTE-NETS.S) Standards. 5.b Exhibit a positive attitude toward using technology that supports collaboration, learning, and productivity
  • Repairs for RPD Occasionally needed. How to avoid??

    Repairs for RPD Occasionally needed. How to avoid??

    Otherwise modification to the exiting tooth should be done the same as during any other mouth preparations, with proximal recontouring, preparation of an adequate occlusal rest seat, and any reduction in tooth, contours necessary to accommodate retentive and stabilizing components.
  • Chapter 1 Structure and Bonding - faculty.swosu.edu

    Chapter 1 Structure and Bonding - faculty.swosu.edu

    Substitution Reactions of Square Planar Complexes Kinetics Rate = k1[Complex] + k2[Complex][Y] The k2 term is the normal Assoctiative (A) mechanism The k1 term involves solvent association followed by fast exchange with Y Solvent effects [Solvent] = large and constant...
  • Vernier Science: The Greenhouse effect

    Vernier Science: The Greenhouse effect

    The probe tips should be 3 cm from the ruler ends and the tape should not cover the probe tips. Connect the temperature probes-as shown on the next slide. Start the Vernierdata-collection program and open the file "03 The Greenhouse...
  • This is a photographic template  your photograph should

    This is a photographic template your photograph should

    Introduction - Rachael Thompson. IT Manager of EDI Integration - Eaton Corporation. Team responsible for EDI implementations and support activities for all Industrial Sector facilities (Hydraulic, Aerospace, and Vehicle) around the world which utilize MFG/PRO
  • Snímek 1 - Masaryk University

    Snímek 1 - Masaryk University

    evoluce začíná už v tomto hmotném, předživotním stavu ve formě systémů s mechanickou strukturou a interakcí v těchto systémech předobrazy pohybů se zpětnou vazbou - kvaziregulace zdrojem potencionální fyzikální informace je strukturovanost, uspořádanost a organizovanost hmotného objektu První byla informace...
  • CSCI6370: Topics in Computer Science Advanced Topics in ...

    CSCI6370: Topics in Computer Science Advanced Topics in ...

    The Stack ADT. The Queue ADT. Abstract Data Types (ADTs) Definition: An abstract data type(ADT) is a set of objects together with a set of operations for processing the objects. Examples: Objects such as lists, sets, and graphs, along with...