Summary of Python Commands PyProg 1819a
Summary of Python Commands PyProg 1819a
Basic commands
Command Example Explanation
for for element in iterable: Run over all the elements of a given iterable
statement (such as list, string)
while while condition: Used to repeat the same instructions until the
statement stop criterion is met
continue for i in [0,1,2,3]: Continues with the next iteration of the loop
if i % 2 == 0 :
continue
print i
(it will print 1,3)
+ x+y Adds x to y
* x*y Multiplies x by y
** x ** y X to the power y
/ x/y Divides x by y
+ x+y Adds x to y
Comparison operators
Command Example Explanation
Logical operators
Command Example Explanation
Strings
Command Example Explanation
* s*2 Repeat
format "Check {}self before {} Replaces the curly brackets in the original
wreck string with the specified parameters.
{}self.".format("your",
"you", "your")
>>>Check yourself
before you wreck
yourself.
List [i**2 for i in range(10)] Creates a new list based on the specified
comprehensions rule. For ever i in the range add i**2 to the
new list.
Returns [0,1,4,9,16,...,81]
Tuples
print min(t1)
Sets
get D.get(k, [d]) Returns the value mapped for key k in D, if k
is not in D, returns d (default: None).
pop D.pop(k, [d]) Removes the specified key k from D and
return its value. If k is not a key in D, returns
d. Raises a KeyError if k is not in D and d is
not specified.
Basic IO (Input/Output)
Command Example Explanation
raw_input str = raw_input(‘Enter Reads a line from standard input and return it
your input : ’) as a string
File IO (Input/Output)
Command Example Explanation
readline s=f.readline() Read the next line from the file f, returns a
string
For line in f: for line in f: Reads line by line from file f, line holds the
print line current line
Functions
Command Example Explanation
:( זריקת חריגה באופן יזום )כשקוד שאנחנו כותבים נתקל במצב שלא מאפשר להמשיך.ב
Explanation Example Command
Counting Sort
באלגוריתם זה בונים היסטוגרמה -מילון המכיל מפתחות שהם הערכים השונים ברשימה ,וערך כל מפתח הוא מספר
המופעים של מפתח זה ברשימה .בעזרת ההיסטוגרם האלגוריתם בונה רשימה ממוינת.
חשוב לציין שהפונקציה מקבלת את הערך max_valהמקיים שכל איברי הרשימה קטנים או שווים לו ,ושאיברי הרשימה לא
שליליים.
Reduction Functions
Command Example Explanation
np.sum a=np.array([[1,2,3],[4,5,6]])
s=np.sum(a) Returns the sum of all elements in a
s=a.sum() Same (the method version)
np.min
Arg Functions
Command Example Explanation