Python Class-Lesson1 Instructor: Yao

Similar documents
Contents. Lezione 3. Reference sources. Contents

Advanced Algorithms and Computational Models (module A)

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

COMP1730/COMP6730 Programming for Scientists. Strings

Part III Appendices 165

Python Day 3 11/28/16

Lecture 3. Input, Output and Data Types

Fundamentals of Python: First Programs. Chapter 4: Strings (Indexing, Slicing, and Methods)

Introduction to Bioinformatics

CIS192: Python Programming

Variables, expressions and statements

>>> * *(25**0.16) *10*(25**0.16)

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

CIS192 Python Programming. Robert Rand. August 27, 2015

CSCE 110 Programming I

CS 115 Lecture 4. More Python; testing software. Neil Moore

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CSC Web Programming. Introduction to JavaScript

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Programming Languages & Translators. XML Document Manipulation Language (XDML) Language Reference Manual

CS 102 Lab 3 Fall 2012

Expressions and Casting

Computing with Numbers

Introduction to Python

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

Full file at C How to Program, 6/e Multiple Choice Test Bank

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Getting Started Values, Expressions, and Statements CS GMU

4. Inputting data or messages to a function is called passing data to the function.

Introduction to Python Programming

Strings. Genome 373 Genomic Informatics Elhanan Borenstein

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Lesson 4: Type Conversion, Mutability, Sequence Indexing. Fundamentals of Text Processing for Linguists Na-Rae Han

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Table of Contents Date(s) Title/Topic Page #s. Abstraction

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Chapter 17. Fundamental Concepts Expressed in JavaScript

Basics of Java Programming

UNIVERSITÀ DI PADOVA. < 2014 March >

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Python Tutorial. Release Guido van Rossum and the Python development team. December 10, Python Software Foundation

Working with Strings. Husni. "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Lecture 4: Basic I/O

Accelerating Information Technology Innovation

Language Reference Manual

Full file at

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Sequence of Characters. Non-printing Characters. And Then There Is """ """ Subset of UTF-8. String Representation 6/5/2018.

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Programming in Python 3

Fundamentals of Python: First Programs. Chapter 4: Strings and Text Files

PYTHON. Values and Variables

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Java Fall 2018 Margaret Reid-Miller

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Full file at

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

Practical Programming, Third Edition

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

"Hello" " This " + "is String " + "concatenation"

Computer Programming : C++

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Scripting Languages. Diana Trandabăț

Programming for Engineers Introduction to C

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 2 Working with Data Types and Operators

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Programming Fundamentals and Python

Some material adapted from Upenn cmpe391 slides and other sources

Visual C# Instructor s Manual Table of Contents

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

Chapter 2 Getting Started with Python

Chapter 2: Using Data

Algorithms and Programming I. Lecture#12 Spring 2015

Cosmology with python: Beginner to Advanced in one week. Tiago Batalha de Castro

Topic 1: Introduction

Phys Techniques of Radio Astronomy Part 1: Python Programming

Maciej Sobieraj. Lecture 1

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Introduction to Programming

Oliver BONHAM-CARTER. Fall 2018 Week 2

Computer Components. Software{ User Programs. Operating System. Hardware

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Transcription:

Python Class-Lesson1 Instructor: Yao

What is Python? Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance.

Why Python? It s easier to pick up for beginners who want to get into the door of the code world It s multifunctional. It could be utilized with C/C++ to create computer games, manipulate/digest/process data It is trendy in the financial and healthcare industry.

Python Installation Download website: https://www.python.org There are two versions: Python 3.5.2 Python 2.7.12

Python Interpreter The Python interpreter is usually installed as /usr/local/bin/python on those machines where it is available; putting /usr/local/bin in your Unix shell s search path makes it possible to start it by typing the command

Using Python as a calculator The interpreter acts as a simple calculator: you can type an expression at it and it will write the value. Expression syntax is straightforward. The return type of a division (/) operation depends on its operands. If both operands are of type int, floor division is performed and an int is returned. If either operand is a float, classic division is performed and a float is returned. The // operator is also provided for doing floor division no matter what the operands are. The remainder can be calculated with the % operator:

Using Python as a calculator With Python, it is possible to use the ** operator to calculate powers The equal sign (=) is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt: With Python, it is possible to use the ** operator to calculate powers :

Using Python as a calculator The equal sign (=) is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt: If a variable is not defined (assigned a value), trying to use it will give you an error: There is full support for floating point; operators with mixed type operands convert the integer operand to floating point:

Using Python as a calculator In interactive mode, the last printed expression is assigned to the variable _

Strings Besides numbers, Python can also manipulate strings, which can be expressed in several ways. They can be enclosed in single quotes ('...') or double quotes ("...") with the same result. he output string is enclosed in quotes and special characters are escaped with backslashes

Strings If you don t want characters prefaced by \ to be interpreted as special characters, you can use raw strings by adding an r before the first quote String literals can span multiple lines. One way is using triplequotes: """...""" or '''...''' produces the following output (note that the initial newline is not included)

Strings Strings can be concatenated (glued together) with the + operator, and repeated with * Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated This only works with two literals though, not with variables or expressions

Strings If you want to concatenate variables or a variable and a literal, use + This feature is particularly useful when you want to break long strings Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one:

Strings Indices may also be negative numbers, to start counting from the right In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain a substring: Note how the start is always included, and the end always excluded. This makes sure that s[:i] + s[i:] is always equal to s:

Strings Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example:

Strings Python strings cannot be changed they are immutable. Therefore, assigning to an indexed position in the string results in an error If you need a different string, you should create a new one: The built-in function len() returns the length of a string: