Para trabalhar com números, temos os operadores +, -, *, /, como em qualquer outra linguagem.
Podemos atribuir um número a uma variável utilizando o sinal = e podemos associar um número a várias variáveis ao mesmo tempo.
>>> width = 20 >>> height = 5*9 >>> width * height 900 >>> x = y = z = 0 # Zero x, y and z >>> x 0 >>> y 0 >>> z 0
Assim como números, temos as strings:
>>> 'spam eggs' 'spam eggs' >>> 'doesn\'t' "doesn't" >>> "doesn't" "doesn't" >>> '"Yes," he said.' '"Yes," he said.' >>> "\"Yes,\" he said." '"Yes," he said.' >>> '"Isn\'t," she said.' '"Isn\'t," she said.'
Podemos ter strings por mais de uma linha de duas formas:
hello = "This is a rather long string containing\n\ several lines of text just as you would do in C.\n\ Note that whitespace at the beginning of the line is\ significant." print hello print """ Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to """
Strings podem ser contatenadas com o operador + e repetida com *. Podemos trabalhar com Strings utilizando índices:
>>> word = 'Help' + 'A' >>> word 'HelpA' >>> '(' + word*5 + ')' '(helpahelpahelpahelpahelpa)' >>> word[4] 'A' >>> word[0:2] 'He' >>> word[2:4] 'lp' >>> word[:2] # Os primeiros dois caracteres 'He' >>> word[2:] # Tudo menos os primeiros dois caracteres 'lpA' >>> word[-1] # O último caracter 'A' >>> word[-2] # O penúltimo caracter 'p' >>> word[-2:] # Os últimos dois caracteres 'pA' >>> word[:-2] # Tudo menos os dois caracteres 'Hel'
Também temos a função len():
>>> s = 'supercalifragilisticexpialidocious' >>> len(s)
No próximo post, estarei falando sobre estruturas de controle em Python.