Variables

A variable is created the moment you first assign a value to it.

x = 5
y = "Ananya"
print(x)
print(y)
5
Ananya

Data Types

Variables can store data of different types, and different types can do different things.

x = 5
print(type(x))
<class 'int'>

Lists

Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.

thislist = ["apple", "banana", "cherry"]
print(thislist)
['apple', 'banana', 'cherry']

Dictionaries

Dictionaries are Python's implementation of a data structure that is more generally known as an associative array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value

Algorithms

Python algorithms are a set of instructions that are executed to get the solution to a given problem. Since algorithms are not language-specific, they can be implemented in several programming languages. No standard rules guide the writing of algorithms

myfamily = {
  "child1" : {
    "name" : "Emil",
    "year" : 2004
  },
  "child2" : {
    "name" : "Tobias",
    "year" : 2007
  },
  "child3" : {
    "name" : "Linus",
    "year" : 2011
  }
}

Sequence

In Python, sequence is the generic term for an ordered set. There are several types of sequences in Python, the following three are the most important. Lists are the most versatile sequence type.

Selection

In Python, the selection statements are also known as decision making statements or branching statements. The selection statements are used to select a part of the program to be executed based on a condition.

Iteration

Repetitive execution of the same block of code over and over is referred to as iteration. There are two types of iteration: Definite iteration, in which the number of repetitions is specified explicitly in advance.

Expressions

In programming language terminology, an “expression” is a combination of values and functions that are combined and interpreted by the compiler to create a new value, as opposed to a “statement” which is just a standalone unit of execution and doesn't return anything.

Comparison Operators

A comparison operator in python, also called python relational operator, compares the values of two operands and returns True or False based on whether the condition is met

Booleans Expressions and Selection

A boolean expression (or logical expression) evaluates to one of two states true or false. Python provides the boolean type that can be either set to False or True. Many functions and operations returns boolean objects. The not keyword can also be used to inverse a boolean type.

Charcters

Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.

Strings

Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.

a = """HELLO HELLO
HELLOHELLO
HELLOHELLOHELLO
"""
print(a)
HELLO HELLO
HELLOHELLO
HELLOHELLOHELLO

a = "Hello"
print(a)
Hello

Length Concatenation

Concatenation means joining strings together end-to-end to create a new string. To concatenate strings, we use the + operator. Keep in mind that when we work with numbers, + will be an operator for addition, but when used with strings it is a joining operator.

Boolean Values

In programming you often need to know if an expression is True or False. You can evaluate any expression in Python, and get one of two answers, True or False.

print(10 > 9)
print(10 == 9)
print(10 < 9)
True
False
False

Python Operators

Operators are used to perform operations on variables and values. In the example below, we use the + operator to add together two values:

print(10 + 5)
15

Tuple

Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.

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

Python Conditions and If statements

Python supports the usual logical conditions from mathematics:

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • These conditions can be used in several ways, most commonly in "if statements" and loops.

An "if statement" is written by using the if keyword.

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

Python Loops

Python has two primitive loop commands: while loops for loops

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

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.

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

Python ascii() Function

The ascii() function returns a readable version of any object (Strings, Tuples, Lists, etc). The ascii() function will replace any non-ascii characters with escape characters: å will be replaced with \xe5