cs 133 introduction to computational and data science
play

CS 133 - Introduction to Computational and Data Science Instructor: - PowerPoint PPT Presentation

1 CS 133 - Introduction to Computational and Data Science Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017 Previous class We have learned the path and file system. Today we are going to learn


  1. 1 CS 133 - Introduction to Computational and Data Science Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

  2. Previous class • We have learned the path and file system. • Today we are going to learn how to use Atom and get our first Python program! 2

  3. Atom • Demo how to open and use Atom 3

  4. Introduction to Python • What is Python? Why do people use Python? Python Java Perl Ruby R Scala Go C C++ C# Scheme Swift Objective C JavaScript

  5. Introduction to Python • It’s free • It’s portable • It’s powerful • It’s mixable

  6. Programming basics ■ code or source code : The sequence of instructions in a program. ■ syntax : The set of legal structures and commands that can be used in a particular programming language. ■ output : The messages printed to the user by a program. ■ console : The text box onto which output is printed. ■ Some source code editors pop up the console as an external window, and others contain their own console window. 6

  7. The Python Interpreter •Python is an interpreted language >>> 3 + 7 10 •The interpreter provides an interactive >>> 3 < 15 environment to play with the language True >>> 'print me' •Results of expressions are printed on the 'print me' screen >>> print 'print me' print me >>>

  8. Compiling and interpreting ■ Many languages require you to compile (translate) your program into a form that the machine understands. compile execute source code byte code output Hello.java Hello.class ■ Python is instead directly interpreted into machine instructions. interpret source code output Hello.py 8

  9. How to run? • Python scripts end with .py • python test.py • python2 test.py Python 2 vs Python 3 • print 'Hello, World!‘ vs print(' Hello, World!')

  10. Demo: Hello World •Open a terminal window and type “python” •If on Windows open a Python IDE like IDLE •At the prompt type ‘hello world!’ >>> 'hello world!' 'hello world!'

  11. Demo: Hello World • In Atom, create a python script: helloworld.py. Let’s do it together!

  12. How to run? • We need to “navigate” until we find our file to be able to execute it. • To do that we need to open a command prompt window. • Type the words cmd in the search bar and click on the command prompt. • To find your file you can use the following commands • cd name_of_directory to change directory • cd .. To go one directory “below” (Note that there is a space between cd and the 2 dots) • dir list all files in the current directory

  13. Handout 2 • Let’s practice it for a simple Python program!

  14. Comments • Documenting your code is very important • Use # to write any message that will be ignored by Python. • This can also be used to test your code # This is a single line comment • ‘’’ is used to have multiline comments ‘’’ This is a multi Lin E Comment ‘’’

  15. The print statement •Elements separated by commas print >>> print 'hello' with a space between them hello •A comma at the end of the statement >>> print 'hello', 'there' (print ‘hello’,) will not print a newline hello there character

  16. variables • In Python, like in other languages, we store values in variables. Unlike other languages, in Python the variables don’t have a “type” • Use of single quotes ‘’ represents text. No quotes represents numbers • >>>message = ‘Hello’ • >>>print message • >>>message = “Hello” • >>>print message • >>>message = ‘’’Hello’’’ • >>>print message

  17. Everything is an object • Everything means everything, including >>> x = 7 functions and classes (more on this >>> x later!) 7 Data type is a property of the object and >>> x = 'hello' • not of the variable >>> x 'hello' >>>

  18. variables variables Rules of naming a variable: • Don’t start with numbers • Don’t use @ or - • Don’t use reserved words

  19. Practice Can I use the following variable names? 1ab • • ab@a • aAAA3 • ABDA2 • AND • for • For • a_12A • b-32D

  20. Object Type Summary

  21. Numbers: Integers • Integer – the equivalent of a C long • Long Integer – an unbounded integer value. >>> 132224 132224 >>> 132323 ** 2 17509376329L >>>

  22. Numbers: Floating Point >>> 1.23232 • int(x) converts x to an integer 1.2323200000000001 >>> print 1.23232 • float(x) converts x to a floating point 1.23232 • The interpreter shows 
 >>> 1.3E7 a lot of digits 13000000.0 >>> int(2.0) 2 >>> float(2) 2.0

  23. Numbers: Floating Point • int(10.39) • int(100.9999) • int(1001.00001) • float(87) • float(eight)

  24. Numbers: Complex • Built into Python >>> x = 3 + 2j >>> y = -1j • Same operations are supported as integer >>> x + y and float (3+1j) >>> x * y (2-3j)

  25. Operators Operations in Python are based on sign precedence Image from: http:// www.tutorialspoint.com/ python/ operators_precedence_exa mple.htm

  26. Operators Integer vs float operations • Integer operation will result in only the “integer” part of the operation • 5/3 equals 1 • Float operation will result in the “float” value of the operation • 5/3.0 equals 1.66666667 • 5.0/3 equals 1.66666667 • 5.0/3.0 equals 1.666666667 • You can fix that by adding the words: from __future__ import division • At the beginning of your code • Let’s try it together

  27. After class 1. Practice and get familiar with Atom, command prompt 2. Try examples using python, such as Integer, Strings

Recommend


More recommend