Pythonista. Hola Python

Prefacio

Buen día, Habr. Estoy lanzando un curso de artículo corto que cubre las habilidades clave de Python necesarias para aprender ciencia de datos. Estos artículos son adecuados para aquellos que ya tenían experiencia en programación y desean agregar Python a su conjunto de habilidades.





¡Hola Python!

Python recibió su nombre del popular programa de televisión británico de comedia Monty Python's Flying Circus, ya que el autor era fanático del programa de televisión.





Solo por diversión, intente leer el código a continuación y predecir lo que hará al inicio. (Si no lo sabe, ¡está bien!) Está programado para un boceto de Monty Python sobre spam.





spam_amount = 0
print(spam_amount)

#  , , , ,    ( 4  )
spam_amount = spam_amount + 4

if spam_amount > 0:
    print("But I don't want ANY spam!")

viking_song = "Spam " * spam_amount
print(viking_song)
      
      



Output

0





But I don`t want ANY spam!





Spam Spam Spam Spam





, Python . .





spam_amount = 0
      
      



: spam_amount 0 =



, .  





: (, Java C ++), , Python :





spam_amount







• Python, spam_amount



. , spam_amount



, .





print(spam_amount)
      
      



: print



- Python, . , ( ) .





#  , , , ,    ( 4  )
spam_amount = spam_amount + 4
      
      



- . Python #



.





. , - - =



.





, spam_amount



,   . , Python =



(0 + 4 = 4), .





if spam_amount > 0:
    print("But I don't want ANY spam!")

viking_song = "Spam Spam Spam"
print(viking_song)
      
      



« » , , , , , , . Python .





, , if



. "But I don't want ANY spam! "



, spam_amount



. (, print (viking_song)



) . ( Python) ?





(:



) if



, « ». . {



}



. Python , , , , .





, viking_song



, 4 , if



. , .





​​Python:





"But I don't want ANY spam!"
      
      



. ( , Python, , .)





viking_song = "Spam " * spam_amount
print(viking_song)
      
      



*



(3 * 3



9), , , , , . Python , , *



+



, . ( - )





Python

, :





spam_amount = 0
      
      



«» - , , Python, , spam_amount



:





type(spam_amount)
      
      



int
      
      



int



- integer. , Python:





type(19.95)
      
      



float
      
      



float



- , , .





type()



- , ( print()



), . Python « ?».





- . +



*



. Python :

















a + b











a



b







a - b











a



b







a * b











a



b







a / b











a



b







a // b











a



b



,





a % b











a



b







a ** b











a



b







-a



















, , , , , Python . « » - , :





print(5 / 2)
print(6 / 2)
      
      



2.5
3.0
      
      



float







//



, .





print(5 // 2)
print(6 // 2)
      
      



2
3
      
      



, ?





, , , . , PEMDAS - , , /, / (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction).





Python , . . 





8 - 3 + 2
      
      



7
      
      



-3 + 4 * 2
      
      



5
      
      







hat_height_cm = 25
my_height_cm = 190
#        ?
total_height_meters = hat_height_cm + my_height_cm / 100
print("Height in meters =", total_height_meters, "?")
      
      



Height in meters = 26.9 ?
      
      



  . , Python .  





total_height_meters = (hat_height_cm + my_height_cm) / 100
print("Height in meters =", total_height_meters)
      
      



Height in meters = 2.15
      
      



Funciones integradas para trabajar con números

Funciona min



y max



devuelve el mínimo y máximo de sus argumentos, respectivamente:





print(min(1, 2, 3))
print(max(1, 2, 3))
      
      



1
3
      
      



La función abs



devuelve el valor absoluto de su argumento: 





print(abs(32))
print(abs(-32))
      
      



32
32
      
      



Además de los nombres de los dos tipos numéricos principales en Python, int



y float



también se pueden llamar como funciones que convierten sus argumentos al tipo apropiado:  





print(float(10))
print(int(3.33))
#      
print(int('807') + 1)
      
      



10.0
3
808
      
      



Epílogo

Con esto llegó a su fin el primer artículo. Gracias a todos los que leyeron y se tomaron el tiempo. También espero que haya aprendido información útil y aprendido algo nuevo. ¡Continúe desarrollando y aprendiendo cosas nuevas! Nos vemos pronto.








All Articles