Created for
we can use positive and negative integers:
>>> 42
42
>>> -42
-42
we can use positive and negative floating point numbers
>>> 3.1234
3.1234
>>> -0.255
-0.255
>>> .44
0.44
>>> -.55
-0.55
>>>
The division operator "/
" in python2 returns the integer part(i.e. works like Floor division), while in python3 it returns the float result
>>> 5-3
2
>>> 5*3
15
>>> 5/3
1.6666666666666667
>>> 5//3
1
>>> 5%3
2
>>> 5/3
1
>>> 5/3
1.6666666666666667
>>> 5//3
1
Python3 adds the "//
" operator for Integer division
math
module
import math
# do something with math
>>> import math
>>>
>>> math.pi
3.141592653589793
>>> math.floor(math.pi)
3
>>> math.pow(2,3)
8.0
>>> math.sqrt(9)
3.0
>>> "this is a single line string"
'this is a single line string'
>>> 'another single line string with UTF charactes like 🍷'
'another single line string with UTF charactes like 🍷'
>>> 'but can not be spred in multiple lines
File "", line 1
'but can not be spred in multiple lines
^
SyntaxError: EOL while scanning string literal
>>> """infact you can -
... if you use these triple quotes"""
'infact you can - \nif you use these triple quotes'
>>> '''or these triple quotes
... can separate multiline without errors'''
'or these triple quotes\ncan separate multiline without errors'
+
The operation is defined only on string operands:
#string concatenation with '+':
>>> "ala" + "bala"
'alabala
>>> "1" + "2"
'12'
>>> "1" + 3
Traceback ...
TypeError: cannot concatenate 'str' and 'int' objects
*
One of the operands must be string, the other - integer
>>> "-" * 10
'----------'
>>> "1" * 10
'1111111111'
>>> ">hello<" * 3
'>hello<>hello<>hello<'
>>> "a" * "3"
Traceback ...
TypeError: can't multiply sequence by non-int of type 'str'
# string methods:
>>> "iva".capitalize()
'Iva'
What a method is will be discussed further!
For now, you can try to look at the docs: String Methods @docs.python.org
x = 99
y = 3,141516
first_name = "ada"
sur_name = "byron"
num_list = [1,2,3]
print("x = ", x,)
print("y = ", y)
print("first_name = ", first_name)
print("sur_name = ", sur_name)
print("num_list = ", num_list)
_myvar #OK,
my_var #OK
my-var # error
my var # error
myvar_1 # OK
1myvar # error
from PEP 8 -- Style Guide for Python Code:
#
#
(hash tag) is a comment and is ignored py Python interpreter
# this is a just a comment: no print("whatever") will happens
print("this will be printed, of course")
### a more semantic example for comment:
# check if a triangle with sides (3,4,5) is a Pythagorean:
print(3**2 + 4**2 == 5**2)
48.0
"""
acad_to_astro_hours.py
Solution without using variables to store values
"""
print((64*40 + 64/4*20)/60)
"""
acad_to_astro_hours_with_vars.py
An example of too many variables, with too long names.
"""
# given:
minutes_in_astro_hour = 60
minutes_in_acad_hour = 40
acad_course_hours = 64
acad_hours_in_a_partida = 4
# program logic:
acad_hours_partidas = acad_course_hours/acad_hours_in_a_partida
total_minutes_brakes = acad_hours_partidas * 20
minutes_in_a_course = acad_course_hours * minutes_in_acad_hour + total_minutes_brakes
course_in_astro_hours = minutes_in_a_course / 60
print(course_in_astro_hours)
BMI = W / (H*H)
>>> round(2.1457, 2)
2.15
>>> round(1.4234,2)
1.42
>>> round(1.4284,2)
1.43
What is your BMI category?
BMI | Category |
---|---|
<= 18.5 | Underweight |
18.5–24.9 | Normal |
25–29.9 | Overweight |
>= 30 | Obesity |
"""
Solution without using variables to store values,
and given that W = 53, H = 1.67:
"""
print(round(53/(1.67*1.67), 2))
# 19.0
These slides are based on
customised version of
framework