|
1 \documentclass[12pt]{article} |
|
2 |
|
3 |
|
4 \title{Python: Basics} |
|
5 \author{FOSSEE} |
|
6 \usepackage{listings} |
|
7 \lstset{language=Python, |
|
8 basicstyle=\ttfamily, |
|
9 commentstyle=\itshape\bfseries, |
|
10 showstringspaces=false |
|
11 } |
|
12 \newcommand{\typ}[1]{\lstinline{#1}} |
|
13 \usepackage[english]{babel} |
|
14 \usepackage[latin1]{inputenc} |
|
15 \usepackage{times} |
|
16 \usepackage[T1]{fontenc} |
|
17 \usepackage{ae,aecompl} |
|
18 \usepackage{mathpazo,courier,euler} |
|
19 \usepackage[scaled=.95]{helvet} |
|
20 |
|
21 \begin{document} |
|
22 \date{} |
|
23 \vspace{-1in} |
|
24 \begin{center} |
|
25 \LARGE{Python: Basics}\\ |
|
26 \large{FOSSEE} |
|
27 \end{center} |
|
28 \section{Data types} |
|
29 Complex Numbers |
|
30 \begin{lstlisting} |
|
31 In []: c = 3+4j |
|
32 In []: abs(c) |
|
33 Out[]: 5.0 |
|
34 In []: c.imag |
|
35 Out[]: 4.0 |
|
36 In []: c.real |
|
37 Out[]: 3.0 |
|
38 \end{lstlisting} |
|
39 Boolean |
|
40 \begin{lstlisting} |
|
41 In []: a = False |
|
42 In []: b = True |
|
43 In []: c = True |
|
44 In []: (a and b) or c |
|
45 Out[]: True |
|
46 \end{lstlisting} |
|
47 Strings |
|
48 \begin{lstlisting} |
|
49 In []: w = "hello" |
|
50 In []: print w[0] + w[2] + w[-1] |
|
51 Out[]: hlo |
|
52 In []: len(w) |
|
53 Out[]: 5 |
|
54 In []: w[0] = 'H' # ERROR: Strings are immutable |
|
55 \end{lstlisting} |
|
56 String methods |
|
57 \begin{lstlisting} |
|
58 In []: a = 'Hello World' |
|
59 In []: a.startswith('Hell') # 'a' starts with 'Hell' |
|
60 In []: a.endswith('ld') # 'a' ends with 'ld' |
|
61 In []: a.upper() # all characters to upper case |
|
62 In []: a.lower() # all characters to lower case |
|
63 In []: ''.join(['a', 'b', 'c']) |
|
64 Out[]: 'abc' |
|
65 \end{lstlisting} |
|
66 String formatting |
|
67 \begin{lstlisting} |
|
68 In []: x, y = 1, 1.234 |
|
69 In []: 'x is %s, y is %s' %(x, y) |
|
70 Out[]: 'x is 1, y is 1.234' |
|
71 \end{lstlisting} |
|
72 Arithmetic Operators |
|
73 \begin{lstlisting} |
|
74 In []: 45 % 2 # Modulo operator |
|
75 Out[]: 1 |
|
76 In []: 1234567891234567890 ** 3 # Power |
|
77 In []: a = 5 |
|
78 In []: a += 1 |
|
79 In []: a *= 2 |
|
80 \end{lstlisting} |
|
81 String Operations |
|
82 \begin{lstlisting} |
|
83 In []: s = 'Hello' |
|
84 In []: p = 'World' |
|
85 In []: s + p |
|
86 Out[]: 'HelloWorld' |
|
87 In []: s * 4 |
|
88 Out[]: 'HelloHelloHelloHello' |
|
89 \end{lstlisting} |
|
90 Relational and Logical Operators |
|
91 \begin{lstlisting} |
|
92 In []: p, z, n = 1, 0, -1 |
|
93 In []: p == n |
|
94 Out[]: False |
|
95 In []: p >= n |
|
96 Out[]: True |
|
97 In []: n < z < p |
|
98 Out[]: True |
|
99 In []: p + n != z |
|
100 Out[]: False |
|
101 \end{lstlisting} |
|
102 Built-ins |
|
103 \begin{lstlisting} |
|
104 In []: int(17 / 2.0) |
|
105 Out[]: 8 |
|
106 In []: float(17 / 2) |
|
107 Out[]: 8.0 |
|
108 In []: str(17 / 2.0) |
|
109 Out[]: '8.5' |
|
110 In []: round( 7.5 ) |
|
111 Out[]: 8.0 |
|
112 \end{lstlisting} |
|
113 Console Input |
|
114 \begin{lstlisting} |
|
115 In []: a = raw_input('Enter a value: ') |
|
116 Enter a value: 5 |
|
117 \end{lstlisting} |
|
118 \section{Conditionals} |
|
119 \typ{if} |
|
120 \begin{lstlisting} |
|
121 In []: x = int(raw_input("Enter an integer:")) |
|
122 In []: if x < 0: |
|
123 ...: print 'Be positive!' |
|
124 ...: elif x == 0: |
|
125 ...: print 'Zero' |
|
126 ...: elif x == 1: |
|
127 ...: print 'Single' |
|
128 ...: else: |
|
129 ...: print 'More' |
|
130 \end{lstlisting} |
|
131 Ternary Operator |
|
132 \begin{lstlisting} |
|
133 In []: a = raw_input('Enter number(Q to quit):') |
|
134 In []: num = int(a) if a != 'Q' else 0 |
|
135 \end{lstlisting} |
|
136 \end{document} |