Python Basics. 1 of 7 9/5/2018, 8:51 AM. txt1 = "ada lovelace, english mathematician and writer" print(txt1)

Size: px
Start display at page:

Download "Python Basics. 1 of 7 9/5/2018, 8:51 AM. txt1 = "ada lovelace, english mathematician and writer" print(txt1)"

Transcription

1 1 of 7 9/5/2018, 8:51 AM Python Basics In [1]: txt1 = "ada lovelace, english mathematician and writer" print(txt1) ada lovelace, english mathematician and writer Here txt1 is a variable and "ada lovelace, english mathematician and writer" is a string. The rules for naming variables are: Variable names can only contain letters, numbers, and underscores. They can not start with a number. Spaces are not allowed in variable names. Do not use Python keywords and (built-in) function names as variable names. Examples: Good: message1, text_1, first_name, string1, length_of_txt1, _1txt. Bad/Wrong: 1message, str, text 1, global, 1txt. In [2]: # To obtain the keywords for your version of Python (e.g., 3.5) import keyword print(keyword.kwlist) print(len(keyword.kwlist)) ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', ' def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'retur n', 'try', 'while', 'with', 'yield'] 33

2 2 of 7 9/5/2018, 8:51 AM In [3]: # Examples of built-in functions print(len(txt1)) # length of string in txt1 print(hex(ord('a'))) # converting ascii character to decimal to hex print(chr(97)) # lowercase(a) is hex 61 = decimal 97 print(chr(int('6d',16))) # converting hex to decimal to ascii character 46 0x41 a m Simple Object Types None (null object) Boolean (True, False) Integer Float Complex String

3 3 of 7 9/5/2018, 8:51 AM In [4]: n1 = 91 n2 = n3 = float(n1) n4 = int(n2) n5 = round(n2) print('n1={}, type(n1)={}'.format(n1,type(n1))) print('n2={}, type(n2)={}'.format(n2,type(n2))) uments to include print('n3={}, type(n3)={}'.format(n3,type(n3))) print('n4={}, type(n4)={}'.format(n4,type(n4))) print('n5={}, type(n5)={}'.format(n5,type(n5))) # formatting print output # {} are placeholders for the arg n1=91, type(n1)=<class 'int'> n2= , type(n2)=<class 'float'> n3=91.0, type(n3)=<class 'float'> n4=2, type(n4)=<class 'int'> n5=3, type(n5)=<class 'int'> In [5]: n6 = complex(1,2) print('n6={}, type(n6)={}'.format(n6,type(n6))) print('n6*={}, type(n6*)={}'.format(n6.conjugate(),type(n6.conjugate()))) ate is a method print('re[n6]={}, type(re[n6]={})'.format(n6.real,type(n6.real))) s a property print('im[n6]={}, type(im[n6]={})'.format(n6.imag,type(n6.imag))) s a property print('abs(n6)={}, type(abs(n6)={})'.format(abs(n6),type(abs(n6)))) n6=(1+2j), type(n6)=<class 'complex'> n6*=(1-2j), type(n6*)=<class 'complex'> Re[n6]=1.0, type(re[n6]=<class 'float'>) Im[n6]=2.0, type(im[n6]=<class 'float'>) abs(n6)= , type(abs(n6)=<class 'float'>) # conjug # real i # imag i In [6]: n0 = None print('n0={}, type(n0)={}'.format(n0,type(n0))) print('n1==n2: {}, type(n1==n2)={}'.format(n1==n2,type(n1==n2))) print('n1!=n0: {}, type(n1!=n0)={}'.format(n1!=n0,type(n1!=n0))) print('n1==n1*: {}, type(n1==n1*)={}'.format(n1==n1.conjugate(),type(n1==n1.conjuga te()))) print('n6==n6*: {}, type(n6==n6*)={}'.format(n6==n6.conjugate(),type(n6==n6.conjuga te()))) n0=none, type(n0)=<class 'NoneType'> n1==n2: False, type(n1==n2)=<class 'bool'> n1!=n0: True, type(n1!=n0)=<class 'bool'> n1==n1*: True, type(n1==n1*)=<class 'bool'> n6==n6*: False, type(n6==n6*)=<class 'bool'> In [7]: Out[7]: n0 = None type(n0) NoneType Strings are sequences of characters inside a pair of single (') or double (") quotes. One of the simplest things one can do with strings is to change the case of the words in a string using the upper(), lower(), or title() methods. A method is an action that can be performed on some data, for instance 'A'.lower() yields a. Methods are followed by parentheses because they often need additional parameters. In general arithmetic operations are not defined for strings, but strings can be concatenated using the + operator.

4 4 of 7 9/5/2018, 8:51 AM In [8]: print('a'.lower()) print(txt1.upper()) print(txt1.title()) txt2 = "is considered to be the first computer programmer." print(txt1 + ', ' + txt2) # string concatenation a ADA LOVELACE, ENGLISH MATHEMATICIAN AND WRITER Ada Lovelace, English Mathematician And Writer ada lovelace, english mathematician and writer, is considered to be the first co mputer programmer. In [9]: txt3 = "Ada Lovelace's" + ' mother was called "The Princess of Parallelograms"' print(txt3) # using both single and double quotes in strings Ada Lovelace's mother was called "The Princess of Parallelograms" Jupyter Notebooks are quite versatile. They can have "Markdown" cells with html, LaTeX (, ) and images: In [10]: # Accessing individual characters in strings # Indexing starts at 0 txt4 = txt3[0] + txt3[8] + txt3[9] + txt3[13] print(txt4) print(type(txt4)) Alas <class 'str'> More general built-in object types in Python that can contain sequences or collections of data are tuples, lists, sets, and dictionaries. The values in a list are called elements or items. They can be of any type, including lists (called nested lists). The elements in a list can change throughout the life of a program. Tuples are very similar to lists, but they are immutable, that is, once defined the elements in the tuples can be accessed, but not altered. A set in Python is a data structure that is equivalent to sets in mathematics. It can contain any immutable data type like numbers, strings or tuples as elements and the elements themselves are mutable, but each specific element occurs only once. Dictionaries can store an almost limitless amount of easily retrievable information in the form of key-value pairs. A key-value pair is a set of values associated with each other. When a key is provided, Python returns the value associated with that key. Dictionaries are dynamic structures and key-value pairs can be added, modified, or deleted at any time.

5 5 of 7 9/5/2018, 8:51 AM In [11]: # Example lists L0 = [] # empty list L1 = [1, 2, 4, 8, 16, 32, 64, 128] # numeric list L2 = list(range(5)) # range: from 0 up to but not including 5 L3 = [1, 'two', [3, 4]] # mixed elements print('l0={}, type(l0)={}'.format(l0,type(l0))) print('l1={}, type(l1)={}'.format(l1,type(l1))) print('l2={}, type(l2)={}'.format(l2,type(l2))) print('l3={}, type(l3)={}'.format(l3,type(l3))) L0=[], type(l0)=<class 'list'> L1=[1, 2, 4, 8, 16, 32, 64, 128], type(l1)=<class 'list'> L2=[0, 1, 2, 3, 4], type(l2)=<class 'list'> L3=[1, 'two', [3, 4]], type(l3)=<class 'list'> In [12]: # Accessing list elements L1a = L1[4] L1b = L1[5:8] # subset of list L2a = L2[-1] # last element in list L3a = L3[1] L3b = L3[2][0] # first element in nested list print('l1a={}, type(l1a)={}'.format(l1a,type(l1a))) print('l1b={}, type(l1b)={}'.format(l1b,type(l1b))) print('l2a={}, type(l2a)={}'.format(l2a,type(l2a))) print('l3a={}, type(l3a)={}'.format(l3a,type(l3a))) print('l3b={}, type(l3b)={}'.format(l3b,type(l3b))) L1a=16, type(l1a)=<class 'int'> L1b=[32, 64, 128], type(l1b)=<class 'list'> L2a=4, type(l2a)=<class 'int'> L3a=two, type(l3a)=<class 'str'> L3b=3, type(l3b)=<class 'int'> In [13]: # Cloning/copying a list L4 = list(range(5)) L5 = L4 # only one list, but with two names L5.extend([11,12]) L6 = list(range(5)) L7 = L6.copy() # L6 and L7 are two separate lists L7.extend([11,12]) print('l4={}'.format(l4)) print('l5={}'.format(l5)) print('l6={}'.format(l6)) print('l7={}'.format(l7)) L4=[0, 1, 2, 3, 4, 11, 12] L5=[0, 1, 2, 3, 4, 11, 12] L6=[0, 1, 2, 3, 4] L7=[0, 1, 2, 3, 4, 11, 12] In [14]: # Example tuples T1 = ('alpha', 'beta', 'delta', 'delta') T2 = ([ , ], 'e', 'pi', -1, -2) print('t1={}, type(t1)={}'.format(t1,type(t1))) print('t2={}, type(t2)={}'.format(t2,type(t2))) T1=('alpha', 'beta', 'delta', 'delta'), type(t1)=<class 'tuple'> T2=([ , ], 'e', 'pi', -1, -2), type(t2)=<class 'tuple'>

6 6 of 7 9/5/2018, 8:51 AM In [15]: # Tuples are immutable, but not lists nested in tuples #T1[2] = 'gamma' # TypeError: 'tuple' object does not support item assignment T2[0][0] = -2.7 # but this works print(t2) ([-2.7, ], 'e', 'pi', -1, -2) In [16]: # Example sets S1 = {1,2,3,2,1} S2 = set('ada Lovelace') S3 = set((2,3,5,('am','pm'),(2,3,5,7,11),2,3,5,7,11,('am','pm'))) print('s1={}, type(s1)={}'.format(s1,type(s1))) print('s2={}, type(s2)={}'.format(s2,type(s2))) print('s3={}, type(s3)={}'.format(s3,type(s3))) S1={1, 2, 3}, type(s1)=<class 'set'> S2={'d', 'c', 'L', 'e', ' ', 'a', 'l', 'o', 'A', 'v'}, type(s2)=<class 'set'> S3={2, 3, 5, 7, 11, ('am', 'pm'), (2, 3, 5, 7, 11)}, type(s3)=<class 'set'> In [17]: # Add/remove an element from a set S1.add(99) S3.remove(('am','pm')) print('s1={}, type(s1)={}'.format(s1,type(s1))) print('s3={}, type(s3)={}'.format(s3,type(s3))) S1={99, 1, 2, 3}, type(s1)=<class 'set'> S3={2, 3, 5, 7, 11, (2, 3, 5, 7, 11)}, type(s3)=<class 'set'> In [18]: # Example dictionaries D0 = {} # empty dictionary D1 = {'AM': [54,74,75], 'FM': [103,109], 'ASK': 316, 'FSK': 316, 'PSK': 316, 'QPSK' : 329} D2 = {'apple': 'green', 'mango': 'yellow', 'cherry': 'red', 'apple': 'red'} print('d0={}, type(d0)={}'.format(d0,type(d0))) print('d1={}, type(d1)={}'.format(d1,type(d1))) print('d2={}, type(d2)={}'.format(d2,type(d2))) # note: keys must be unique, only last one is kept D0={}, type(d0)=<class 'dict'> D1={'PSK': 316, 'ASK': 316, 'QPSK': 329, 'FM': [103, 109], 'AM': [54, 74, 75], ' FSK': 316}, type(d1)=<class 'dict'> D2={'mango': 'yellow', 'cherry': 'red', 'apple': 'red'}, type(d2)=<class 'dict'> In [19]: # Get the keys from a dictionary K0 = D0.keys() K1 = D1.keys() K2 = list(d2.keys()) # keys placed in a list print('k0={}, type(k0)={}'.format(k0,type(k0))) print('k1={}, type(k1)={}'.format(k1,type(k1))) print('k2={}, type(k2)={}'.format(k2,type(k2))) K0=dict_keys([]), type(k0)=<class 'dict_keys'> K1=dict_keys(['PSK', 'ASK', 'QPSK', 'FM', 'AM', 'FSK']), type(k1)=<class 'dict_k eys'> K2=['mango', 'cherry', 'apple'], type(k2)=<class 'list'>

7 7 of 7 9/5/2018, 8:51 AM In [20]: # Accessing values in a dictionary print(d1['fm']) print(d1['qpsk']) print(d2['apple'],d2['mango']) [103, 109] 329 red yellow In [21]: # Add/modify/delete a key-value pair D2['peach'] = 'white' # add D2['plum'] = 'purple' # add D2['apple'] = 'green' # modify del D1['FM'] # delete print('d1 = {}'.format(d1)) print('d1 keys: {}'.format(list(d1.keys()))) print('d2 = {}'.format(d2)) print('d2 keys: {}'.format(list(d2.keys()))) D1 = {'PSK': 316, 'ASK': 316, 'QPSK': 329, 'AM': [54, 74, 75], 'FSK': 316} D1 keys: ['PSK', 'ASK', 'QPSK', 'AM', 'FSK'] D2 = {'mango': 'yellow', 'cherry': 'red', 'apple': 'green', 'peach': 'white', 'p lum': 'purple'} D2 keys: ['mango', 'cherry', 'apple', 'peach', 'plum'] In [ ]:

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

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output Dr. Tiffani L. Williams Department of Computer Science and Engineering Texas A&M University Spring 2011 Python Python was developed

More information

Variable and Data Type I

Variable and Data Type I Islamic University Of Gaza Faculty of Engineering Computer Engineering Department Lab 2 Variable and Data Type I Eng. Ibraheem Lubbad September 24, 2016 Variable is reserved a location in memory to store

More information

ENGR 101 Engineering Design Workshop

ENGR 101 Engineering Design Workshop ENGR 101 Engineering Design Workshop Lecture 2: Variables, Statements/Expressions, if-else Edgardo Molina City College of New York Literals, Variables, Data Types, Statements and Expressions Python as

More information

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

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2 Basic Concepts Computer Science Computer - Science - Programming history Algorithms Pseudo code 2013 Andrew Case 2 Basic Concepts Computer Science Computer a machine for performing calculations Science

More information

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

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines. Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

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

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

Variable and Data Type I

Variable and Data Type I The Islamic University of Gaza Faculty of Engineering Dept. of Computer Engineering Intro. To Computers (LNGG 1003) Lab 2 Variable and Data Type I Eng. Ibraheem Lubbad February 18, 2017 Variable is reserved

More information

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers Lecture 27 Lecture 27: Regular Expressions and Python Identifiers Python Syntax Python syntax makes very few restrictions on the ways that we can name our variables, functions, and classes. Variables names

More information

CSCE 110 Programming I

CSCE 110 Programming I CSCE 110 Programming I Basics of Python (Part 1): Variables, Expressions, and Input/Output Dr. Tiffani L. Williams Department of Computer Science and Engineering Texas A&M University Spring 2013 Tiffani

More information

Introduction to Python

Introduction to Python Introduction to Python خانه ریاضیات اصفهان فرزانه کاظمی زمستان 93 1 Why Python? Python is free. Python easy to lean and use. Reduce time and length of coding. Huge standard library Simple (Python code

More information

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

Python I. Some material adapted from Upenn cmpe391 slides and other sources Python I Some material adapted from Upenn cmpe391 slides and other sources Overview Names & Assignment Data types Sequences types: Lists, Tuples, and Strings Mutability Understanding Reference Semantics

More information

Python Programming: Lecture 2 Data Types

Python Programming: Lecture 2 Data Types Python Programming: Lecture 2 Data Types Lili Dworkin University of Pennsylvania Last Week s Quiz 1..pyc files contain byte code 2. The type of math.sqrt(9)/3 is float 3. The type of isinstance(5.5, float)

More information

Introduction to Python! Lecture 2

Introduction to Python! Lecture 2 .. Introduction to Python Lecture 2 Summary Summary: Lists Sets Tuples Variables while loop for loop Functions Names and values Passing parameters to functions Lists Characteristics of the Python lists

More information

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING GE8151 - PROBLEM SOVING AND PYTHON PROGRAMMING Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING 1) Define Computer 2) Define algorithm 3) What are the two phases in algorithmic problem solving? 4) Why

More information

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s PYTHON FO R K I D S A P l ay f u l I n t r o d u c t i o n to P r o g r a m m i n g Jason R. Briggs Index Symbols and Numbers + (addition operator), 17 \ (backslash) to separate lines of code, 235 in strings,

More information

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala Loops and Conditionals HORT 59000 Lecture 11 Instructor: Kranthi Varala Relational Operators These operators compare the value of two expressions and returns a Boolean value. Beware of comparing across

More information

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D 1/58 Interactive use $ python Python 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information.

More information

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D 1/60 Interactive use $ python Python 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information.

More information

Computer Sciences 368 Scripting for CHTC Day 3: Collections Suggested reading: Learning Python

Computer Sciences 368 Scripting for CHTC Day 3: Collections Suggested reading: Learning Python Day 3: Collections Suggested reading: Learning Python (3rd Ed.) Chapter 8: Lists and Dictionaries Chapter 9: Tuples, Files, and Everything Else Chapter 13: while and for Loops 1 Turn In Homework 2 Homework

More information

Introduction to Python: Data types. HORT Lecture 8 Instructor: Kranthi Varala

Introduction to Python: Data types. HORT Lecture 8 Instructor: Kranthi Varala Introduction to Python: Data types HORT 59000 Lecture 8 Instructor: Kranthi Varala Why Python? Readability and ease-of-maintenance Python focuses on well-structured easy to read code Easier to understand

More information

Getting Started with Python

Getting Started with Python Fundamentals of Programming (Python) Getting Started with Python Sina Sajadmanesh Sharif University of Technology Some slides have been adapted from Python Programming: An Introduction to Computer Science

More information

Lists, Tuples and Dictionaries. HORT Lecture 10 Instructor: Kranthi Varala

Lists, Tuples and Dictionaries. HORT Lecture 10 Instructor: Kranthi Varala Lists, Tuples and Dictionaries HORT 59000 Lecture 10 Instructor: Kranthi Varala Core data types Numbers Strings Lists Dictionaries Tuples Files Sets References and dynamic typing Dynamic typing allows

More information

Collections. Lists, Tuples, Sets, Dictionaries

Collections. Lists, Tuples, Sets, Dictionaries Collections Lists, Tuples, Sets, Dictionaries Homework notes Homework 1 grades on canvas People mostly lost points for not reading the document carefully Didn t play again Didn t use Y/N for playing again

More information

About Variables in Python F E B 1 1 T H

About Variables in Python F E B 1 1 T H About Variables in Python F E B 1 1 T H Range of floating point numbers What is the largest floating point number in Python? Unfortunately, there is no sys.maxfloat. Here is an interesting way to find

More information

Working with Lists 4

Working with Lists 4 CS 61A Lecture 10 Announcements Lists ['Demo'] Working with Lists 4 Working with Lists >>> digits = [1, 8, 2, 8] 4 Working with Lists >>> digits = [1, 8, 2, 8] >>> digits = [2//2, 2+2+2+2, 2, 2*2*2] 4

More information

Real Python: Python 3 Cheat Sheet

Real Python: Python 3 Cheat Sheet Real Python: Python 3 Cheat Sheet Numbers....................................... 3 Strings........................................ 5 Booleans....................................... 7 Lists.........................................

More information

Beyond Blocks: Python Session #1

Beyond Blocks: Python Session #1 Beyond Blocks: Session #1 CS10 Spring 2013 Thursday, April 30, 2013 Michael Ball Beyond Blocks : : Session #1 by Michael Ball adapted from Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike

More information

Control Structures 1 / 17

Control Structures 1 / 17 Control Structures 1 / 17 Structured Programming Any algorithm can be expressed by: Sequence - one statement after another Selection - conditional execution (not conditional jumping) Repetition - loops

More information

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

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter Variables, Expressions, and Statements Chapter 2 Unless otherwise noted, the content of this course material is licensed under a Creative Commons Attribution 3.0 License. http://creativecommons.org/licenses/by/3.0/.

More information

https://lambda.mines.edu Why study Python in Principles of Programming Languages? Multi-paradigm Object-oriented Functional Procedural Dynamically typed Relatively simple with little feature multiplicity

More information

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

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an  . Michigan State University CSE 231, Fall 2013 CSE 231, Rich Enbody Office Hours After class By appointment send an email 2 1 Project 1 Python arithmetic Do with pencil, paper and calculator first Idle Handin Help room 3 What is a Computer Program?

More information

PROGRAMMING FUNDAMENTALS

PROGRAMMING FUNDAMENTALS 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

More information

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

CS2304: Python for Java Programmers. CS2304: Sequences and Collections CS2304: Sequences and Collections Sequences In Python A sequence type in python supports: The in membership operator. The len() function. Slicing like we saw with strings, s[1:3]. And is iterable (for

More information

Data Structures. Lists, Tuples, Sets, Dictionaries

Data Structures. Lists, Tuples, Sets, Dictionaries Data Structures Lists, Tuples, Sets, Dictionaries Collections Programs work with simple values: integers, floats, booleans, strings Often, however, we need to work with collections of values (customers,

More information

CMSC 201 Computer Science I for Majors

CMSC 201 Computer Science I for Majors CMSC 201 Computer Science I for Majors Lecture 02 Intro to Python Syllabus Last Class We Covered Grading scheme Academic Integrity Policy (Collaboration Policy) Getting Help Office hours Programming Mindset

More information

Script language: Python Data structures

Script language: Python Data structures Script language: Python Data structures Cédric Saule Technische Fakultät Universität Bielefeld 3. Februar 2015 Immutable vs. Mutable Previously known types: int and string. Both are Immutable but what

More information

Fundamentals of Programming (Python) Getting Started with Programming

Fundamentals of Programming (Python) Getting Started with Programming Fundamentals of Programming (Python) Getting Started with Programming Ali Taheri Sharif University of Technology Some slides have been adapted from Python Programming: An Introduction to Computer Science

More information

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1 What to add next time you are updating these slides Update slides to have more animation in the bullet lists Verify that each slide has stand alone speaker notes Page 1 Python 3 Running The Python Interpreter

More information

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

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif Input, Processing and Output Bonita Sharif 1 Review A program is a set of instructions a computer follows to perform a task The CPU is responsible for running and executing programs A set of instructions

More information

Advanced Algorithms and Computational Models (module A)

Advanced Algorithms and Computational Models (module A) Advanced Algorithms and Computational Models (module A) Giacomo Fiumara giacomo.fiumara@unime.it 2014-2015 1 / 34 Python's built-in classes A class is immutable if each object of that class has a xed value

More information

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

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals: Numeric Types There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals: 1-123 +456 2. Long integers, of unlimited

More information

DM550/DM857 Introduction to Programming. Peter Schneider-Kamp

DM550/DM857 Introduction to Programming. Peter Schneider-Kamp DM550/DM857 Introduction to Programming Peter Schneider-Kamp petersk@imada.sdu.dk http://imada.sdu.dk/~petersk/dm550/ http://imada.sdu.dk/~petersk/dm857/ TUPLES 2 Tuples as Immutable Sequences tuple =

More information

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

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15 1 LECTURE 1: INTRO Introduction to Scientific Python, CME 193 Jan. 9, 2014 web.stanford.edu/~ermartin/teaching/cme193-winter15 Eileen Martin Some slides are from Sven Schmit s Fall 14 slides 2 Course Details

More information

Python Programming Language

Python Programming Language Python Programming Language Data Structure Sachin PVPPCOE December 22, 2015 Data types Basic objects: numbers(float, int, complex), strings, Tuples, lists, sets, & dictionaries Other data types: Modules,

More information

DaMPL. Language Reference Manual. Henrique Grando

DaMPL. Language Reference Manual. Henrique Grando DaMPL Language Reference Manual Bernardo Abreu Felipe Rocha Henrique Grando Hugo Sousa bd2440 flt2107 hp2409 ha2398 Contents 1. Getting Started... 4 2. Syntax Notations... 4 3. Lexical Conventions... 4

More information

Variables, Expressions, and Statements

Variables, Expressions, and Statements Variables, Expressions, and Statements Chapter 2 Python for Informatics: Exploring Information www.pythonlearn.com Constants Fixed values such as numbers, letters, and strings are called constants because

More information

Jython. secondary. memory

Jython. secondary. memory 2 Jython secondary memory Jython processor Jython (main) memory 3 Jython secondary memory Jython processor foo: if Jython a

More information

DSC 201: Data Analysis & Visualization

DSC 201: Data Analysis & Visualization DSC 201: Data Analysis & Visualization Arrays Dr. David Koop Class Example class Rectangle: def init (self, x, y, w, h): self.x = x self.y = y self.w = w self.h = h def set_corner(self, x, y): self.x =

More information

Princeton University COS 333: Advanced Programming Techniques A Subset of Python 2.7

Princeton University COS 333: Advanced Programming Techniques A Subset of Python 2.7 Princeton University COS 333: Advanced Programming Techniques A Subset of Python 2.7 Program Structure # Print "hello world" to stdout. print 'hello, world' # Print "hello world" to stdout. def f(): print

More information

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

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates Chapter 3 : Informatics Practices Class XI ( As per CBSE Board) Python Fundamentals Introduction Python 3.0 was released in 2008. Although this version is supposed to be backward incompatibles, later on

More information

Play with Python: An intro to Data Science

Play with Python: An intro to Data Science Play with Python: An intro to Data Science Ignacio Larrú Instituto de Empresa Who am I? Passionate about Technology From Iphone apps to algorithmic programming I love innovative technology Former Entrepreneur:

More information

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

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops PRG PROGRAMMING ESSENTIALS 1 Lecture 2 Program flow, Conditionals, Loops https://cw.fel.cvut.cz/wiki/courses/be5b33prg/start Michal Reinštein Czech Technical University in Prague, Faculty of Electrical

More information

CS 1301 Exam 1 Fall 2009

CS 1301 Exam 1 Fall 2009 Page 1/6 CS 1301 Fall 2009 Exam 1 Your Name: I commit to uphold the ideals of honor and integrity by refusing to betray the trust bestowed upon me as a member of the Georgia Tech community. CS 1301 Exam

More information

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

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1 Review Midterm Exam 1 Review Midterm Exam 1 Exam on Monday, October 7 Data Types and Variables = Data Types and Variables Basic Data Types Integers Floating Point Numbers Strings Data Types and Variables

More information

COMP 204: Dictionaries Recap & Sets

COMP 204: Dictionaries Recap & Sets COMP 204: Dictionaries Recap & Sets Material from Carlos G. Oliver, Christopher J.F. Cameron October 10, 2018 1/21 Reminder Midterm on Wednesday October 17 at 6:30-8:00 pm. Assignment 2: numpy is allowed

More information

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS 1439-1440 1 Outline 1. Introduction 2. Why Python? 3. Compiler and Interpreter 4. The first program 5. Comments and Docstrings 6. Python Indentations

More information

Data Handing in Python

Data Handing in Python Data Handing in Python As per CBSE curriculum Class 11 Chapter- 3 By- Neha Tyagi PGT (CS) KV 5 Jaipur(II Shift) Jaipur Region Introduction In this chapter we will learn data types, variables, operators

More information

Language Reference Manual

Language Reference Manual Espresso Language Reference Manual 10.06.2016 Rohit Gunurath, rg2997 Somdeep Dey, sd2988 Jianfeng Qian, jq2252 Oliver Willens, oyw2103 1 Table of Contents Table of Contents 1 Overview 3 Types 4 Primitive

More information

ME30 Lab3 Decisions. February 20, 2019

ME30 Lab3 Decisions. February 20, 2019 ME30 Lab3 Decisions February 20, 2019 0.0.1 ME 30 Lab 4 - Conditional Program Execution ME 30 ReDev Team 2018-07-06 Description and Summary: This lab introduces the programming concept of decision-making,

More information

python 01 September 16, 2016

python 01 September 16, 2016 python 01 September 16, 2016 1 Introduction to Python adapted from Steve Phelps lectures - (http://sphelps.net) 2 Python is interpreted Python is an interpreted language (Java and C are not). In [1]: 7

More information

Here n is a variable name. The value of that variable is 176.

Here n is a variable name. The value of that variable is 176. UNIT II DATA, EXPRESSIONS, STATEMENTS 9 Python interpreter and interactive mode; values and types: int, float, boolean, string, and list; variables, expressions, statements, tuple assignment, precedence

More information

The float type and more on variables FEB 6 TH 2012

The float type and more on variables FEB 6 TH 2012 The float type and more on variables FEB 6 TH 2012 The float type Numbers with decimal points are easily represented in binary: 0.56 (in decimal) = 5/10 + 6/100 0.1011 (in binary) = ½+0/4 + 1/8 +1/16 The

More information

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords Worksheet 1: Introductory Exercises Turtle Programming Calculations The Print Function Comments Syntax Semantics Strings Concatenation Quotation Marks Types Variables Restrictions on Variable Names Long

More information

Basic Scripting, Syntax, and Data Types in Python. Mteor 227 Fall 2017

Basic Scripting, Syntax, and Data Types in Python. Mteor 227 Fall 2017 Basic Scripting, Syntax, and Data Types in Python Mteor 227 Fall 2017 Basic Shell Scripting/Programming with Python Shell: a user interface for access to an operating system s services. The outer layer

More information

Basic Syntax - First Program 1

Basic Syntax - First Program 1 Python Basic Syntax Basic Syntax - First Program 1 All python files will have extension.py put the following source code in a test.py file. print "Hello, Python!";#hello world program run this program

More information

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman 1. BASICS OF PYTHON JHU Physics & Astronomy Python Workshop 2017 Lecturer: Mubdi Rahman HOW IS THIS WORKSHOP GOING TO WORK? We will be going over all the basics you need to get started and get productive

More information

Introduction to Python, Cplex and Gurobi

Introduction to Python, Cplex and Gurobi Introduction to Python, Cplex and Gurobi Introduction Python is a widely used, high level programming language designed by Guido van Rossum and released on 1991. Two stable releases: Python 2.7 Python

More information

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

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT Python The way of a program Srinidhi H Asst Professor Dept of CSE, MSRIT 1 Problem Solving Problem solving means the ability to formulate problems, think creatively about solutions, and express a solution

More information

Ceng 111 Fall 2015 Week 7a

Ceng 111 Fall 2015 Week 7a Ceng 111 Fall 2015 Week 7a Container data Credit: Some slides are from the Invitation to Computer Science book by G. M. Schneider, J. L. Gersting and some from the Digital Design book by M. M. Mano and

More information

Python BASICS. Introduction to Python programming, basic concepts: formatting, naming conventions, variables, etc.

Python BASICS. Introduction to Python programming, basic concepts: formatting, naming conventions, variables, etc. Python BASICS Introduction to Python programming, basic concepts: formatting, naming conventions, variables, etc. Identikit First appeared in 1991 Designed by Guido van Rossum General purpose High level

More information

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

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards Language Reference Manual Introduction The purpose of

More information

Overview of List Syntax

Overview of List Syntax Lists and Sequences Overview of List Syntax x = [0, 0, 0, 0] Create list of length 4 with all zeroes x 4300112 x.append(2) 3 in x x[2] = 5 x[0] = 4 k = 3 Append 2 to end of list x (now length 5) Evaluates

More information

CIS192 Python Programming. Robert Rand. August 27, 2015

CIS192 Python Programming. Robert Rand. August 27, 2015 CIS192 Python Programming Introduction Robert Rand University of Pennsylvania August 27, 2015 Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 1 / 30 Outline 1 Logistics Grading Office

More information

Chapter 8 Dictionaries. Dictionaries are mutable, unordered collections with elements in the form of a key:value pairs that associate keys to values.

Chapter 8 Dictionaries. Dictionaries are mutable, unordered collections with elements in the form of a key:value pairs that associate keys to values. Chapter 8 Dictionaries Dictionaries are mutable, unordered collections with elements in the form of a key:value pairs that associate keys to values. Creating a Dictionary To create a dictionary, you need

More information

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvania) CIS 192 Fall Lecture 2 September 6, 2017 1 / 34

More information

MEIN 50010: Python Data Structures

MEIN 50010: Python Data Structures : Python Data Structures Fabian Sievers Higgins Lab, Conway Institute University College Dublin Wednesday, 2017-10-18 Data Structures Stacks, Queues & Deques Structures Data structures are a way of storing

More information

Programming to Python

Programming to Python Programming to Python Sept., 5 th Slides by M. Stepp, M. Goldstein, M. DiRamio, and S. Shah Compiling and interpreting Many languages require you to compile (translate) your program into a form that the

More information

Variables, Constants, and Data Types

Variables, Constants, and Data Types Variables, Constants, and Data Types Strings and Escape Characters Primitive Data Types Variables, Initialization, and Assignment Constants Reading for this lecture: Dawson, Chapter 2 http://introcs.cs.princeton.edu/python/12types

More information

VLC : Language Reference Manual

VLC : Language Reference Manual VLC : Language Reference Manual Table Of Contents 1. Introduction 2. Types and Declarations 2a. Primitives 2b. Non-primitives - Strings - Arrays 3. Lexical conventions 3a. Whitespace 3b. Comments 3c. Identifiers

More information

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

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes GIS 4653/5653: Spatial Programming and GIS More Python: Statements, Types, Functions, Modules, Classes Statement Syntax The if-elif-else statement Indentation and and colons are important Parentheses and

More information

Variables, expressions and statements

Variables, expressions and statements Variables, expressions and statements 2.1. Values and data types A value is one of the fundamental things like a letter or a number that a program manipulates. The values we have seen so far are 2 (the

More information

STA141C: Big Data & High Performance Statistical Computing

STA141C: Big Data & High Performance Statistical Computing STA141C: Big Data & High Performance Statistical Computing Lecture 1: Python programming (1) Cho-Jui Hsieh UC Davis April 4, 2017 Python Python is a scripting language: Non-scripting language (C++. java):

More information

CS 1301 Exam 1 Answers Fall 2009

CS 1301 Exam 1 Answers Fall 2009 Page 1/6 CS 1301 Fall 2009 Exam 1 Your Name: I commit to uphold the ideals of honor and integrity by refusing to betray the trust bestowed upon me as a member of the Georgia Tech community. CS 1301 Exam

More information

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

Computer Components. Software{ User Programs. Operating System. Hardware Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

More information

CS 2316 Exam 1 Spring 2014

CS 2316 Exam 1 Spring 2014 CS 2316 Exam 1 Spring 2014 Name : Grading TA: Integrity: By taking this exam, you pledge that this is your work and you have neither given nor received inappropriate help during the taking of this exam

More information

(f) d={ alchemist :( a, t ), shaman : ( s, n ), wizard : ( w, z )} d[ shaman ][1]

(f) d={ alchemist :( a, t ), shaman : ( s, n ), wizard : ( w, z )} d[ shaman ][1] CSCI1101 Final Exam December 18, 2018 Solutions 1. Determine the value and type of each of the expressions below. If the question has two lines, assume that the statement in the first line is executed,

More information

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

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu) I. INTERACTIVE MODE VERSUS SCRIPT MODE There are two ways to use the python interpreter: interactive mode and script mode. 1. Interactive Mode (a) open a terminal shell (terminal emulator in Applications

More information

Introduction to Problem Solving and Programming in Python.

Introduction to Problem Solving and Programming in Python. Introduction to Problem Solving and Programming in Python http://cis-linux1.temple.edu/~tuf80213/courses/temple/cis1051/ Overview Types of errors Testing methods Debugging in Python 2 Errors An error in

More information

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

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python CS95003 - Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / 2014 Subjects 1) Beginning with Python 2) Variables 3) Strings 4) Basic arithmetic operators 5) Flow control 6) Comparison

More information

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Python review. 1 Python basics. References. CS 234 Naomi Nishimura Python review CS 234 Naomi Nishimura The sections below indicate Python material, the degree to which it will be used in the course, and various resources you can use to review the material. You are not

More information

Lecture #12: Mutable Data. map rlist Illustrated (III) map rlist Illustrated. Using Mutability For Construction: map rlist Revisited

Lecture #12: Mutable Data. map rlist Illustrated (III) map rlist Illustrated. Using Mutability For Construction: map rlist Revisited Lecture #12: Mutable Data Using Mutability For Construction: map rlist Revisited Even if we never change a data structure once it is constructed, mutation may be useful during its construction. Example:

More information

Coral Programming Language Reference Manual

Coral Programming Language Reference Manual Coral Programming Language Reference Manual Rebecca Cawkwell, Sanford Miller, Jacob Austin, Matthew Bowers rgc2137@barnard.edu, {ja3067, skm2159, mlb2251}@columbia.edu October 15, 2018 Contents 1 Overview

More information

The Pyth Language. Administrivia

The Pyth Language. Administrivia Administrivia The Pyth Language Lecture 5 Please make sure you have registered your team, created SSH keys as indicated on the admin page, and also have electronically registered with us as well. Prof.

More information

Python for ArcGIS. Lab 1.

Python for ArcGIS. Lab 1. Python for ArcGIS. Lab 1. Python is relatively new language of programming, which first implementation arrived around early nineties of the last century. It is best described as a high level and general

More information

egrapher Language Reference Manual

egrapher Language Reference Manual egrapher Language Reference Manual Long Long: ll3078@columbia.edu Xinli Jia: xj2191@columbia.edu Jiefu Ying: jy2799@columbia.edu Linnan Wang: lw2645@columbia.edu Darren Chen: dsc2155@columbia.edu 1. Introduction

More information

CIS192 Python Programming

CIS192 Python Programming CIS192 Python Programming Data Types Joseph Cappadona University of Pennsylvania September 03, 2015 Joseph Cappadona (University of Pennsylvania) CIS 192 September 03, 2015 1 / 32 Outline 1 Data Types

More information

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists Module 04: Lists Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10 1 Consider the string method split >>> name = "Harry James Potter" >>> name.split() ['Harry',

More information

Python Evaluation Rules

Python Evaluation Rules Python Evaluation Rules UW CSE 160 https://courses.cs.washington.edu/courses/cse160/15sp/ Michael Ernst and Isaac Reynolds mernst@cs.washington.edu April 1, 2015 Contents 1 Introduction 2 1.1 The Structure

More information

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

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu) I. INTERACTIVE MODE VERSUS SCRIPT MODE There are two ways to use the python interpreter: interactive mode and script mode. 1. Interactive Mode (a) open a terminal shell (terminal emulator in Applications

More information

Lecture 0. Introduction to Python. Randall Romero Aguilar, PhD II Semestre 2018 Last updated: July 12, 2018

Lecture 0. Introduction to Python. Randall Romero Aguilar, PhD II Semestre 2018 Last updated: July 12, 2018 Lecture 0 Introduction to Python Randall Romero Aguilar, PhD II Semestre 2018 Last updated: July 12, 2018 Universidad de Costa Rica SP6534 - Economía Computacional Table of contents 1. Getting Python and

More information