author | shantanu <shantanu@fossee.in> |
Tue, 08 Dec 2009 13:06:14 +0530 | |
changeset 329 | 0a6ab1d81491 |
parent 327 | c78cad28c2f7 |
child 330 | 46533051b9d3 |
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} |
49bdffe4dca5
Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
20 |
|
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 |
\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
|
22 |
\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
|
23 |
\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
|
24 |
\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
|
25 |
\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
|
26 |
\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
|
27 |
\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
|
28 |
\section{Data types} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
29 |
\subsection{int and float} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
30 |
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
|
31 |
\begin{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
32 |
In []: a = 13 |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
33 |
In []: type(a) |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
34 |
Out[]: <type 'int'> |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
35 |
In []: b = -2 |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
36 |
In []: type(b) |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
37 |
Out[]: <type 'int'> |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
38 |
In []: c = 500000000 |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
39 |
In []: type(c) |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
40 |
Out[]: <type 'int'> |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
41 |
\end{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
42 |
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
|
43 |
\begin{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
44 |
In []: p = 3.141592 |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
45 |
In []: type(p) |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
46 |
Out[]: <type 'float'> |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
47 |
\end{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
48 |
\subsection{Complex Numbers} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
49 |
\begin{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
50 |
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
|
51 |
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
|
52 |
Out[]: 5.0 |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
53 |
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
|
54 |
Out[]: 4.0 |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
55 |
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
|
56 |
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
|
57 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
58 |
\newpage |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
59 |
\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
|
60 |
\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
|
61 |
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
|
62 |
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
|
63 |
In []: c = True |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
64 |
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
|
65 |
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
|
66 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
67 |
\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
|
68 |
\typ{In []: a = true}\\ |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
69 |
\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
|
70 |
\begin{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
71 |
In []: w = "hello" #w is string variable |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
72 |
In []: print w[1] |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
73 |
Out[]: e |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
74 |
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
|
75 |
Out[]: o |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
76 |
\end{lstlisting} |
327
c78cad28c2f7
modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
321
diff
changeset
|
77 |
\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
|
78 |
\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
|
79 |
\begin{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
80 |
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
|
81 |
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
|
82 |
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
|
83 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
84 |
\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
|
85 |
\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
|
86 |
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
|
87 |
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
|
88 |
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
|
89 |
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
|
90 |
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
|
91 |
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
|
92 |
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
|
93 |
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
|
94 |
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
|
95 |
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
|
96 |
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
|
97 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
98 |
\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
|
99 |
\begin{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
100 |
In []: ' '.join(['a','b','c']) |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
101 |
Out[]: 'a b c' |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
102 |
In []: ','.join(['a','b','c']) |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
103 |
Out[]: 'a,b,c' |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
104 |
\end{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
105 |
\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
|
106 |
\begin{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
107 |
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
|
108 |
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
|
109 |
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
|
110 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
111 |
\textbf{Note:} \typ{\%s} used in above fomatting specifies \typ{'str'} representation of variables. One can also try:\\ |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
112 |
\typ{\%d} for \typ{int} representation\\ |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
113 |
\typ{\%f} for \typ{float} representation |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
114 |
\begin{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
115 |
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
|
116 |
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
|
117 |
|
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
118 |
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
|
119 |
Out[]: 'x is 1, y is 1' |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
120 |
\end{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
121 |
\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
|
122 |
\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
|
123 |
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
|
124 |
Out[]: 1 |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
125 |
In []: 5 ** 3 # Power |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
126 |
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
|
127 |
In []: a = 5 |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
128 |
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
|
129 |
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
|
130 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
131 |
\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
|
132 |
\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
|
133 |
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
|
134 |
In []: p = 'World' |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
135 |
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
|
136 |
Out[]: 'HelloWorld' |
329
0a6ab1d81491
Some more changes for day 2 sessions.
shantanu <shantanu@fossee.in>
parents:
327
diff
changeset
|
137 |
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
|
138 |
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
|
139 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
140 |
\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
|
141 |
\begin{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
142 |
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
|
143 |
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
|
144 |
Out[]: False |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
145 |
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
|
146 |
Out[]: True |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
147 |
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
|
148 |
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
|
149 |
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
|
150 |
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
|
151 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
152 |
\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
|
153 |
\begin{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
154 |
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
|
155 |
Out[]: 8 |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
156 |
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
|
157 |
Out[]: 8.0 |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
158 |
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
|
159 |
Out[]: '8.5' |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
160 |
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
|
161 |
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
|
162 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
163 |
\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
|
164 |
\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
|
165 |
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
|
166 |
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
|
167 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
168 |
\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
|
169 |
\begin{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
170 |
In []: type(a) |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
171 |
Out[]: <type 'str'> |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
172 |
\end{lstlisting} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
173 |
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
|
174 |
\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
|
175 |
\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
|
176 |
\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
|
177 |
\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
|
178 |
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
|
179 |
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
|
180 |
...: 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
|
181 |
...: 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
|
182 |
...: 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
|
183 |
...: 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
|
184 |
...: 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
|
185 |
...: 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
|
186 |
...: 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
|
187 |
\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
|
188 |
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
|
189 |
\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
|
190 |
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
|
191 |
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
|
192 |
\end{lstlisting} |
321
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
193 |
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
|
194 |
\section{Links and References} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
195 |
\begin{itemize} |
8bf99f747817
Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents:
301
diff
changeset
|
196 |
\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
|
197 |
\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
|
198 |
\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
|
199 |
\end{document} |