Comment
comment - single line |
# this is a single line comment |
comment - multiline comments |
""" this is multi lines comments """ |
Variables Declaration
integer |
students_count = 1000 |
decimal |
rating = 4.99 |
boolean |
is_published = False |
string - single line |
course_name = "Python" |
string - multiple lines |
course_name = """ Multiple Lines """ |
string concatenation |
full_name = first_name + " " + last_name |
formatted string |
full_name = f"{first_name} {last_name}" |
assign multiple variables |
x, y, z = "John", "Alex", "May" |
one value to multiple variables |
x = y = z = "Value" |
uppack |
animals = ["monkey", "cat", "dog"] x, y, z = animals |
boolean - capitalise the first letter
single line - can use either single or double quote
multiple lines - use triple double quote to wrap the entire strings
formatted string - use 'f' and curly braces with the double quotes wrap with the separated strings
Output Variables
text output |
print("Hello World") |
variable output |
print(x + y + x) |
Global Variable
def myfunc():
global x
x = " John"
myfunc()
print("Hello " + x)
|
Data Types
text Type: str
numeric Types: int, float, complex
sequence Types: list, tuple, range
mapping Type: dict
set Types: set, frozenset
boolean Type: bool
binary Types: bytes, bytearray, memoryview
none Type: NoneType |
Collection Types
List - like an array |
fruits = ['apple', 'orange', 'banana'] |
Dictionary - key value pair |
fruit = {'name'='apple, 'colour: 'red'} |
Tuples - order is unchangable |
fruit = ('apple', 'orange', 'banana') |
Set - set items are unchangable |
fruit = {'apple', 'orange', ;banana'} |
|
|
Built-in function
check type of variable |
x = 2.8 |
print(type(x)) |
<class 'float'> |
casting |
int(), float(), str() |
x = int(3) |
x will be 3 |
input |
username = input("Enter your name") |
user type "John" |
username = "John" |
Strings
slicing strings |
a = "Hello World" |
print(a[2:7]) |
'llo W' |
from index to the end |
|
print(a[2:]) |
'llo World' |
from start to index |
|
print(a[:8]) |
'Hello Wo' |
from negative to negative |
-2 is not included 'd' |
print(a[-7:-2]) |
'o Wor' |
String Methods
upper case |
print("Hello World".upper()) |
"HELLO WORLD" |
lower case |
print("Hello World".lower() |
"hello world" |
trim white space (leading & trailing) |
print(" No Space ".strip |
"No Space" |
replace string |
print("Hello John".replace("John","Alex")) |
"Hello Alex" |
split string |
print("Hello, World".split(",") |
return ["Hello", "World"] |
String Concatenation
use '+' operator |
|
"Hello World" |
use join() |
"".join(["Hello "," World]"
|
"Hello World" |
use format() - convert number to text |
age = 18" text = "I am {} years old" print(text.format(age))
|
"I am 18 years old" |
"+' operator cannot combine string and number
{} is a placeholder
Formatted String
# formatted string
first = "Mosh"
last = "Hamedani"
full = f"{first} {last}"
print(full)
|
Number
There are three types of number: integer, float & complex number
x = 1 # integer
x = 1.1 # float
x = 1 + 2j # a + bi # complex number
print(10 + 3) # addition
print(10 - 3) # subtraction
print(10 * 3) # multiplication
print(10 / 3) # division outcome with float
print(10 // 3) # division outcome with integer
print(10 % 3) # modulus, the remainder of a division
print(10 ** 3) # exponent, the power
|
|