Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32For what it's worth, the final one is powers - you can't do superscript on the command line to tell Python you want 43 i.e. 4 * 4 * 4 so Python uses double asterisk for powers/exponentiation. You can also use this to find the square root ( ** 0.5) and the inverse (** -1)
Type "copyright", "credits" or "license()" for more information.
>>> 5 + 3
8
>>> 4 * 2
8
>>> 4 ** 3
64
>>>
>>> 49 ** 0.5Note here it switches from integers (whole numbers) to floating point numbers (numbers with decimal points). Python treats these two types slightly differently and you need to be careful.
7.0
>>> 5 ** -1
0.2
>>>
When two integers are divided, Python always produces a floating point answer:
>>> 12 / 4I can also use brackets to ensure multi-part calculations are done in the right order:
3.0
>>> 12 / 7
1.7142857142857142
>>> (4 * 5) + 3So far it's nice but nothing special - this could all be done on a pocket calculator. But when we combine it with a for loop, things get a bit more interesting as we can now repeat the calculation over and over. For example:
23
>>> 4 * (5 + 3)
32
>>>
gives us the output#!/usr/bin/python3print ("Savings Calculator")
deposit = 100.00
interest = 0.030
year = 2017
savingyears = 20print(year, deposit)
for i in range(savingyears):
deposit = deposit + (deposit * interest)
year += 1
print (year, deposit)
>>>Actually, although this is a short and simple script, it has brought up a number of points, some of which have been covered in earlier posts.
RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test02b_interest.py
Savings Calculator
2017 100.0
2018 103.0
2019 106.09
2020 109.2727
2021 112.550881
2022 115.92740743
2023 119.4052296529
2024 122.987386542487
2025 126.67700813876161
2026 130.47731838292447
2027 134.3916379344122
2028 138.42338707244454
2029 142.57608868461787
2030 146.8533713451564
2031 151.2589724855111
2032 155.79674166007644
2033 160.47064390987873
2034 165.2847632271751
2035 170.24330612399035
2036 175.35060530771005
2037 180.61112346694136
>>>
- Variables and assignment - Before the for loop starts, the variables are named appropriately and initialised (given initial values).
- range() can use a variable if the variable represent a number, giving a range from 0 to the variable -1. Here savingyears = 20, so it is effectively range(20)
- year +=1 is modifying the variable every time it goes through the loop - it increases year by 1, and is shorthand for year = year +1
- Debugging. Even though this is very simple I still made mistakes. I find that programming has an element of trial and error. This will be covered soon.
- Usability and Presentation. Would a non-programmer understand what's going on? Looking at the script now, I would probably include column headers at the start so that someone looking at the output knows what the numbers mean.
Python has a lot of mathematical functions that can be used.
Modulus function, symbol %, is used for finding the remainder of division, useful for finding if a number is neatly divisible by another number (no fractions or remainder).
>>> 60 % 12
0
>>> 55 % 12
7
int() will convert a floating point number, or if it can, a string containing numbers, into an integer (whole number). If given a floating point, it will round down, even if the decimal fraction is greater than .5.
>>> int(3.2)
3
>>> int(3.8)3
To get to the nearest integer, upwards or downwards, you can use round()
>>> round(3.8)float() will convert an integer, or if it can, a string containing numbers, into a floating point number (a number with a decimal point).
4
>>> round(3.2)3
>>> foo = "34.325"So both variables are strings here, but the first attempt demonstrates that int() has its limits, particularly with strings of floating point numbers.
>>> int(foo)
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
int(foo)
ValueError: invalid literal for int() with base 10: '34.325'
>>> float(foo)
34.325
>>> bur = "35"
>>> int(bur)
35
>>> float(bur)
35.0
>>>
No comments:
Post a Comment