author | Christopher Burns <chris.d.burns@gmail.com> |
Wed, 16 Jun 2010 23:19:09 -0700 | |
branch | scipy2010 |
changeset 403 | 9b8f03e012ba |
parent 346 | e9961fb16c58 |
permissions | -rwxr-xr-x |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
1 |
\documentclass[12pt]{article} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
2 |
|
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
3 |
|
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
4 |
\title{Python: Basics} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
5 |
\author{FOSSEE} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
6 |
\usepackage{listings} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
7 |
\lstset{language=Python, |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
8 |
basicstyle=\ttfamily, |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
9 |
commentstyle=\itshape\bfseries, |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
10 |
showstringspaces=false |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
11 |
} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
12 |
\newcommand{\typ}[1]{\lstinline{#1}} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
13 |
\usepackage[english]{babel} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
14 |
\usepackage[latin1]{inputenc} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
15 |
\usepackage{times} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
16 |
\usepackage[T1]{fontenc} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
17 |
\usepackage{ae,aecompl} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
18 |
\usepackage{mathpazo,courier,euler} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
19 |
\usepackage[scaled=.95]{helvet} |
346
e9961fb16c58
Added package url to cheatsheets.
Puneeth Chaganti <punchagan@fossee.in>
parents:
330
diff
changeset
|
20 |
\usepackage{url} |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
21 |
|
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
22 |
\begin{document} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
23 |
\date{} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
24 |
\vspace{-1in} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
25 |
\begin{center} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
26 |
\LARGE{Python: Basics}\\ |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
27 |
\large{FOSSEE} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
28 |
\end{center} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
29 |
\section{Data types} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
30 |
\subsection{int and float} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
31 |
A whole number is a \typ{int} variable. |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
32 |
\begin{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
33 |
In []: a = 13 |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
34 |
In []: type(a) |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
35 |
Out[]: <type 'int'> |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
36 |
In []: b = -2 |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
37 |
In []: type(b) |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
38 |
Out[]: <type 'int'> |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
39 |
In []: c = 500000000 |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
40 |
In []: type(c) |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
41 |
Out[]: <type 'int'> |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
42 |
\end{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
43 |
A number with decimal is a \typ{float}. |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
44 |
\begin{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
45 |
In []: p = 3.141592 |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
46 |
In []: type(p) |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
47 |
Out[]: <type 'float'> |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
48 |
\end{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
49 |
\subsection{Complex Numbers} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
50 |
\begin{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
51 |
In []: c = 3+4j #coeff of j specifies imaginary part |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
52 |
In []: abs(c) #absolute value of complex number |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
53 |
Out[]: 5.0 |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
54 |
In []: c.imag #accessing imaginary part of c |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
55 |
Out[]: 4.0 |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
56 |
In []: c.real #accessing real part of c |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
57 |
Out[]: 3.0 |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
58 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
59 |
\newpage |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
60 |
\subsection{Boolean} |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
61 |
\begin{lstlisting} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
62 |
In []: a = False |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
63 |
In []: b = True |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
64 |
In []: c = True |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
65 |
In []: (a and b) or c #Boolean operations |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
66 |
Out[]: True |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
67 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
68 |
\textbf{Note:} Python is case sensitive language, \typ{True} is \typ{bool} type, but \typ{true} would be a variable. and hence following assignment fails:\\ |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
69 |
\typ{In []: a = true}\\ |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
70 |
\subsection{Strings} |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
71 |
\begin{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
72 |
In []: w = "hello" #w is string variable |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
73 |
In []: print w[1] |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
74 |
Out[]: e |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
75 |
In []: print w[-1] #last character of string |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
76 |
Out[]: o |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
77 |
\end{lstlisting} |
327
c78cad28c2f7
modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
321
diff
changeset
|
78 |
\textbf{Note:} For a string variable, individual elements can be accessed using indices.\\ |
c78cad28c2f7
modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
321
diff
changeset
|
79 |
\textbf{Note:} All slicing and striding operations works with strings as well. |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
80 |
\begin{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
81 |
In []: len(w) #function to calculate length of string |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
82 |
Out[]: 5 |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
83 |
In []: w[0] = 'H' # ERROR: Strings are immutable |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
84 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
85 |
\subsection{String methods} |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
86 |
\begin{lstlisting} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
87 |
In []: a = 'Hello World' |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
88 |
In []: a.startswith('Hell') # 'a' starts with 'Hell' |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
89 |
Out[]: True |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
90 |
In []: a.endswith('ld') # 'a' ends with 'ld' |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
91 |
Out[]: True |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
92 |
In []: a.upper() # all characters to upper case |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
93 |
Out[]: 'HELLO WORLD' |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
94 |
In []: a.lower() # all characters to lower case |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
95 |
Out[]: 'hello world' |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
96 |
In []: ''.join(['a', 'b', 'c']) |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
97 |
Out[]: 'abc' |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
98 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
99 |
\typ{join} function joins all the list member passed as argument with the string it is called upon. In above case it is \typ{empty string}. |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
100 |
\begin{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
101 |
In []: ' '.join(['a','b','c']) |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
102 |
Out[]: 'a b c' |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
103 |
In []: ','.join(['a','b','c']) |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
104 |
Out[]: 'a,b,c' |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
105 |
\end{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
106 |
\subsection{String formatting} |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
107 |
\begin{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
108 |
In []: x, y = 1, 1.234 #initializing two variables |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
109 |
In []: 'x is %s, y is %s' %(x, y) |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
110 |
Out[]: 'x is 1, y is 1.234' |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
111 |
\end{lstlisting} |
330
46533051b9d3
commited changes made for scipy and cheatsheats.
Shantanu <shantanu@fossee.in>
parents:
329
diff
changeset
|
112 |
\textbf{Note:} \typ{\%s} used in above formatting specifies \typ{'str'} representation of variables. One can also try:\\ |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
113 |
\typ{\%d} for \typ{int} representation\\ |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
114 |
\typ{\%f} for \typ{float} representation |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
115 |
\begin{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
116 |
In []: 'x is %f, y is %f' %(x, y) |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
117 |
Out[]: 'x is 1.000000, y is 1.234000' |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
118 |
|
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
119 |
In []: 'x is %d, y is %d' %(x, y) |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
120 |
Out[]: 'x is 1, y is 1' |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
121 |
\end{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
122 |
\subsection{Arithmetic Operators} |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
123 |
\begin{lstlisting} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
124 |
In []: 45 % 2 # Modulo operator |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
125 |
Out[]: 1 |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
126 |
In []: 5 ** 3 # Power |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
127 |
Out[]: 125 |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
128 |
In []: a = 5 |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
129 |
In []: a += 1 #increment by 1, translates to a = a + 1 |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
130 |
In []: a *= 2 |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
131 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
132 |
\subsection{String Operations} |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
133 |
\begin{lstlisting} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
134 |
In []: s = 'Hello' |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
135 |
In []: p = 'World' |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
136 |
In []: s + p #concatenating two strings |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
137 |
Out[]: 'HelloWorld' |
329
0a6ab1d81491
Some more changes for day 2 sessions.
shantanu <shantanu@fossee.in>
parents:
327
diff
changeset
|
138 |
In []: s * 4 #repeat string for given number of times |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
139 |
Out[]: 'HelloHelloHelloHello' |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
140 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
141 |
\subsection{Relational and Logical Operators} |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
142 |
\begin{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
143 |
In []: p, z, n = 1, 0, -1 #initializing three variables |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
144 |
In []: p == n #equivalency check |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
145 |
Out[]: False |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
146 |
In []: p >= n |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
147 |
Out[]: True |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
148 |
In []: n < z < p #finding largest number among three |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
149 |
Out[]: True |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
150 |
In []: p + n != z |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
151 |
Out[]: False |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
152 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
153 |
\subsection{Built-ins} |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
154 |
\begin{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
155 |
In []: int(17 / 2.0) #converts arguments to integer |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
156 |
Out[]: 8 |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
157 |
In []: float(17 / 2) #argument is already integer(17 / 2 = 8) |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
158 |
Out[]: 8.0 |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
159 |
In []: str(17 / 2.0) #converts to string |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
160 |
Out[]: '8.5' |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
161 |
In []: round( 7.5 ) |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
162 |
Out[]: 8.0 |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
163 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
164 |
\subsection{Console Input} |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
165 |
\begin{lstlisting} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
166 |
In []: a = raw_input('Enter a value: ') |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
167 |
Enter a value: 5 |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
168 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
169 |
\textbf{Note:} \typ{raw_input} always returns string representation of user input and hence: |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
170 |
\begin{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
171 |
In []: type(a) |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
172 |
Out[]: <type 'str'> |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
173 |
\end{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
174 |
To get integer or floating point of this input, one has to perform type conversion:\\ |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
175 |
\typ{In []: b = int(a)} |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
176 |
\section{Conditionals} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
177 |
\typ{if} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
178 |
\begin{lstlisting} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
179 |
In []: x = int(raw_input("Enter an integer:")) |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
180 |
In []: if x < 0: |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
181 |
...: print 'Be positive!' |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
182 |
...: elif x == 0: |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
183 |
...: print 'Zero' |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
184 |
...: elif x == 1: |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
185 |
...: print 'Single' |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
186 |
...: else: |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
187 |
...: print 'More' |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
188 |
\end{lstlisting} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
189 |
Ternary Operator |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
190 |
\begin{lstlisting} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
191 |
In []: a = raw_input('Enter number(Q to quit):') |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
192 |
In []: num = int(a) if a != 'Q' else 0 |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
193 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
194 |
Above statement can be read as ``num is int of a, if a is not equal to 'Q', otherwise 0 `` |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
195 |
\section{Links and References} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
196 |
\begin{itemize} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
197 |
\item Reference manual to describe the standard libraries that are distributed with Python is available at \url{http://docs.python.org/library/} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
198 |
\item To read more on strings refer to: \\ \url{http://docs.python.org/library/stdtypes.html#string-methods} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
199 |
\end{itemize} |
301
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
200 |
\end{document} |