(Feb-22-2018, 10:12 PM)Larz60+ Wrote: or can it just be written and the IDE or Python understand what you want and does it?Yes if don't need stuff from standard library or 3-party module just write code and Python will understand it.
secret_number = 34 tries, guess = 0, 0 while guess != secret_number: guess = int(input("Take a guess: ")) if guess > secret_number: print("Lower...") elif guess < secret_number: print("Higher...") tries += 1 print('You guessed it! The number was {} in {} tries'.format(guess, tries))
If file called
guess.py
run with python guess.py
If as a example want use
math
from standard library,it's kind of similar to C++ example Python:
from math import sqrt print(sqrt(4))C++:
#include<iostream> #include<cmath> int main(){ cout<<sqrt(4); return 0; }
The concept of
import
is kind of simple if look at a module.# foo.py def add(a, b): """This program adds two numbers and return the result.""" result = a + b return resultSo now i want to use add function in an other script(file).
# bar.py from foo import add a = 11 b = 3 print(add(a, b)) # Use add from foo.py
Output:14
Package more stuff than just a couple of files and sharing stuff with wheel or PyPi,is a more complex,i have long tutorial about here