CS602 – - Data-Driven Development with Python–Fall’22
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
CS602 – - Data-Driven Development with Python–Fall’22
Handout 1
Introduction to Python programming language.
Data types and operations.
Input/output operations.
Python – a general-purpose programming language
• Created in 1990 by Guido van Rossum, in Netherlands
• Interpreted language, originally created as a middle-ground between scripting languages and C
• The core of the standard interpreter is implemented in C
• Quickly gained popularity, volunteers built multiple packages extending language capabilities
• Developed and maintained by volunteers, distributed for free by Python Software Foundation
• Python 2 and Python 3 are two different versions not compatible with each other.
1 We will use Python 3, although it is possible to have both versions installed on your computer.
Illustration byJake VanderPlas
Important features of Python
• Interpreted
• Easily extendable
• Very versatile
• ‘easy to use’
• Uses whitespace to structure code
• Every data value is an object
• Currently supports
1 Procedural programming
1 Object-oriented programming
1 Functional programming
FIRST EXAMPLE
''' Created on Aug 27, 2018 by tbabaian Simple example program for the first class. Converts temperature in degrees Fahrenheit to Celsius according to the formula TC = (TF -32)* 5/9 ''' print ( 'This program converts temperature in degrees Fahrenheit to degrees Celcius') fahStr = input( 'Please enter an integer number of degrees F ') fah = eval(fahStr) # evaluate the string, will convert input string into a number celcius = (fah - 32) * 5 / 9 print (fah, 'degrees F equals ', celcius, ' degrees C') |
Components of the program
Comments: # for a single line, till the end of line
''' paragraph-comments
Start a multiple line comment with three quotations
End it with three quotations
'''
Identifiers: names for elements in a program, including functions, variables, classes, etc…
• Must not start with a digit
• Can’t be a keyword : word that has a meaning in the language, such as int or def
• Can’t include spaces, punctuation symbols
• Are case sensitive: x is not the same as X! Punctuation and other grammar
1 Python is sensitive to white space – indentation is used to indicate nesting of blocks (where other languages use {} or other blog begin-end statements) Statements in the same block must be indented in the same way.
Outer block is not indented.
1 a “;” is needed to indicate the end of a statement only if there are multiple statements on the same line, e.g.
a = 4; b = 5
1 \ is a line continuation symbol
Variable - is a named location to store data
• all Python variables are pointers
• Python is dynamically typed, meaning a variable type is not declared, it is dynamically inferred based on the value it points to, and a variable can be assigned any type of values at any point.
o Avoid changing the type of value stored in a variable to avoid errors!
DATA TYPES AND VALUES
Python has a range of built-in types.
Numeric built-in types: int, float, complex
have no range limit, i.e. can represent an arbitrary large or small number. Text built-in type: str
Boolean values: True, False
- But other values can be used to indicate False, including 0 and None
Sequence types: list, tuple, range
list – mutable heterogeneous collection
tuple – immutable heterogeneous collection
range - immutable sequence of numbers and is commonly used for looping
'''Created on Aug 27, 2018 @author: TBABAIAN Demo of built-in types''' # numeric types: int, float, complex print (3, 'has type', type(3)) print (-4.5 , 'has type', type(-4 .5)) print (complex(3,4), 'has type', type ( complex(3,4) )) # have no range limit a = 30000000000000000000000000000+1 print (a, 'has type', type(a)) print (1/a, 'has type' , type(1/a)) print ( '\nStrings ---------------------------------------------------- ') # str type print (type( 'abc ')) print (type( 'a')) print (type("also a string")) print ( '\nSequences ------------------------------------------------ ') # sequence types: list, tuple range lst = [a, 'b', 4 .3, -9] lst [3] = 55 #lists are mutable, i.e. can be changed print (lst, ' has type ', type(lst)) tpl = (3, 7 , 'foo') print (tpl, ' has type ', type(tpl)) r = range(20, 30, 2) |
ASSIGNMENT OPERATOR =
Used to set, or assign a variable is to store a given value in the location denoted by the variable name . Not the same as equality in algebra, It means -
“Store the value of the expression on the right side to the variable on the left side. ”
Can have any expression on the right hand side of =
x = 1 # Assign 1 to x
x = x + 1
i = j = k = 1
Simultaneous assignment: var1 , var2, ..., varn = exp1, exp2, ..., expn means: var1 = exp1; var2 = exp2; …varn=expn
x,y = 1, 3
x,y = y, x
# Assign 1 to x, 3 to y
# Swap values stored in x and y
KEYBOARD INPUT
input(‘prompt’) – input function, displays prompt specified as a parameter and returns the string of characters entered by the user.
To convert user input from str to another type, use
- eval function, or
- Type conversion.
Examples:
1.
fahStr = input( 'Please enter an integer number of degrees F ') fah = eval(fahStr) # convert input string into a number
2.
fahStr = input( 'Please enter an integer number of degrees F ') fah = int(fahStr) # convert input string into a number
ARITHMETIC OPERATORS:
+, -, *, /, //, %, ( ), **
(1) integer division: results from dividing one int by another. Returns whole number quotient, ignoring remainder (truncates).
21 // 4 = ? 7 // 2 = ?
(2) % : modulus, or remainder.
21 % 4 = ? 8 % 2 = ?
(3) ** - exponentiation.
3** 2 = ?
Practice: what is the value of z in the following?
x = 7; y = 3;
z = (x//y)*y + x%y
Order of precedence (elements in the same row have the same precedence): ()
+(unary) – (unary)
* / // %
+ -
= (assignment)
Relational Operators: also called Comparison Operators.
Use them for making decisions: True or False for outcome.
Include : > < >= <= == !=
'''Created on Aug 27, 2018 @author: TBABAIAN Demonstrating comparison operators ''' print ( '---comparison of numbers---------------------------- ') i = 5; j =9; print (i > j); |
2023-02-21