You are on page 1of 14

- String = “ ” // holds empty

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))

- quantity = 3 // Format datatype to print with


location
Itemno = 456
Price = 55
Myorder = “my pay is {2} for {0} pices with item
{1}”
Print(Myorder.format(quantity,itemno,price))

- a = “we \”humans\” from world” // Escape


Char
- a= ”yoyo\n” // New
Line

- 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)

- List_name = [] //(array) (declare for hold


data)
- Print(List_name) // Print a List
- Print(List_name[1]) // Print Specific
Values
- Print(List_Name[-1]) // From Last
Position
- List_name.append(“Item”) // Appends new
Value
- Print(len(List_name)) // List
Length
- Print(type(List_name)) // List Type
- Print(List_Name[2:5]) // Item 2 to
Item 5
- Print(List_Name[:4]) // From Start to
4
- Print(List_Name[2:]) // From 2 to
End
- Print(List_Name[-4:-1]) // From last position -1 to
-4
- List_Name[1] = “NewItem” // Change Item at 1
position
- List_Name[1:3] = [“Item1”,”Item2”] // Change 1 to 3
Range
Values
- List_Name[1:2] = [“Item”,”Item”] // Change one value
with
Two Values
- List_Name[1:3] = [“Item”] // Change Two Values
with One
Value
- List_Name.insert(2, “Item”) // Insert at 2
Position
- List_Name.extend(New_List) // Merge
List
- New_List = List_Name.copy() // Copy
List
- List 3 = List1 + List2 // Merge
List
- List_Name.append(New_List) // Merge
List
- List_Name.extend(Tuple_Name) // Add Tuple
to List
- List_Name.remove(“Item”) // Remove
Item
- List_Name.pop(1) // Remove Item at 1st
Position
- List_Name.pop() // Remove Last
Item
- del List_Name[1] // Remove 1st Position
Item
- List_Name.sort() // Sort
alphabetically
- List_Name.sort(reverse = True) // Sort in
Reverse
- List.reverse() // Reverse
Order
- List_Name.sort(key = str.lowere) // Case Insensitive
sort
- del List_Name // Delete Entire
List
- List_Name.clear() // Empty a
List
- If “Item” in List_Name: // Check’s item in List
using IF
Print(“yes , it is the list”)
- for x in List_Name: // Print all Items one by one
using For
Print(x)
- for i in range(len(List_Name)): // Print all items by
referring
Print(List_Name[i]) //to their Index
Number

-i=0 // Print all Items in List Using


While Loop
While i < len(List_Name):
Print(List_Name[i])
i=i+1
List Comprehension **************************

Tuple (Store multiple items in a single variable)


- Tuple_Name = (“Item”,”Item”,”Item”)
- Tuple_Name = (“item”,) // Create tuple with one
item
comma (,)
- x = (“Item1”, “Item2”)
y = list (x) // Convert into List
y[1] = “Item3” // Add item
x = tuple(y) // Convert back into Tuple
- tuple3 = tuple1 + tuple2

Sets (No Duplicates, Duplicates Ignored)


- Set_Name = {“Item2”, “Item1”} // Sets

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

For x in Listname: for x in range(6):


If x == value: print(x)
Continue else:
Print(x) print(“”)

For x in range(6) for x in range(2,6)


Print(x) print(x)

For x in range(6): list1 = []


Print(x) list2 = []
Else: for x in list1:
Print(“”) for x in list2:
Print(x,y)

Def my_function(arg1,arg2):
Print(arg1 + arg2 + “sent.”)

My_function(arg1,arg2)

Def my_function(x):
Return 5 * x

Print(my_function(3))

Username = input(“Enter username:”)


Print(“username is ” + username)

Try: Try: x = -1
Print(x) print(x) if x < 0:
Except: except Errorname: Raise
Print(“”) print(“”)
exception
Except: (“”)
Print(“”)

You might also like