Introduction to programming with Python

Similar documents
Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

Algorithms and Programming I. Lecture#12 Spring 2015

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

Visual C# Instructor s Manual Table of Contents

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

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Expressions and Variables

BASIC ELEMENTS OF A COMPUTER PROGRAM

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Introduction to computers and Python. Matthieu Choplin

Course Outline. Introduction to java

CS4120/4121/5120/5121 Spring 2016 Xi Language Specification Cornell University Version of May 11, 2016

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

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

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

The current topic: Python. Announcements. Python. Python

Ex: If you use a program to record sales, you will want to remember data:

Program Fundamentals

Chapter 2: Using Data

Topic 1: Introduction

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

ENGR 102 Engineering Lab I - Computation

Chapter 2: Introduction to C++

LECTURE 02 INTRODUCTION TO C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

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

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 2: Data and Expressions

What did we talk about last time? Examples switch statements

Chapter 2: Data and Expressions

Introduction to Java Applications

ENGR 101 Engineering Design Workshop

MIT AITI Python Software Development

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

PHY224 Practical Physics I Python Review Lecture 1 Sept , 2013

CSC Web Programming. Introduction to JavaScript

Python Programming Exercises 1

Chapter 2 Working with Data Types and Operators

Basics of Java Programming

Computer Programming : C++

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Full file at

Declaration and Memory

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

CHAPTER 3 BASIC INSTRUCTION OF C++

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

Full file at

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

MODULE 02: BASIC COMPUTATION IN JAVA

Programming Lecture 3

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

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Python Input, output and variables

Lecture 1. Types, Expressions, & Variables

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Cellular Automata Language (CAL) Language Reference Manual

Information Science 1

Chapter 2: Data and Expressions

Lecture 3. Input, Output and Data Types

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Information Science 1

Objectives. In this chapter, you will:

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

Fundamental of Programming (C)

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Topic 9: Type Checking

Topic 9: Type Checking

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Elementary Programming

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

Variable and Data Type I

CS 106 Introduction to Computer Science I

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

Chapter 2 Getting Started with Python

C++ Programming: From Problem Analysis to Program Design, Third Edition

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

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Outline. Data and Operations. Data Types. Integral Types

6.096 Introduction to C++ January (IAP) 2009

Eng. Mohammed S. Abdualal

egrapher Language Reference Manual

FRAC: Language Reference Manual

Unit 3. Constants and Expressions

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

Introduction to Python Part 1. Brian Gregor Research Computing Services Information Services & Technology

Our Strategy for Learning Fortran 90

Monty Python and the Holy Grail (1975) BBM 101. Introduction to Programming I. Lecture #03 Introduction to Python and Programming, Control Flow

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Transcription:

Introduction to programming with Python Ing. Lelio Campanile 1/61

Main Goal - Introduce you to programming - introduce you to the most essential feature of python programming 2/61

Before to start The name "Python" does not refer to the snake, but it comes from (Monty) Python" 3/61

Why Python? - Software Quality: -Python code is designed to be readable -by design python implements readable syntax 4/61

Why Python? - Productivity: -python code is typically one-third the size of equivalent C++ or Java code 5/61

Why Python? - Program portability: -Python programs run on all major computer platform -support for portable GUI, DB, web system 6/61

Why Python? - Support Library: -python comes with an extensive collection of libraries, The Standard Library -a lot of third-party projects and libraries 7/61

Why Python? Enjoyment let s have fun! Programming in python is fun! 8/61

Is Python a Scripting language? NO! But you can use it to write a script 9/61

Is Python a Scripting language? - sometimes applied in scripting roles - General Purpose programming language that blends procedural, functional and objectoriented paradigms 10/61

11/61 Who does use python?

Python main features - succinct yet readable syntax - Great introspection - Great standard library -Built-in data structures -Language battery included - Interactive shell 12/61

Python is an Interpreted language 13/61

Compiled vs Interpreted Language 14/61

Install python which version? - nowadays python has 2 different branch version 2.x and version 3.x - We ll use the 3.x version - the python 2.x branch ends support in the next year 15/61

Install python - official python distribution - Anaconda distribution 16/61

Anaconda python distribution - Anaconda is a python distribution by Continuum Analytics. - Anaconda is a completely free enterprise-ready Python distribution for large-scale data processing, predictive analytics, and scientific computing. - Apart from that, Anaconda ships with easy-to-use installers for almost every platform, that would drastically reduce the burden of setting up the environment (exp. on Windows) 17/61

Get Anaconda https://www.continuum.io/downloads 18/61

Open the terminal and type python3 19/61

Python Shell - python shell is useful for testing - executes immediately the commands that you type - it doesn't save the code 20/61

IDLE Editor - default editor installed with python - simple and efficient 21/61

For a more serious job - Atom (open source) - Pycharm (free and pro) 22/61

save your file with a.py extension execute it with the command: python3 filename.py 23/61

Variables a variable holds a value message = "Hello python world" print(message) 24/61

Excercise write your first program: store and print your own version of "Hello world" 25/61

26/61

Exercise Store a message in a variable, and then print that message. Store a new message in the same variable, and then print that new message. 27/61

you can change the value of a variable at any point message = "Hello python world" print(message) message = "Hello python" print(message) 28/61

29/61

Naming Rules - Variable can only contain letters, numbers, underscores - Variable names can start with a letter or an underscore, but not with a number - Spaces are not allowed in variable names - we use underscores characters for that - You cannot use Python Keywords as names - Variable names should be descriptive - Can contain Unicode Literals -π = 3.141592653589793 30/61

31/61 your first error: Name Error

What are we missing? message = "Hello python world" print(message) message = "Hello python" print(message)??type declaration?? 32/61

Python Typing Mechanism Dynamic Typing - It is not required to specify the types of variables/functions - it is automatically inferred by operations Strong Typing - Once the type has been inferred, it cannot change without explicit CAST 33/61

Python Typing Mechanism Duck typing If it walks like a Duck, it quacks like a Duck then It's a Duck (inference) 34/61

Numeric Type Integer: 345, 1, -32 Float: 1.2, 1.32, 19e5, 12.1e-6 boolean: True, False 35/61

You can use parenthesis to modify the standard order of operation standard_order = 4+5*2 print(standard_order) 14 my_order = (4+5)*4 print(my_order) 18 36/61

Operations - + adition - - subtraction - * multiplication - / division 37/61

Exercise a = 4 b = 2 c = 1.5 a / b a + b c / b a / b 38/61

39/61

! how to check the type? a = 4 b = 2 c = a / b print(c) 2.0 type(c) <class 'float'> type(a) <class 'int'> type(b) <class 'int'> 40/61

When the operands are of different type, the python interpreter before converts them into the most complex type, and then it executes the calculation. 41/61

Integer operator a = 4 b = 2 c = a // b print(c) 2 type(c) <class 'int'> 42/61

other operations - ** pow operator - % modulus (remainder operator) 43/61

Exercise Given 3 numbers (12, 32, 2.0) write a program that print: - the sum - the multiplication 44/61

Exercise - Given the base = 10 and the height = 15 of a triangle, write a program that prints the area. - Given 2 dates (2019-02-28, 2019-04-20) write a program that prints the days between them - Given the radius = 5 of a circle, write a program that prints the area and the circumference 45/61

Booleans and Logical Tests - True and False are Python keywords - bool type in Python corresponds to int - True = 1 - False = 0 46/61

Logical operators - == equality -!= inequality - > greater than - >= greater than or equal to - < less than - <= less than or equal to - in test if an item is in a list 47/61

Equality 5 == 5 True 3 == 5 False 5 == 5.0 True 'lelio' == 'lelio' True 'lelio == Lelio' False 48/61

Inequality Two items are unequal if they do not have the same value 'alpha'!= 'beta' True 1.0!= 1 False 1!= 2 True 49/61

Other Logical Tests 10>3 10>=10 10>=3 10>=11 True True True False 10<3 3<10 3<=10 10<=10 False True True True 50/61

Logical operators and not or Use them to write more complex logical tests a == b and a!=b False a == b or not a == b True 51/61

True and False python's definition - False: False (bool), None, 0, empty object - True: everything else (number > 0, a not empty object or string, etc.. ) 52/61

Python operator precedence - * % / // - + - - < <= > >= ==!= - not - and - or To force the precedence order, you can use the parentheses 53/61

Comments # this is a comment a = 20 # this is a comment too To Comment your code is a good habit! Make sure that you add a comment to your code when: - you want to remember why you write your code in that manner - when there more than one way to solve a problem write comments short and clear! 54/61

Strings Strings are sets of characters To define a string you can use single or double quotes string = "this is a string" string = 'this is a string too' You can combine single and double when you have a string that contains a quotation: 55/61

[.auto-scale: false] python quote = "Einstein once said:'try not to become a man of success, but rather try to become a man of value.'" 56/61

String Combination first_name = "Lelio" last_name = "Campanile" full_name = first_name + " " + last_name print(full_name) Lelio Campanile 57/61

String Combination string = full_name + " is my full name" print(string) Lelio Campanile is my full name string_multiplication = "=" * 20 print(string_multiplication) ==================== 58/61

Exercise - Find a quote that you like. Store the quote in a variable, with an appropriate introduction such as "Ken Thompson once said, 'One of my most productive days was throwing away 1000 lines of code'". Print the quote. - Store your first name and last name in separate variables, and then combine them to print out your full name. - Use concatenation to make a sentence about you, and store that sentence in a variable. 59/61

Exercise - Store the results of at least 5 different calculations in separate variables. Make sure you use each operation at least once. - Print a series of informative statements, such as "The result of the calculation 5+7 is 12." 60/61

a = 1 b = 1 str_a = str(a) print("this is a cast " + str(a)) this is a cast 1 print("this is a cast " + str_a) this is a cast 1 61/61