Λογισμικό & κατασκευή λογ/κού

7 Νοε 2013 (πριν από 4 χρόνια και 6 μήνες)

72 εμφανίσεις

COMPUTER

SCIENCE

PYTHON I

CSCI 1001

OVERVIEW

OF

represent
data

manipulate
data

collect

data

present
data

construct
sequences
of operations

organize and
structure
data

Integer:

-
1048, 0xA375, 0b1101

Floating point:

3.14159, 6.02e23

Strings (of
ascii

text):

“This is a python string”

‘so is this.
\
t

with a tab!’

‘Just 1 backslash:
\
\

Boolean:

True, False

assignment: variable

=
value
.

age = 30

name = ‘
justin

arithmetic
: +, *,
-
, /, **

lower = age/2 + 7

string manipulation
:

email = lastname[0:4]+‘0001’

functions
:

c

=
math.sqrt(a
**2 +
b
**2)

sequence:
in order:

age = 30

lower = age/2 + 7

conditional:

if age < lower:

print “age is less”

else:

print “age is more”

iterative:

while score < 10:

print score

score = score + 1

for score in range(1,10):

print score

for day in range(0,7):

if day == 0: print “black”

if day == 1 or day == 2:

print “heart attack”

if day ==3:

print “never looking back.”

if day == 4:

print “I’m in love”

if day == 5: print “wait.”

if day == 6:

print “too late.”

functions
:

def
example(n
):

for
i

in range(1,n+1):

print
i

return

example(1)

example(10)

def
area(r
):

return 3.14 *
r

*
r

input
:

n

=
raw_input(‘enter

a number:’)

output
:

print ‘string’, 7, 3.14, True

lists
:

[‘a’, ‘
b
’, ‘
c
’], []

dictionaries
:

dict[‘key
’] = ‘value’

tuples
:

(‘a’, ‘
b
’, ‘
c
’)

import sys

count = 0

for age in
sys.stdin
:

if (
int(age
) >= 21):

count = count + 1

print count

n

=
int(raw_input
())

sum = 0

for
i

in range(1,n+1):

sum = sum +
i

print sum

http://cs1001.us/

Please read Sections 1 and 2 of the Python chapter for Monday