Professional Documents
Culture Documents
string
- “” // for space
- A,B = 3,4 // multiple in
one line
- X = Y = Z = “Red” // One value to
Multiple Var
- global A (for fix value) // Global
Value
- myint = 7 // Int value
Print(myint)
- myfloat = 7
Myfloat = float(7) // Float
conversion
Print(myfloat)
- mystring = ‘Hello’ // String
Mystring = “Hello” // String
- Mystring = “””Line1, // Multiple String
Lines
Line2,
Line3”””
Print(Mystring)
- Myno = 3
Mynewno = str(3)
Print(Mynewno) // Str
conversion
- Print(type(varname)) // Get
Datatype
Strings are Arrays
A = “Hello”
- Print(a[1]) // String
Position
- for x in “Hello” // Loop through a
String
Print(x)
- Print(len(a)) // String
Length
- Print(“llo” in a) // Check or Find in
string
- if “llo” in a: // Check or Find in string
using if
Print(“Yes, word is Present”)
- Print(“llo” not in a) // Check not
present
- If “llo” not in a: // Check not present
using if
Print(“Yes, word is not present”)
- Print(a[2:5]) // String Slicing 2
to 5
- Print(a[:5]) // String Slicing from Start
to 5
- Print(a[2:]) // String Slicing from 2
to End
- Print(a[-5:-2]) // From last position (not
include -2)
- Print(a.upper()) // Converts
UpperCase
- Print(a.lower()) // Converts
LowerCase
- Print(a.strip()) // Remove White
spaces
- Print(a.replace(“H”,”J”)) // Replace current with
require
- Print(a.split(“,”)) // String Split
with “ , “
- Print(a.capatalize()) // String first character to
Upper
- Print(a.count(“llo”)) // Count words or
chars
- Print(a.find(“llo”)) // Find First
location
- Age = 11 // Format datatype to
print
Txt = “my age is {}”
Print(txt.format(age))
- Print(10>9) //
Boolean
- Print(bool(“Hello”)) //
Evaluate
List (Ordered and Changeable, Allowed Duplicates)
Tuple (Ordered and Unchangeable, Allowed
Duplicates)
Set (Unordered and Unindexed, No Duplicates)
Dictionary (Ordered and Changeable, No Duplicates)
Dictionary (Key:Value) //
Dictionary
DictName={
“brand”:”Ford”,
“model”:”Mustang”,
“year”:1964
}
X = DictName[“model”] or X = DictName.get(“model”)
X = DictName.keys()
DictName[“year”] = 2018 // Dictionary change
values
DictName.update({“year”:2020}) // Dictionary update
values
DictName.pop(“model”) // Remove item
For X in DictName.values(): // Loop
Print(x)
NewDict = Dictname.copy() // Copy Dictionary
A dictionary can contain dictionaries, this is called nested
dictionaries.
If cond: Or if cond: print(“”)
Print(“”)
Elif cond: print(“”) if cond else
print(“”)
Print(“”)
Else: print(“”) if cond else print (“”) if cond
else
Print(“”)
print(“”)
I=1 I=1
While I < 6: while I < 6:
Print(i) print(i)
I=I+1 if I == 3:
Break
I = I +1
I=1 I =0
While I < 6: while I < 6:
Print(i) I=I+1
I=I+1 if I == 3:
Else: continue
Print(“”) print(i)
Listname = []
For x in Listname: For x in Listname:
Print(x) Print(x)
If x == value:
Break
Def my_function(arg1,arg2):
Print(arg1 + arg2 + “sent.”)
My_function(arg1,arg2)
Def my_function(x):
Return 5 * x
Print(my_function(3))
Try: Try: x = -1
Print(x) print(x) if x < 0:
Except: except Errorname: Raise
Print(“”) print(“”)
exception
Except: (“”)
Print(“”)