|
| 1 | +examples |
| 2 | + |
| 3 | +:: |
| 4 | + >>> 1 + 1 |
| 5 | + 2 |
| 6 | + |
| 7 | + >>> 2 * 3 |
| 8 | + 6 |
| 9 | + |
| 10 | + >>> 2.5 * 3 |
| 11 | + 7.5 |
| 12 | + |
| 13 | + >>> type(1) |
| 14 | + <type 'int'> |
| 15 | + |
| 16 | + >>> type(7.5) |
| 17 | + <type 'float'> |
| 18 | + |
| 19 | + >>> a = 13 |
| 20 | + |
| 21 | + >>> type(a) |
| 22 | + <type 'int'> |
| 23 | + |
| 24 | + >>> 4.0/3 |
| 25 | + 1.3333333333333333 |
| 26 | + |
| 27 | + >>> a = 4.0/3 |
| 28 | + |
| 29 | + >>> a |
| 30 | + 1.3333333333333333 |
| 31 | + |
| 32 | + >>> type(a) |
| 33 | + <type 'float'> |
| 34 | + |
| 35 | + |
| 36 | +tipul este asociat valorii, nu variabilei |
| 37 | + |
| 38 | +:: |
| 39 | + >>> type("Hello") |
| 40 | + <type 'str'> |
| 41 | + |
| 42 | + >>> "Hello" + "world!" |
| 43 | + 'Helloworld!' |
| 44 | + |
| 45 | + >>> "Your lucky number is %d" % 13 |
| 46 | + 'Your lucky number is 13' |
| 47 | + |
| 48 | + >>> "Your other number is %d" % a |
| 49 | + 'Your other number is 1' |
| 50 | + |
| 51 | + >>> "Your other number is %f" % a |
| 52 | + 'Your other number is 1.333333' |
| 53 | + |
| 54 | + >>> "Your other number is %.2f" % a |
| 55 | + 'Your other number is 1.33' |
| 56 | + |
| 57 | + >>> msg = "Your other number is %.2f" % a |
| 58 | + |
| 59 | + >>> len(msg) |
| 60 | + 25 |
| 61 | + |
| 62 | + >>> msg[0] |
| 63 | + 'Y' |
| 64 | + |
| 65 | + >>> msg[1] |
| 66 | + 'o' |
| 67 | + |
| 68 | + >>> msg[-1] |
| 69 | + '3' |
| 70 | + |
| 71 | + >>> msg[12:17] |
| 72 | + 'umber' |
| 73 | + |
| 74 | + |
| 75 | +string-urile au metode |
| 76 | + |
| 77 | +:: |
| 78 | + >>> msg.lower() |
| 79 | + 'your other number is 1.33' |
| 80 | + |
| 81 | + >>> msg.find("number") |
| 82 | + 11 |
| 83 | + |
| 84 | + >>> msg.split(" ") |
| 85 | + ['Your', 'other', 'number', 'is', '1.33'] |
| 86 | + |
| 87 | + |
| 88 | +am primit o listă! putem defini propriile liste |
| 89 | + |
| 90 | +:: |
| 91 | + >>> l = ['ana', 'are', 'mere'] |
| 92 | + |
| 93 | + >>> l |
| 94 | + ['ana', 'are', 'mere'] |
| 95 | + |
| 96 | + >>> type(l) |
| 97 | + <type 'list'> |
| 98 | + |
| 99 | + >>> l[0] |
| 100 | + 'ana' |
| 101 | + |
| 102 | + >>> type(l[0]) |
| 103 | + <type 'str'> |
| 104 | + |
| 105 | + >>> l.pop() |
| 106 | + 'mere' |
| 107 | + |
| 108 | + >>> l.append("cartofi") |
| 109 | + |
| 110 | + >>> l |
| 111 | + ['ana', 'are', 'cartofi'] |
| 112 | + |
| 113 | + >>> l.extend(["si", 3, "mere"]) |
| 114 | + |
| 115 | + >>> l |
| 116 | + ['ana', 'are', 'cartofi', 'si', 3, 'mere'] |
| 117 | + |
| 118 | +listele primesc conținut de orice type |
| 119 | + |
| 120 | +:: |
| 121 | + >>> type(l[4]) |
| 122 | + <type 'int'> |
| 123 | + |
| 124 | + >>> ' '.join(l) |
| 125 | + Traceback (most recent call last): |
| 126 | + File "<stdin>", line 1, in <module> |
| 127 | + TypeError: sequence item 4: expected string, int found |
| 128 | + |
| 129 | +type checking la runtime |
| 130 | + |
| 131 | +:: |
| 132 | + >>> l.pop(4) |
| 133 | + 3 |
| 134 | + |
| 135 | + >>> ' '.join(l) |
| 136 | + 'ana are cartofi si mere' |
| 137 | + |
| 138 | +remember, string-urile au metode |
| 139 | + |
| 140 | +:: |
| 141 | + >>> ship = {'name': "Enterprise", 'enemies': ['ferengi', 'cardassian']} |
| 142 | + |
| 143 | + >>> len(ship) |
| 144 | + 2 |
| 145 | + |
| 146 | + >>> ship.keys() |
| 147 | + ['name', 'enemies'] |
| 148 | + |
| 149 | + >>> type(ship['name']) |
| 150 | + <type 'str'> |
| 151 | + |
| 152 | + >>> type(ship['enemies']) |
| 153 | + <type 'list'> |
| 154 | + |
| 155 | +primește orice fel de valori |
| 156 | + |
| 157 | +:: |
| 158 | + >>> ship['enemies'].append('borg') |
| 159 | + |
| 160 | + >>> ship |
| 161 | + {'name': 'Enterprise', 'enemies': ['ferengi', 'cardassian', 'borg']} |
| 162 | + |
| 163 | + >>> ship[1] = 'Riker' |
| 164 | + |
| 165 | + >>> ship |
| 166 | + {1: 'Riker', 'name': 'Enterprise', 'enemies': ['ferengi', 'cardassian', 'borg']} |
| 167 | + |
| 168 | +primește integer la chei (în general non-mutable values) |
| 169 | + |
| 170 | +:: |
| 171 | + >>> ship[l] = "hello" |
| 172 | + Traceback (most recent call last): |
| 173 | + File "<stdin>", line 1, in <module> |
| 174 | + TypeError: unhashable type: 'list' |
| 175 | + |
| 176 | + >>> del ship['enemies'] |
| 177 | + |
| 178 | + >>> ship |
| 179 | + {1: 'Riker', 'name': 'Enterprise'} |
| 180 | + |
| 181 | + |
| 182 | +if/else, for |
| 183 | + |
| 184 | +:: |
| 185 | + >>> if 2 < 3: |
| 186 | + ... print "doi e mai mic decat trei!" |
| 187 | + |
| 188 | + >>> if False: |
| 189 | + ... print "WTF?!" |
| 190 | + ... else: |
| 191 | + ... print "totul e ok" |
| 192 | + |
| 193 | + >>> for c in range(30): |
| 194 | + ... if c % 2 == 0: |
| 195 | + ... print "Numar par:", c |
| 196 | + |
| 197 | + >>> l = [c**2 for c in range(20)] |
| 198 | + >>> for c in l: |
| 199 | + ... print 'numarul este', c |
0 commit comments