|
1 #+LaTeX_CLASS: beamer |
|
2 #+LaTeX_CLASS_OPTIONS: [presentation] |
|
3 #+BEAMER_FRAME_LEVEL: 1 |
|
4 |
|
5 #+BEAMER_HEADER_EXTRA: \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} |
|
6 #+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra) |
|
7 #+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC |
|
8 |
|
9 #+LaTeX_CLASS: beamer |
|
10 #+LaTeX_CLASS_OPTIONS: [presentation] |
|
11 |
|
12 #+LaTeX_HEADER: \usepackage[english]{babel} \usepackage{ae,aecompl} |
|
13 #+LaTeX_HEADER: \usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} |
|
14 |
|
15 #+LaTeX_HEADER: \usepackage{listings} |
|
16 |
|
17 #+LaTeX_HEADER:\lstset{language=Python, basicstyle=\ttfamily\bfseries, |
|
18 #+LaTeX_HEADER: commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, |
|
19 #+LaTeX_HEADER: showstringspaces=false, keywordstyle=\color{blue}\bfseries} |
|
20 |
|
21 #+TITLE: Getting started with for |
|
22 #+AUTHOR: FOSSEE |
|
23 #+EMAIL: |
|
24 #+DATE: |
|
25 |
|
26 #+DESCRIPTION: |
|
27 #+KEYWORDS: |
|
28 #+LANGUAGE: en |
|
29 #+OPTIONS: H:3 num:nil toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t |
|
30 #+OPTIONS: TeX:t LaTeX:nil skip:nil d:nil todo:nil pri:nil tags:not-in-toc |
|
31 |
|
32 * Outline |
|
33 - ~for~ loop in Python. |
|
34 - Blocks of code in Python. |
|
35 - Indentation |
|
36 * Whitespace in Python |
|
37 - Whitespace is significant |
|
38 - blocks are visually separated |
|
39 - Blocks are indented using 4 spaces |
|
40 : Block A |
|
41 : Block A |
|
42 : Block B |
|
43 : Block B |
|
44 : Block A |
|
45 ~Block B~ is an inner block and is indented using 4 spaces |
|
46 * Exercise 1 |
|
47 Write a ~for~ loop which iterates through a list of numbers and find |
|
48 the square root of each number. |
|
49 : |
|
50 The numbers are, |
|
51 : 1369, 7225, 3364, 7056, 5625, 729, 7056, |
|
52 : 576, 2916 |
|
53 * Solution 1 |
|
54 - Open text editor and type the following code |
|
55 #+begin_src python |
|
56 numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, |
|
57 576, 2916] |
|
58 |
|
59 for each in numbers: |
|
60 print "Square root of", each, "is", sqrt(each) |
|
61 |
|
62 print "This is not in for loop!" |
|
63 #+end_src |
|
64 * Save \& run script |
|
65 - Save the script as ~list_roots.py~ |
|
66 - Run in ~ipython~ interpreter as, |
|
67 : In []: %run -i list_roots.py |
|
68 * Exercise 2 |
|
69 From the given numbers make a list of perfect squares and a list of those which are not. |
|
70 : |
|
71 The numbers are, |
|
72 : 7225, 3268, 3364, 2966, 7056, 5625, 729, 5547, |
|
73 : 7056, 576, 2916 |
|
74 * Exercise 3 (indentation in ~ipython~) |
|
75 Print the square root of numbers in the list. |
|
76 : |
|
77 Numbers are, |
|
78 : 7225, 3268, 3364, 2966, 7056, 5625, 729, 5547, |
|
79 : 7056, 576, 2916 |
|
80 * Indentation in ~ipython~ |
|
81 : In []: numbers = [1369, 7225, 3364, 7056, 5625, |
|
82 : ...: 729, 7056, 576, 2916] |
|
83 |
|
84 : In []: for each in numbers: |
|
85 : ...: |
|
86 Note the four spaces here |
|
87 : |
|
88 : |
|
89 : |
|
90 : |
|
91 : |
|
92 : |
|
93 * Indentation in ~ipython~ (cont'd) |
|
94 : In []: numbers = [1369, 7225, 3364, 7056, 5625, |
|
95 : ...: 729, 7056, 576, 2916] |
|
96 : In []: for each in numbers: |
|
97 : ...: |
|
98 Note the four spaces here |
|
99 : |
|
100 Now type the rest of the code |
|
101 : ...: print "Square root of", each, |
|
102 : ...: print "is", sqrt(each) |
|
103 : ...: |
|
104 : ...: |
|
105 * Indentation in ~python~ interpreter |
|
106 Find out the cube of all the numbers from 1 to 10. |
|
107 : |
|
108 /do it in the python interpreter/ |
|
109 * Indentation in ~python~ interpreter (cont'd) |
|
110 #+begin_src python |
|
111 >>> for i in range(1, 11): |
|
112 ... print i, "cube is", i**3 |
|
113 ... |
|
114 #+end_src |
|
115 * ~range()~ function |
|
116 - in built function in Python |
|
117 - generates a list of integers |
|
118 - /syntax:/ range([start,] stop[, step]) |
|
119 - /example:/ |
|
120 - range(1, 20) - /generates integers from 1 to 20/ |
|
121 - range(20) - /generates integers from 0 to 20/ |
|
122 * Exercise 4 |
|
123 Print all the odd numbers from 1 to 50. |
|
124 * Summary |
|
125 - blocks in ~python~ |
|
126 - indentation |
|
127 - blocks in ~ipython~ interpreter |
|
128 - ~for~ loop |
|
129 - iterating over list using ~for~ loop |
|
130 - ~range()~ function |
|
131 * Thank you! |
|
132 #+begin_latex |
|
133 \begin{block}{} |
|
134 \begin{center} |
|
135 This spoken tutorial has been produced by the |
|
136 \textcolor{blue}{FOSSEE} team, which is funded by the |
|
137 \end{center} |
|
138 \begin{center} |
|
139 \textcolor{blue}{National Mission on Education through \\ |
|
140 Information \& Communication Technology \\ |
|
141 MHRD, Govt. of India}. |
|
142 \end{center} |
|
143 \end{block} |
|
144 #+end_latex |
|
145 |
|
146 |