PROGRAMMING FUNDAMENTALS

Similar documents
PROGRAMMING FUNDAMENTALS

PROGRAMMING FUNDAMENTALS

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

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

Variables, expressions and statements

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

ENGR 101 Engineering Design Workshop

Variable and Data Type I

Variable and Data Type I

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Algorithms and Programming I. Lecture#12 Spring 2015

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

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

Algorithms and Data Structures

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

CMSC 201 Computer Science I for Majors

Getting Started with Python

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Fundamentals of Programming (Python) Getting Started with Programming

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

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

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

CSCE 110 Programming I

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

Variables, Expressions, and Statements

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

Chapter 2 Working with Data Types and Operators

About Variables in Python F E B 1 1 T H

Full file at

And Parallelism. Parallelism in Prolog. OR Parallelism

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

Individual research task. You should all have completed the research task set last week. Please make sure you hand it in today.

Python notes from codecademy. Intro: Python can be used to create web apps, games, even a search engine.

How to Think Like a Computer Scientist: Learning with Python 3 Documentation

The float type and more on variables FEB 6 TH 2012

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

CSCE 120: Learning To Code

Chapter 2 Writing Simple Programs

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Jython. secondary. memory

Python Programming Exercises 1

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

Programming with Python

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

1/11/2010 Topic 2: Introduction to Programming 1 1

CEN 414 Java Programming

DaMPL. Language Reference Manual. Henrique Grando

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

Introduction to Java Applications

Topic 2: Introduction to Programming

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Fundamentals of Programming

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

Al al-bayt University Prince Hussein Bin Abdullah College for Information Technology Computer Science Department

Fundamental of Programming (C)

Table of Contents EVALUATION COPY

Basic Syntax - First Program 1

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

WELCOME! (download slides and.py files and follow along!) LECTURE 1

REVIEW. The C++ Programming Language. CS 151 Review #2

DATA AND ABSTRACTION. Today you will learn : How to work with variables How to break a program down Good program design

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

COMP 110 Introduction to Programming. What did we discuss?

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

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

Lesson 3: Basic Programming Concepts

Full file at

Python Unit

Chapter 17. Fundamental Concepts Expressed in JavaScript

Introduction to Problem Solving and Programming in Python.

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Expressions and Variables

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Language Reference Manual simplicity

VARIABLES & ASSIGNMENTS

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

FIT9133 Module 1: Introduction to Algorithms and Python Programming Basics

Unit 3. Operators. School of Science and Technology INTRODUCTION

CSCI 121: Anatomy of a Python Script

SI Networked Computing: Storage, Communication, and Processing, Winter 2009

Beyond Blocks: Python Session #1

CS 112: Intro to Comp Prog

Operators. Java operators are classified into three categories:

Short, Unique and Mysterious

Lecture 2: Python Arithmetic

Introduction to the C++ Programming Language

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

VBScript: Math Functions

egrapher Language Reference Manual

L-System Fractal Generator: Language Reference Manual

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

SCoLang - Language Reference Manual

CS 1301 Exam 1 Answers Fall 2009

CSC326 Python Imperative Core (Lec 2)

Python Class-Lesson1 Instructor: Yao

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

Transcription:

PROGRAMMING FUNDAMENTALS VARIABLES, EXPRESSIONS AND STATEMENTS João Correia Lopes INESC TEC, FEUP 27 September 2018 FPRO/MIEIC/2018-19 27/09/2018 1 / 21

INTRODUCTION GOALS By the end of this class, the student should be able to: Describe and distinguish the concepts of variable, location, value, type Identify the Python reserved words Describe the concepts of statement and expression Identify some of the Python operands and their precedence Use operators with suitable operands Describe the Python cast operations and use them Describe how to get input, at runtime, from an user of the program FPRO/MIEIC/2018-19 27/09/2018 2 / 21

INTRODUCTION BIBLIOGRAPHY Peter Wentworth, Jeffrey Elkner, Allen B. Downey, and Chris Meyers, How to Think Like a Computer Scientist Learning with Python 3, 2018 (Chapter 2) [PDF] Brad Miller and David Ranum, Learning with Python: Interactive Edition. Based on material by Jeffrey Elkner, Allen B. Downey, and Chris Meyers (Chapter 2) [HTML] FPRO/MIEIC/2018-19 27/09/2018 3 / 21

INTRODUCTION TIPS This is a script and some illustrations used in the class, NOT a replacement for reading the bibliography listed in the class sheet Students are responsible for anything that transpires during a class therefore if you re not in a class, you should get notes from someone else (not the instructor) David Mayer The best thing to do is to read carefully and understand the documentation published in the Content wiki (or else ask in the class) We will be using Moodle as the primary means of communication FPRO/MIEIC/2018-19 27/09/2018 4 / 21

INTRODUCTION CONTENTS 1 SIMPLE PYTHON DATA 2.1 Values and data types 2.2 Variables 2.3 Variable names and keywords 2.4 Statements 2.5 Evaluating expressions 2.6 Operators and operands 2.7 Type converter functions 2.8 Order of operations 2.9 Operations on strings 2.10 Input 2.11 Composition 2.12 The modulus operator FPRO/MIEIC/2018-19 27/09/2018 5 / 21

PYTHON 1 #!/usr/bin/env python3 2 3 import datetime 4 now = datetime.datetime.now() 5 6 print() 7 print("current date and time using str method of datetime object:") 8 print() 9 print(str(now)) 10 11 print() 12 print("current date and time using instance attributes:") 13 print() 14 print("current year: %d" % now.year) 15 print("current month: %d" % now.month) 16 print("current day: %d" % now.day) 17 print("current hour: %d" % now.hour) 18 print("current minute: %d" % now.minute) 19 print("current second: %d" % now.second) 20 print("current microsecond: %d" % now.microsecond) 21 22 print() 23 print("current date and time using strftime:") 24 print(now.strftime("%y-%m-%d %H:%M")) https://github.com/fpro-admin/lectures/blob/master/01/basics.py FPRO/MIEIC/2018-19 27/09/2018 6 / 21

2.1 VALUES AND DATA TYPES VALUES AND DATA TYPES A value is one of the fundamental things that a program manipulates Values are classified into different classes, or data types type() is a function that tell us the type of a value FPRO/MIEIC/2018-19 27/09/2018 7 / 21

2.2 VARIABLES VARIABLES A variable is a name that refers to a value The assignment statement gives a value to a variable The assignment statement binds a name, on the left-hand side of the operator, to a value, on the right-hand side Later, one can assign a different value to the same variable (this is different from maths!) The assignment token, =, should not be confused with the equals token, == Visualise a state snapshot FPRO/MIEIC/2018-19 27/09/2018 8 / 21

2.3 VARIABLE NAMES AND KEYWORDS VARIABLE NAMES Variable names can be arbitrarily long They can contain both letters and digits, but they have to begin with a letter or an underscore It is legal to use uppercase letters, but it is not done (by convention) Names should be meaningful to the human readers (not to be confused with meaningful to the computer ) FPRO/MIEIC/2018-19 27/09/2018 9 / 21

2.3 VARIABLE NAMES AND KEYWORDS KEYWORDS Keywords define the language s syntax rules and structure They cannot be used as variable names and as assert break class continue def del elif else except exec finally for from global if import in is lambda nonlocal not or pass raise return try while with yield True False None FPRO/MIEIC/2018-19 27/09/2018 10 / 21

2.4 STATEMENTS STATEMENTS A statement is an instruction that the Python interpreter can execute Statements don t produce any result Further to the assignment statement, there are others (while statements, for statements, if statements, import statements) FPRO/MIEIC/2018-19 27/09/2018 11 / 21

2.5 EVALUATING EXPRESSIONS EVALUATING EXPRESSIONS An expression is a combination of values, variables, operators, and calls to functions The Python interpreter evaluates expressions and displays its result (a value) A value all by itself is a simple expression, and so is a variable FPRO/MIEIC/2018-19 27/09/2018 12 / 21

2.6 OPERATORS AND OPERANDS OPERATORS AND OPERANDS Operators are special tokens that represent computations like addition, multiplication and division The values the operator uses are called operands When a variable name appears in the place of an operand, it is replaced with its value before the operation is performed Operations in Python (+, -, /) mean what they mean in mathematics Asterisk ( * ) is the token for multiplication, and ** is the token for exponentiation FPRO/MIEIC/2018-19 27/09/2018 13 / 21

2.6 OPERATORS AND OPERANDS TIME FOR KAHOOT! Not now! https://kahoot.com/ FPRO/MIEIC/2018-19 27/09/2018 14 / 21

2.7 TYPE CONVERTER FUNCTIONS TYPE CONVERTER FUNCTIONS Type converter functions int(), float() and str() will (attempt to) convert their arguments into types int, float and str respectively FPRO/MIEIC/2018-19 27/09/2018 15 / 21

2.8 ORDER OF OPERATIONS ORDER OF OPERATIONS When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence Python follows the same precedence rules for its mathematical operators that mathematics does (PEMDAS) Operators with the same precedence are evaluated from left-to-right (left-associative) An exception to the left-to-right left-associative rule is the exponentiation operator ** FPRO/MIEIC/2018-19 27/09/2018 16 / 21

2.9 OPERATIONS ON STRINGS OPERATIONS ON STRINGS One cannot perform mathematical operations on strings, even if the strings look like numbers The + operator represents concatenation, not addition The * operator also works on strings; it performs repetition FPRO/MIEIC/2018-19 27/09/2018 17 / 21

2.10 INPUT INPUT There is a built-in function in Python, input(), for getting input from the user The user of the program can enter the input and click OK The input() function always return a string (without the new-line) FPRO/MIEIC/2018-19 27/09/2018 18 / 21

2.11 COMPOSITION COMPOSITION One of the most useful features of programming languages is their ability to take small building blocks and compose them into larger chunks. Let s do four-step program that asks the user to input a value for the radius of a circle, and then computes the area of the circle from the formula πr 2 TIP: try to make code as simple as you can for the human to follow https://github.com/fpro-admin/lectures/blob/master/02/area.py FPRO/MIEIC/2018-19 27/09/2018 19 / 21

2.12 THE MODULUS OPERATOR THE MODULUS OPERATOR The modulus operator works on integers (and integer expressions) and gives the remainder when the first number is divided by the second In Python, the modulus operator is a percent sign (%) It has the same precedence as the multiplication operator https://github.com/fpro-admin/lectures/blob/master/02/remainder.py FPRO/MIEIC/2018-19 27/09/2018 20 / 21

2.12 THE MODULUS OPERATOR EXERCISES Moodle activity at: LE02: Variables, expressions and assignments FPRO/MIEIC/2018-19 27/09/2018 21 / 21