Sysop.Fr
Cat├ęgories

apache

awk

bash

charmap

date

echo

encoding

find

grep

hexdump

irc

iso3166

ls

mysql

printf

python

read

sed

seq

smpp

sort

tar

ucp

Articles

Variables: type, affectation et tests

1. Le type int

print(1 + 2)  # adition
print(5 - 3)  # soustraction
print(3 * 5)  # multiplication
print(2 ** 3) # exposant
print(5 / 2)  # division
print(5 // 2) # division entiere
print(20 % 3) # modulo
print(20 % 5) # modulo
            
3
2
15
8
2.5
2
2
0
            
print("convert from int(40) to float {0:.2f}".format(40))
print("convert from int(40) to hex {0:x} or {0:#x}".format(40))
print("convert from int(40) to bin {:0>8b}".format(40))
print("convert from int(40) to oct {0:o}".format(40))
            
convert from int(40) to float 40.00
convert from int(40) to hex 28 or 0x28
convert from int(40) to bin 00101000
convert from int(40) to oct 50
            

2. Le type bool

Deux valeurs possibles : False ou 0, True ou 1.

3. Affectation de valeurs

a = 1a reçoit la valeur 2
a = 1 + 2a reçoit le résultat de 1 + 2
a = a + 1a est incrémenté de 1 (idem que a += 1)
a = a - 1a est décrémenté de 1 (idem que a -= 1)
a, b = 1, 2a reçoit la valeur 1 et b reçoit la valeur 2 (tuple)
a, b = ['A', 'B']a reçoit la valeur 'A' et b reçoit la valeur 'B' (liste)
c = a + bc reçoit la valeur 'AB'

4. Tests de variables

if 1 < 2:True si 1 inférieur à 2
if 2 > 1:True si 2 supérieur à 1
if 1 <= 2:True si 1 inférieur ou égal à 2
if 2 >= 1:True si 2 supérieur ou égal à 1
if 2 == 2:True si 2 égal à 2
if 2 != 1:True si 1 différent de 2
if myvar:True si myvar égal à True ou 1

4.1 Test d'une valeur booléenne

if estPair: # mieux que if estPair == True:
    print("La condition est vraie")
            
request_active = 0
if request_active == 1:
    print('request_active=1 ({!r})'.format(request_active))
elif request_active == 0:
    print('request_active=0 ({!r})'.format(request_active))
elif request_active == False:
    print('request_active=False ({!r})'.format(request_active))
else:
    print('request_active="" ({!r})'.format(request_active))

request_active = 1
if request_active == 1:
    print('request_active=1 ({!r})'.format(request_active))
elif request_active == 0:
    print('request_active=0 ({!r})'.format(request_active))
elif request_active == False:
    print('request_active=False ({!r})'.format(request_active))
else:
    print('request_active="" ({!r})'.format(request_active))

request_active = ''
if request_active == 1:
    print('request_active=1 ({!r})'.format(request_active))
elif request_active == 0:
    print('request_active=0 ({!r})'.format(request_active))
elif request_active == False:
    print('request_active=False ({!r})'.format(request_active))
else:
    print('request_active="" ({!r})'.format(request_active))

request_active = False
if request_active == 1:
    print('request_active=1 ({!r})'.format(request_active))
elif request_active == 0:
    print('request_active=0 ({!r})'.format(request_active))
elif request_active == False:
    print('request_active=False ({!r})'.format(request_active))
else:
    print('request_active="" ({!r})'.format(request_active))
            
request_active=0 (0)
request_active=1 (1)
request_active="" ('')
request_active=0 (False)
            

if - elif - else

x = int(input("Saisir une variable de type int: "))
if x < 0:
    print("x est négatif. Pour sa valeur absolue, on prend l'opposé")
elif x % 2:
    print("x est positif et impair")
else:
    print("x n'est pas négatif et est pair")
            

for

for lettre in 'mot':
    print(lettre)
            
m
o
t