Intro to CS Nov 2, 2015.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

COMPSCI 101 Principles of Programming Lecture 6 – Getting user input, converting between types, generating random numbers.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Python November 14, Unit 7. Python Hello world, in class.
Munster Programming Training
Computer Science 111 Fundamentals of Programming I Number Systems.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Type Conversions Implicit Conversion Explicit Conversion.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
C ONTINUING C++ Precedence Rules, Numeric Types, More Operator Symbols, and Keyboard Input Prompts.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
Python Conditionals chapter 5
Introduction to Java Primitive Types Operators Basic input and output.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Variables, Types, Expressions Intro2CS – week 1b 1.
A: A: double “4” A: “34” 4.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Course A201: Introduction to Programming 09/09/2010.
Input, Output and Variables GCSE Computer Science – Python.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
CompSci 230 S Programming Techniques
Scientific Programming in Python -- Cheat Sheet
Numbers and arithmetic
Fundamentals of Programming I Overview of Programming
Numbers and Arithmetic
Python unit_4 review Tue/Wed, Dec 1-2
Topics Designing a Program Input, Processing, and Output
Topic: Python’s building blocks -> Statements
Data Types and Conversions, Input from the Keyboard
COMPSCI 107 Computer Science Fundamentals
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Design & Technology Grade 7 Python
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
Useful String Methods Cont…
Introduction to Programming
CMSC201 Computer Science I for Majors Lecture 03 – Operators
And now for something completely different . . .
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Introduction to C++ Programming
Console input.
Fundamentals of Programming I Number Systems
Chapter 2: Basic Elements of Java
Reading Input from the Keyboard
Input from the Console This video shows how to do CONSOLE input.
Topics Designing a Program Input, Processing, and Output
CS150 Introduction to Computer Science 1
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
CS150 Introduction to Computer Science 1
Variables In today’s lesson we will look at: what a variable is
Topics Designing a Program Input, Processing, and Output
Arithmetic Operations
Nate Brunelle Today: Conditional Decision Statements
Topics Designing a Program Input, Processing, and Output
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
General Computer Science for Engineers CISC 106 Lecture 03
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Data Types and Maths Programming Guides.
Text Copyright (c) 2017 by Dr. E. Horvath
Nate Brunelle Today: Strings, Type Casting
CS 1111 Introduction to Programming Spring 2019
Data Types and Expressions
Introduction to Python
Python Creating a calculator.
Presentation transcript:

Intro to CS Nov 2, 2015

Today Python data types and operations Casting data types Built-in functions for keyboard input

Four Python data types int: age = 20 float: pi = 3.14159 String: name = ‘Alice’ Boolean: isCold = True

Doing arithmetic + Addition - Subtraction * Multiplication / Division >>> 5/2 = 2.5 % Modulus How to determine an integer is even or odd? // Floor division >>> 5//2 = 2 ** Exponent >>> 5**3 = 125

Data types and operators Numeric types can use all arithmetic operators If mixing string and numeric, be careful !!! print(3+5.5) # ok. Output: 8.5 print(3+’5.5’) # error: unsupported type print(‘3’+’5.5’) # ok. String operation # output: 35.5 print( ‘Hi’*5) #ok. Output: HiHiHiHiHi print(‘Hi’+5) # error.

Built-in functions for type conversion Type conversion is also called type casting int(x): convert x to a integer example: int(‘5’) float(x): convert x to a decimal example: float(‘2.8’) str(x): convert x to a string example: str(5)

Type conversion example print(3+’5’) #error print(3+int(‘5’)) #ok print(‘Hi’ + 5) #error print(‘Hi’ + str(5)) #ok. Output: Hi5

Built-in function for keyboard input Program stops, waits for the user to type something. After user types something and presses Enter, the program resumes and returns what the user typed as a string. Example print(“Please enter your username: “) x=raw_input() print(“You just entered: “, x)

Built-in function for keyboard input (cont’) The Zen of Python: simple is better than complex print(“Please enter your username: “) x=raw_input() print(“You just entered: “, x) Can be rewritten as: x=raw_input(“Please enter your username: “)

Built-in function for keyboard input (cont’) Question: What is the total price to buy 5 books if each costs $10? print("One book costs $10") num_books=input(“How many books you will buy: “) price = num_books*10 print(“Your total price = “, price, “dollars”)

Built-in function for keyboard input (cont’) Question: input() always returns a string. What if I need a number? x=raw_input(“How many books you will buy: “) num_books = int(x) #use int() to convert # string to int price = num_books*10 print(“Your total price = “, price)

Summary Python data types: int, float, string, boolean Arithmetic operations Casting data types: int(), float(), str() keyboard input()