Python Important Shortcut Notes For Beginners - Python Last Minute Notes
Python Last Minute Notes¶
Data Types¶
The data type of a value (or variable in some contexts) is an attribute that tells what kind of data that value can have. Data types define particular characteristics of data used in software programs and inform the compilers about predefined attributes required by specific variables or associated data objects.
Arithmetic Operations¶
1+1
2
2-1
1
2*2
4
2/2
1.0
10%6
4
2//2
1
3**3
27
(3-2)*(3-2)
1
Variable Declaration¶
A Python variable is a reserved memory location to store values.
a = 1
b = 2
c = a+b
c
3
abc = 1
ABC = 2
aBc = 3
abc
1
Strings¶
'abc'
'abc'
"abc"
'abc'
"I'm okay!"
"I'm okay!"
Printing¶
a = "Hello"
a
'Hello'
print(a)
Hello
name = "John Doe"
age = 40
print("The name of {a} is {b}".format(a=name, b=age))
The name of John Doe is 40
String Splicing¶
a = "abcdefghijklmnop"
a[0]
'a'
a[3]
'd'
a[:]
'abcdefghijklmnop'
Lists¶
A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [].
Lists are used to store multiple items in a single variable.
a=[1,2,3,4,5]
a
[1, 2, 3, 4, 5]
b = ['a','b','c','d','e','f']
b
['a', 'b', 'c', 'd', 'e', 'f']
c = ['a',1,'b',2]
c
['a', 1, 'b', 2]
a.append(6)
a
[1, 2, 3, 4, 5, 6]
b.append("g")
b
['a', 'b', 'c', 'd', 'e', 'f', 'g']
a[0]
1
a[1:4]
[2, 3, 4]
a[:4]
[1, 2, 3, 4]
a[2:]
[3, 4, 5, 6]
b
['a', 'b', 'c', 'd', 'e', 'f', 'g']
b[0]="NEW"
b
['NEW', 'b', 'c', 'd', 'e', 'f', 'g']
## NESTED LISTS
d
d
[1, 2, 3, 4, 5, [6, 7, 8, 9]]
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.
You can define a dictionary by enclosing a comma-separated list of key-value pairs in curly braces ({}). A colon (:) separates each key from its associated value
Dictionaries does not allow duplicates
a = [1,2,3,4,5]
b = {"student1":"John Doe", "student2":"Tom", "student3":"Jack"}
b["student1"]
'John Doe'
b["student2"]
'Tom'
c = {"student1":[1,2,3,4,5]}
c["student1"][2]
3
c = {"student1":{"age":20}}
c["student1"]["age"]
20
Tuples¶
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.
A tuple is a collection which is ordered and unchangeable (IMMUTABLE)
Tuples are written with round brackets.
a = (1,2,3,4,5)
b = [1,2,3,4,5]
b.append(6)
b
[1, 2, 3, 4, 5, 6]
a.append(6)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-42-a09ce8848fdf> in <module> ----> 1 a.append(6) AttributeError: 'tuple' object has no attribute 'append'
Sets¶
a = {1,2,3,4,5}
a
{1, 2, 3, 4, 5}
a = {1,1,1,1,1,1,1,1,1}
a
{1}
b = {1,1,1,2,2,2,3,3,3}
b
{1, 2, 3}
Operators¶
1 < 2
True
2 > 3
False
2 <= 3
True
3 >= 4
False
2==3
False
3!=3
False
## Logical Operators
(1>2) and (2>1)
False
(1>2) or (2>1)
True
If Else Statements¶
if 1>2:
print("Task Successful")
elif 3>4:
print("Task 2 Successful")
else:
print("Task failed")
Task failed
For Loops¶
a = [1,2,3,4,5]
for temp in a:
print("The element is : {}".format(temp))
The element is : 1 The element is : 2 The element is : 3 The element is : 4 The element is : 5
While Loops¶
a = 1
while a<5:
print("HELLO")
a = a+1
HELLO HELLO HELLO HELLO
Functions¶
A function can be defined as the organized block of reusable code, which can be called whenever required.
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.
"hello".upper()
'HELLO'
"HELLO".lower()
'hello'
a = [1,2,3,4,5]
len(a)
5
sum(a)
15
Defining Your Own Function¶
def greet(name):
print("Good Morning "+name)
greet("Jack")
Good Morning Jack
def square(a):
print(a**2)
square(100)
10000
def square(a):
return a**2
b = square(100)
0 Comments