This is not a complete python 3 tutorial. This just quick note to remember the python 3 syntax.
Python Data Type:
Name | Type | Description |
---|---|---|
Integer | int | Numbers such as 100,200,1337 |
Strings | str | More than one characters such as “Cyber” |
Booleans | bool | Logical Value: True or False |
Floating Point | float | Numbers with decimal point such as: 1.1 |
Lists | list | Sequence of objects: [“Cyber”,10] |
Dictionaries | dict | Value pairs: {“key1″:”Cyber”,”key2″:”Red”} |
Sets | set | Collection of unique objects: {“test”,”test1″} |
Tuples | tup | Ordered secquence of objects: (1,”Cyber”) |
String
Here is the example of String indexing and slicing:
>>> string= "onetwothree"
>>> string[1]
'n'
>>> string[1:]
'netwothree'
>>> string[2:]
'etwothree'
>>> string[2:5]
'etw'
>>> string[2:5:7] # Start:Stop:Step Size
'e'
>>>
Format String
>>> print("This is {}".format(string))
This is one two three
>>> print("This is {1}".format(string))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: Replacement index 1 out of range for positional args tuple
>>> print("This is {0}".format(string))
This is one two three
>>> print("This is {0}".format(string,string))
This is one two three
>>> print("This is {0} {1}".format(string,string))
This is one two three one two three
>>> print("This is {0} {1}".format(a='One',b='Two'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: Replacement index 0 out of range for positional args tuple
>>> print("This is {a} {b}".format(a='One',b='Two')) #Assign Variable for the Format
This is One Two
>>> string.split() #Convert a string to List
['one', 'two', 'three']
>>> print(f'hmmm {test}')
hmmm testing
>>>
>>> print(f"Here it is %s"%(string))
Here it is TEST
>>> print(f"Here it is %s" %(string))
Here it is TEST
>>> print(f"Here it is {string}")
Here it is TEST
>>>
Variables
Assign data types a variable to reference later. Variable name cant contain special charcters except _
var1 = "String" #String
var2 = 1337 # Integer
var3 = True # Boolean
var4 = 1337.1 #Float
var5 = ["Cyber","World", 1337]
var6 = {"key1":"value1","key2":"value2"} #Dictionaries
var7 = {"Test1","Test2"} #Sets
var8 = (1,"Cyber") #Tuples
Python Operators
Operator | Description | Exampl |
---|---|---|
== | Check If two operands are equal | (1==2) is not true |
!= | Check two operands are not equal | (1 != 2) is true |
> | If the value of left operand is greater than right operand | 1>2 False |
< | if the left operand’s value is less than right operand value | 1<2 True |
>= | If the value of left operand is greater than right operand or equal | 1>=2 False |
<= | if the left operand’s value is less than right operand value or equal | 1<=2 True |
and | Logical and check if both is true | (1==1) and (3=3) True |
or | Logical or check if any of operands is true | (1==1) or (3==4) True |
not | Logical not check if the given operand is true | not (1==1) False |
Conditional
if (1==1) or (2=1):
print("Condition is True")
elif (2==2) and (3==3):
print("Condition is True")
else:
print("Nothing match?")
Lists
-
Lists are ordered Sequence
-
It supports indexing and slicing
lists = [“Cyber”,1337]
lists = [“Cyber”,1337] lists [‘Cyber’, 1337] lists[0] ‘Cyber’ lists[:1] [‘Cyber’] lists[1:] [1337]
li = [1,2,3] lists+li [‘Cyber’, 1337, 1, 2, 3]
lists[0] = ‘Cyber World’ lists [‘Cyber World’, 1337]
lists[0].upper() ‘CYBER WORLD’
Nested List:
>>> lists
['Cyber World', 1337]
>>> listing = [1,2]
>>> lists.append(listing)
>>> lists
['Cyber World', 1337, [1, 2]]
>>> lists[2]
[1, 2]
>>> lists[2][0]
1
Useful Available method:
list.append(x): Add an item to the end
list.insert(i,x): insert an item to given postion.
list.remove(x): Remove the first item from the list whose value is equal to x
list.clear(): Remove all items from the list.
list.reverse(): Reverse the elements of the list in place.
Dictionaries
It is key and value pair. Use dictionary when need to retrive value by key name.
dicto = {'key':'val1','key1':'val2'}
print(dicto['key1'])
Tuples
Tuples is immutable, mean, we can’t change or modify Tuples
>>> tupl = (1,2,3)
>>> tupl[0]
1
Loop
For Loop
Looping through list:
list1 = [1,2,3]
for n in list1:
if n==2:
print("Loop rach the"+str(n))
else:
print('Wtf')
listing = 0
for n in list1:
listing += listing+n
print(listing)
Looping through a string:
strange = "This is strange"
for s in strange:
print(s)
Looping through the Dictionary:
d = {'key1':1,'key2':2}
for key,value in d.items():
print(key)
print(value)
While Loop
Loop until y=False
>>> y = True
>>> while y:
... print(x)
... if x == 10:
... y=False #Or break statement
... x += 1
...
0
1
2
3
4
5
6
7
8
9
10
More to update!!!