42 pages
English

# Python language: Data structures and functions (session 8)

-

Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
42 pages
English
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

Description

Python language: Data structuresand functionsThe FOSSEE GroupDepartment of Aerospace EngineeringIIT Bombay1 May, 2010Day 2, Session 2FOSSEE Team (IIT Bombay) Basic Python 1 / 40Outline1 Control ﬂowBasic LoopingExercises2 Data structuresListsTuplesDictionariesSets3 FunctionsFOSSEE Team (IIT Bombay) Basic Python 2 / 40Control ﬂowOutline1 Control ﬂowBasic LoopingExercises2 Data structuresListsTuplesDictionariesSets3 FunctionsFOSSEE Team (IIT Bombay) Basic Python 3 / 40Control ﬂow Basic LoopingOutline1 Control ﬂowBasic LoopingExercises2 Data structuresListsTuplesDictionariesSets3 FunctionsFOSSEE Team (IIT Bombay) Basic Python 4 / 40Control ﬂow Basic LoopingwhileExample: Fibonacci seriesSum of previous two elements deﬁnes the nextIn []: a, b = 0, 1In []: while b < 10:...: print b, a, b = b, a + b...:1 1 2 3 5 8FOSSEE Team (IIT Bombay) Basic Python 5 / 40Control ﬂow Basic Loopingrange()range([start,] stop[, step])range() returns a list of integersThestart and thestep arguments are optionalstop is not included in the listDocumentation conventionAnything within[] is optionalNothing to do with Python.FOSSEE Team (IIT Bombay) Basic Python 6 / 40Control ﬂow Basic Loopingfor . . .range()Example: print squares of ﬁrst5 numbersIn []: for i in range(5):....: print i, i i*....:0 01 12 43 94 16FOSSEE Team (IIT Bombay) Basic Python 7 / 40Control ﬂow Basic Loopingfor . . .range()Example: print squares of odd ...

Sujets

##### Calcul (mathématiques)

Informations

 Publié par mtoledan Publié le 27 juin 2011 Nombre de lectures 181 Langue English

Exrait

FO
Python
SSEEeTam(IITBombay)
language: Data structures and functions
The FOSSEE Group
Department of Aerospace Engineering IIT Bombay
1 May, 2010 Day 2, Session 2
aBiscPython1/40
Outline
1
2
3
OF
Control ﬂow Basic Looping Exercises
Data structures Lists Tuples Dictionaries Sets
Functions
SSEETeam(IITBombay)BasicyPhtno2/40
3/40thon
2
Data structures Lists Tuples Dictionaries Sets
3
Functions
Outline
Control ﬂow Basic Looping Exercises
1
CwrtnooloicPy)BasmbayITBomaI(EEeTOFSS
ControlowBsacioLponigamTeEESSFO
Functions
3
yaB)saciI(TIoBbm40
Outline
Control ﬂow Basic Looping Exercises
1
2
Data structures Lists Tuples Dictionaries Sets
Python4/
elicLowBasgwhiopinColortnoTBomm(IIETeaOSSEF
1 1 2 3 5 8
Example: Fibonacci series Sum of previous two elements deﬁnes the next
0/4
a
In []: a, b = 0, 1 In []:whileb < 10: ...:printb, ...: a, b = b, ...: ...:
b
+
tyPc5noh)yabisaB
oCtnorlpioorangBaowcLsi(egn)SEOSFIIm(eaET
Documentation convention Anything within[]is optional Nothing to do with Python.
range([start,] stop[, step])
range()returns a list of integers Thestartand thesteparguments are optional stopis not included in the list
6noh04/siBaytcPomTBy)ba
LcisaBwolortnoC(e)rangr...ngfooopim(eaETSEbaomTBIISOF
iinargn(e)5: printi, i i *
Example: print squares of ﬁrst5numbers
0
In []: ....: ....: ....: 0 0 1 1 2 4 3 9 4 16
for
isPc)yaB7n4/tyoh
sicLoopirolowBaargn(e)gnof.r..tnoChtyP/8no
In []: ....: ....: ....: 3 9 5 25 7 49 9 81
04
Example: print squares of odd numbers from 3
2):
iinrange(3, 10, print *i, i i
for
5 m
to
9
OFSSEEeTma(IITBombay)Basic
sesExowcierntColro
Outline
40
2
Data structures Lists Tuples Dictionaries Sets
Control ﬂow Basic Looping Exercises
1
3
Functions
B)yacisahtyP/9noTeamSSEEBomb(IITOF
SSFOnortlooEwexcrsiCelbo1.1mPresleobetmsPr1:TeEE(IamBoITaymbsaB)yPcinoht4/010
Write a program that displays all three digit numbers that are equal to the sum of the cubes of their digits. That is, print numbersabcthat have the property abc=a3+b3+c3 For example, 153=13+53+33
These are calledtsmrAngros.ermbnu
Start with an arbitrary (positive) integer. If the number is even, divide by 2; if the number is odd, multiply by 3 and add 1. Repeat the procedure with the new number. It appears that for all starting values there is a cycle of 4, 2, 1 at which the procedure loops.
3 4
1 2
1/n1
Write a program that accepts the starting value and prints out the Collatz sequence.10 m
40PcisohtyabmoaB)yeam(IITBFOSSEETexEwsicrrtnoolo.2m1ol-CPresleobneecalztesuqC
• Accueil
• Ebooks
• Livres audio
• Presse
• BD
• Documents