Difference between revisions of "Python"

From RHS Wiki
Jump to navigation Jump to search
Line 53: Line 53:
 
=== Numbers ===
 
=== Numbers ===
 
=== String ===
 
=== String ===
Strings can be subset,
+
String assingment:<br />
 +
MyString = 'Hello World'  OR  MyString = "Hello World"<br />
 +
Strings can be subset sliced:
 
<source lang="python">
 
<source lang="python">
 
MyString = "Hello world"
 
MyString = "Hello world"
 
MyString[0:4]</source>
 
MyString[0:4]</source>
Hel
+
Hel<br />
=== Lists ===
+
 
=== Tuple ===
+
in operator: to check if a substring is contained in a string<br />
=== Dictionary ===
+
 
 +
String library:<br />
 +
<source lang="python">
 +
str.lower()
 +
str.upper()
 +
str.capitalize() → Uppercases 1st char
 +
str.center(width[, fillchar])
 +
str.endswith(suffix[, start[, end]])
 +
str.find(substring[, start[, end]])
 +
str.lstrip([chars])
 +
str.rstrip([chars])
 +
str.strip([chars])
 +
str.replace(old, new[, count])
 +
</source>
 +
=== Collections ===
 +
==== List ====
 +
==== Tuple ====
 +
==== Dictionary ====
 
Key - Value pairs,
 
Key - Value pairs,
 +
<source lang="python">
 +
 +
</source>

Revision as of 09:07, 5 April 2015

Indentation

Python is an indented language, so the code indentation matters. A good practice is to indent with 4 spaces (if you mix spaces and tabs the code won't work.

Operators

Arithmetic

+    Addition
-    Substraction
*    Multiplication
/    Division
**   Power
%    Reminder
//   Floor division (Reminder is removed)

Comparison

==   Equal 
!=   Not equal
>    Greater
>=   Greater or equal
<    Smaller
<=   Smaller or equal
<>   Similar to !=

Assignment

=    Simple assignment
+=   Add and as
-=   Substract and  assignment
*=   Multiply and assignment
/=   Divide and assignment
%=   Modulus and assignment
**=  Exponent and assignment
//=  Floor Divisionn and assignment

Bitwise operators

They perform operations on binary terms. a= 8 → 100; b= 9 → 101; a & b → 100

$    Binary AND
|    Binary OR
^    Binary XOR
~    Binary complement
<<   Binary left shift
>>   Binary right shift

Logic

and
or
not

Membership operators

in
not in

Variables

Numbers

String

String assingment:
MyString = 'Hello World' OR MyString = "Hello World"
Strings can be subset sliced:

MyString = "Hello world"
MyString[0:4]

Hel

in operator: to check if a substring is contained in a string

String library:

str.lower()
str.upper()
str.capitalize()  Uppercases 1st char
str.center(width[, fillchar])
str.endswith(suffix[, start[, end]])
str.find(substring[, start[, end]])
str.lstrip([chars])
str.rstrip([chars])
str.strip([chars])
str.replace(old, new[, count])

Collections

List

Tuple

Dictionary

Key - Value pairs,