Skip to content

Instantly share code, notes, and snippets.

@marrcandre
Created May 15, 2019 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marrcandre/95afa7f98a2ff729a27696bb6a8fc796 to your computer and use it in GitHub Desktop.
Save marrcandre/95afa7f98a2ff729a27696bb6a8fc796 to your computer and use it in GitHub Desktop.
/usr/bin/python3.6 /opt/pycharm-2019.1.1/helpers/pydev/pydevconsole.py --mode=client --port=33969
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['/home/marco/Dropbox/aulas/2019/2019-1/programação1/lista exercicios com teste'])
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
PyDev console: using IPython 5.5.0
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
In[2]: l = [1,2,3]
In[3]: for item in l:
...: print(item())
...:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-3-a9f55446aa43>", line 2, in <module>
print(item())
TypeError: 'int' object is not callable
In[4]: for item in l:
...: print(item)
...:
1
2
3
In[5]: nome = "Pernambuco"
In[6]: for letra in nome:
...: print(letra)
...:
P
e
r
n
a
m
b
u
c
o
In[7]: for letra in nome:
...: print(letra, end="")
...:
In[8]: for letra in nome:
...: print(letra, end="")
...:
In[9]: for letra in nome:
...: print(letra, end="")
...:
In[10]: for letra in nome:
...: print(letra)
...:
PernambucoPernambucoPernambucoP
e
r
n
a
m
b
u
c
o
In[11]: for letra in nome:
...: print(letra)
...: print()
P
e
r
n
a
m
b
u
c
o
In[12]: for letra in nome:
...: print(letra, end='')
...: print()
Pernambuco
In[13]: for letra in nome:
...: print(letra, end='.')
...: print()
P.e.r.n.a.m.b.u.c.o.
In[14]: l = 'jan fev mar abr mai jun ju ago set out nov dez'.split()
In[15]: l
Out[15]:
['jan',
'fev',
'mar',
'abr',
'mai',
'jun',
'ju',
'ago',
'set',
'out',
'nov',
'dez']
In[16]: for mes in l:
...: print(mes)
...:
jan
fev
mar
abr
mai
jun
ju
ago
set
out
nov
dez
In[17]: for mes in reversed(l):
...: print(mes)
...:
dez
nov
out
set
ago
ju
jun
mai
abr
mar
fev
jan
In[18]: l
Out[18]:
['jan',
'fev',
'mar',
'abr',
'mai',
'jun',
'ju',
'ago',
'set',
'out',
'nov',
'dez']
In[19]: for mes in sorted(l):
...: print(mes)
...:
abr
ago
dez
fev
jan
ju
jun
mai
mar
nov
out
set
In[20]: for i in range(10):
...: print('Bom dia BSI1!')
...:
Bom dia BSI1!
Bom dia BSI1!
Bom dia BSI1!
Bom dia BSI1!
Bom dia BSI1!
Bom dia BSI1!
Bom dia BSI1!
Bom dia BSI1!
Bom dia BSI1!
Bom dia BSI1!
In[21]: range(10)
Out[21]: range(0, 10)
In[22]: print(range(10))
...:
range(0, 10)
In[23]: list(range(10))
...:
Out[23]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In[24]: list(range(10))
...:
Out[24]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In[25]: for i in range(10):
...: print(i, 'Bom dia BSI1!')
...:
0 Bom dia BSI1!
1 Bom dia BSI1!
2 Bom dia BSI1!
3 Bom dia BSI1!
4 Bom dia BSI1!
5 Bom dia BSI1!
6 Bom dia BSI1!
7 Bom dia BSI1!
8 Bom dia BSI1!
9 Bom dia BSI1!
In[26]: for mes in l:
...: print(mes)
...:
jan
fev
mar
abr
mai
jun
ju
ago
set
out
nov
dez
In[27]: l[0]
Out[27]: 'jan'
In[28]: l[1]
Out[28]: 'fev'
In[29]: l[11]
Out[29]: 'dez'
In[30]: for i in range(len(l)):
...: print(i, l[i])
...:
0 jan
1 fev
2 mar
3 abr
4 mai
5 jun
6 ju
7 ago
8 set
9 out
10 nov
11 dez
In[31]: l
Out[31]:
['jan',
'fev',
'mar',
'abr',
'mai',
'jun',
'ju',
'ago',
'set',
'out',
'nov',
'dez']
In[32]: sal = [900, 600, 1800]
In[33]: sal[2] = sal[2] * 1.5
In[34]: sal
Out[34]: [900, 600, 2700.0]
In[35]: sal[1] = sal[1] * 1.5
In[36]: sal
Out[36]: [900, 900.0, 2700.0]
In[37]: sal = [900, 600, 1800]
In[38]: sal
Out[38]: [900, 600, 1800]
In[39]: for i, salario in enumerate(sal):
...: sal[i] = sal[i] * 1.10
...:
In[40]: sal
Out[40]: [990.0000000000001, 660.0, 1980.0000000000002]
In[41]: for i in range(len(l)):
...: print(i, l[i])
...:
0 jan
1 fev
2 mar
3 abr
4 mai
5 jun
6 ju
7 ago
8 set
9 out
10 nov
11 dez
In[42]: for mes in reversed(l): print(mes)
...:
dez
nov
out
set
ago
ju
jun
mai
abr
mar
fev
jan
In[43]: i = len(mes)
...: while i >= 0 :
...: print(mes[i])
...: i -= 1
...:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-43-6c0fc6ca25df>", line 3, in <module>
print(mes[i])
IndexError: string index out of range
In[44]: i = len(l)
...: while i >= 0:
...: print(l[i])
...: i -= 1
...:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-44-b01097cc6309>", line 3, in <module>
print(l[i])
IndexError: list index out of range
In[45]: i = len(l)
...: while i >= 0:
...: print(l[i])
...: i -= 1
...:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-45-b01097cc6309>", line 3, in <module>
print(l[i])
IndexError: list index out of range
In[46]: i
Out[46]: 12
In[47]: i
Out[47]: 12
In[48]: i = len(l) - 1
...: while i >= 0:
...: print(l[i])
...: i -= 1
...:
dez
nov
out
set
ago
ju
jun
mai
abr
mar
fev
jan
In[49]: for i in range(len(l)-1, -1, -1):
...: print(l[i])
...:
dez
nov
out
set
ago
ju
jun
mai
abr
mar
fev
jan
In[50]: for mes in reversed(l): print(mes)
...:
dez
nov
out
set
ago
ju
jun
mai
abr
mar
fev
jan
In[51]: sal
Out[51]: [990.0000000000001, 660.0, 1980.0000000000002]
In[52]: for salario in sorted(salarios):
...: print(salario)
...:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-52-0d01d4f83d71>", line 1, in <module>
for salario in sorted(salarios):
NameError: name 'salarios' is not defined
In[53]: salarios = sal
In[54]: for salario in sorted(salarios):
...: print(salario)
...:
660.0
990.0000000000001
1980.0000000000002
In[55]: for salario in reversed(sorted(salarios)):
...: print(salario)
...:
1980.0000000000002
990.0000000000001
660.0
In[56]: numero = 762345
In[57]: numero_str = str(numero)
In[58]: numero_str
Out[58]: '762345'
In[59]: for c in numero_str: print(c)
...:
7
6
2
3
4
5
In[60]: for c in numero: print(c)
...:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-60-8f1bb6297bc5>", line 1, in <module>
for c in numero: print(c)
TypeError: 'int' object is not iterable
In[61]: numero = 991675523
In[62]: soma = 0
In[63]: numero_str = str(numero)
In[64]: for digito in numero_str:
...: soma = soma + digito
...: print(soma)
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-64-effffb93a31b>", line 2, in <module>
soma = soma + digito
TypeError: unsupported operand type(s) for +: 'int' and 'str'
In[65]: for digito in numero_str:
...: soma = soma + int(digito)
...: print(soma)
47
In[66]: soma = 0
In[67]: for digito in numero_str:
...: soma = soma + int(digito)
...: print(soma)
47
In[68]: soma = 0
In[69]: for digito in numero_str:
...: soma += int(digito)
...: print(soma)
47
In[70]: contador = 0
In[71]: for digito in numero_str:
...: if digito == '9'
...: contador += 1
...: print(contador)
File "<ipython-input-71-20a6c0fb8008>", line 2
if digito == '9'
^
SyntaxError: invalid syntax
In[72]: for digito in numero_str:
...: if digito == '9':
...: contador += 1
...: print(contador)
2
In[73]: posicoes = []
In[74]: for i,digito in enumerate(numero_str):
...: if digito == '9':
...: posicoes.append(i)
...: print(posicoes)
[0, 1]
In[75]: posicoes = []
In[76]: for i,digito in enumerate(numero_str):
...: if digito == '5':
...: posicoes.append(i)
...: print(posicoes)
[5, 6]
In[77]: posicoes = []
In[78]: for i,digito in enumerate(numero_str):
...: if digito == '4':
...: posicoes.append(i)
...: print(posicoes)
[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment