Python Basic

11 minute read

Before we dive into Pandas, Numpy and Matplotlib, let’s try remind us the basic of the python first. I wont cover all Python stuff because it took too much time. In this post, I will add some interesting stuff which might useful later. Let start with Python Operator.

Operators

Operators are used to perform operations on variables and values.

Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical operations

Operator Name Example
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x ** y
// Floor division x // y

Assignment Operators

Assignment operators are used to assign values to variables

Operator Example Same As
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x = x & 3
|= x |= 3 x = x | 3
^= x ^= 3 x = x ^ 3
»= x »= 3 x = x » 3
«= x «= 3 x = x « 3

Comparison Operators

Comparison operators are used to compare two values

Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Logical Operators

Logical operators are used to combine conditional statements

Operator Description Example
and Returns True if both statements are true x < 5 and x < 10
or Returns True if one of the statements is true x < 5 or x < 4
not Reverse the result, returns False if the result is true not(x < 5 and x < 10)

Identity and Membeship Operators

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location. It also can used as Membership operators to test if a sequence is presented in an object

Operator Description Example
is Returns True if both variables are the same object x is y
is not Returns True if both variables are not the same object x is not y

Conditional Statement

Python supports the usual logical conditions from mathematics. Mostly used in “if statements” and loops.

If

An “if statement” is written by using the if keyword. Example

a = 33
b = 200
if b > a:
  print("b is greater than a")
b is greater than a

Elif

The elif keyword is pythons way of saying “if the previous conditions were not true, then try this condition”. Example

a = 33
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
a and b are equal

Else

The else keyword catches anything which isn’t caught by the preceding conditions. Example

a = 200
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")
a is greater than b

Strings

String literals in python are surrounded by either single quotation marks, or double quotation marks.

Array of String

Using x = “Hello World” as example

Name Description Example Result
Array of Strings Pick a character from string x[0] ‘H’
String Slicing Return a range of characters by using the slice syntax by Specify the start index and the end index, separated by a colon, to return a part of the string. x[2:5] ‘llo’
Negative Indexing Use negative indexes to start the slice from the end of the string x[-5:-2] ‘orl’

String Length

To get the length of a string, use the len() function. For Example

a = "Hello, World!"
print(len(a))
13

Check String

To check if a certain phrase or character is present in a string, we can use the keywords in or not in. Example

txt = "The rain in Spain stays mainly in the plain"
x = "ain" in txt
print(x)
True

String Format

We cannot combine strings and numbers. But we can combine strings and numbers by using the format() method!. The format() method takes the passed arguments, formats them, and places them in the string where the placeholders {} are. For Example

quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))
I want 3 pieces of item 567 for 49.95 dollars.

String Methods

Method Description
capitalize() Converts the first character to upper case
casefold() Converts string into lower case
center() Returns a centered string
count() Returns the number of times a specified value occurs in a string
encode() Returns an encoded version of the string
endswith() Returns true if the string ends with the specified value
expandtabs() Sets the tab size of the string
find() Searches the string for a specified value and returns the position of where it was found
format() Formats specified values in a string
format_map() Formats specified values in a string
index() Searches the string for a specified value and returns the position of where it was found
isalnum() Returns True if all characters in the string are alphanumeric
isalpha() Returns True if all characters in the string are in the alphabet
isdecimal() Returns True if all characters in the string are decimals
isdigit() Returns True if all characters in the string are digits
isidentifier() Returns True if the string is an identifier
islower() Returns True if all characters in the string are lower case
isnumeric() Returns True if all characters in the string are numeric
isprintable() Returns True if all characters in the string are printable
isspace() Returns True if all characters in the string are whitespaces
istitle() Returns True if the string follows the rules of a title
isupper() Returns True if all characters in the string are upper case
join() Joins the elements of an iterable to the end of the string
ljust() Returns a left justified version of the string
lower() Converts a string into lower case
lstrip() Returns a left trim version of the string
maketrans() Returns a translation table to be used in translations
partition() Returns a tuple where the string is parted into three parts
replace() Returns a string where a specified value is replaced with a specified value
rfind() Searches the string for a specified value and returns the last position of where it was found
rindex() Searches the string for a specified value and returns the last position of where it was found
rjust() Returns a right justified version of the string
rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a list
rstrip() Returns a right trim version of the string
split() Splits the string at the specified separator, and returns a list
splitlines() Splits the string at line breaks and returns a list
startswith() Returns true if the string starts with the specified value
strip() Returns a trimmed version of the string
swapcase() Swaps cases, lower case becomes upper case and vice versa
title() Converts the first character of each word to upper case
translate() Returns a translated string
upper() Converts a string into upper case
zfill() Fills the string with a specified number of 0 values at the beginning

Dictionary

A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values. Example

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict)
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}

Dictionary Methods

Method Description
clear() Removes all the elements from the dictionary
copy() Returns a copy of the dictionary
fromkeys() Returns a dictionary with the specified keys and value
get() Returns the value of the specified key
items() Returns a list containing a tuple for each key value pair
keys() Returns a list containing the dictionary’s keys
pop() Removes the element with the specified key
popitem() Removes the last inserted key-value pair
setdefault() Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
update() Updates the dictionary with the specified key-value pairs
values() Returns a list of all the values in the dictionary

Sets

A set is a collection which is unordered and unindexed. In Python, sets are written with curly brackets. Example

thisset = {"apple", "banana", "cherry"}
print(thisset)
{'cherry', 'apple', 'banana'}

Set Method

Method Description
add() Adds an element to the set
clear() Removes all the elements from the set
copy() Returns a copy of the set
difference() Returns a set containing the difference between two or more sets
difference_update() Removes the items in this set that are also included in another, specified set
discard() Remove the specified item
intersection() Returns a set, that is the intersection of two other sets
intersection_update() Removes the items in this set that are not present in other, specified set(s)
isdisjoint() Returns whether two sets have a intersection or not
issubset() Returns whether another set contains this set or not
issuperset() Returns whether this set contains another set or not
pop() Removes an element from the set
remove() Removes the specified element
symmetric_difference() Returns a set with the symmetric differences of two sets
symmetric_difference_update() inserts the symmetric differences from this set and another
union() Return a set containing the union of sets
update() Update the set with the union of this set and others

Tuple

A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round brackets. Example

thistuple = ("apple", "banana", "cherry")
print(thistuple)
('apple', 'banana', 'cherry')

Tuple Methods

Method Description
count() Returns the number of times a specified value occurs in a tuple
index() Searches the tuple for a specified value and returns the position of where it was found

Iteration

For

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages. With the for loop we can execute a set of statements, once for each item in a strings, list, tuple, and set. Example

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
apple
banana
cherry

Range() Function

To loop through a set of code a specified number of times, we can use the range() function/ The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Example

for x in range(2, 30, 3):
  print(x)
2
5
8
11
14
17
20
23
26
29

While

With the while loop we can execute a set of statements as long as a condition is true. Example

i = 1
while i < 6:
  print(i)
  i += 1
1
2
3
4
5

Function

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result. Example

def my_function():
  print("Hello from a function")
  
my_function()
Hello from a function

If you using arguments in function

def my_function(a, b, c):
    return a+b+c

my_function(1,2,3)
6

Lambda Expression

A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression. The power of lambda is better shown when you use them as an anonymous function inside another function. Say you have a function definition that takes one argument, and that argument will be multiplied with an unknown number:

Syntax

lambda *arguments* : *expression*

Example

x = lambda a : a + 10
print(x(5))
15

Leave a comment