Skip to content

Python Basic

1. Python Output

Python Output

Say hello to world
print("Hello Data Points") 
Your Name
print("Rudra")
Number
print(7)
Decimal
print(3.3)
Boolean
print(True)
Bunch of Text
print("India", 7, 4.5, False, True, "z")

Print Parameter

`sep`
print("India", 7, 4.5, False, True, "z", sep="|")
`sep` Output
India|7|4.5|False|True|z

Example 1

end
1
2
3
# Defalut is '\n'
print('hello')
print('world')
next line
print('Hello \nWorld')
Output
1
2
3
# Both output is the same
hello
world

Example 2

`end`
print('hello', end='---')
print('world')
--- output
hello-world


2. Data Types

Data Type

Integer
print(8)
print(1e309)
Output
8
inf

Decimal/Float
print(8.55)
print(1.7e309)
Output
8.55
inf

Boolean
print(True)
print(False) 
Output
True
False

String
print('Hello World')
Output
Hello World

print(5+6j)
Output
(5+6j)

List
print([1,2,3,4,5])
Output
[1,2,3,4,5]

Tuple
print( (1, 2, 3, 4, 5) )
Output
(1, 2, 3, 4, 5)

Sets
print({1,2,3,4,5})
Output
{1,2,3,4,5}

Dictionary
print({'name':'Rudra', 'gender':'Male', 'weight':65})
Output
{'name':'Rudra', 'gender':'Male', 'weight':65}

Type
type([12, 3, 4])
Output
list


3. Variables

Info

Variables
1
2
3
4
5
6
7
name = "Rudra"
print(name)

a = 5
b = 6

print(a + b)
Output
Rudra
11

Dynamic Typing
1
2
3
a = 5
a = 10
a = 15
Output
15

Dynamic
1
2
3
4
5
a = 5
b = 2
c = 5

print(a + b + c)
Output
15
5 5 5


4. Comments

Comments

Single Line Comment
# This is single line comment
Output

Multiline Comment
'''
This
is 
multi line
comment
'''

"""
This
is 
multiline
comment
"""
Output


5. Keywords & Identifiers

Keywords

Keyword
import keyword
print(keyword.kwlist)
Output
1
2
3
4
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global',
'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return',
'try', 'while', 'with', 'yield']

Identifiers

  • You can't start with a digit
  • You can't use underscore
  • You can't use special Symbols

6. User Input

Input

# Take input from user and store them in a variable
fnum = input('enter first number :')
snum = input('enter second number :')

# add tow variables
result = int(fnum) + int(snum)

# print the result
print(result)

print(type(fnum))
# it doesn't change the original data type
Output
9
<class 'str'> 


7. Type Conversion

  • Implicit Type Conversion: This is done automatically by Python.

  • Explicit Type Conversion: This is done by the programmer using predefined functions

Conversion Type

Implicit
1
2
3
4
# implicit
print(5+5.6)
print(type(5), type(5.6))
type(5+5.6)
Output
1
2
3
10.6
<class 'int'> <class 'float'>
float

Error
print(4 + '4') # this show an error
Output
1
2
3
----> 1 print(4 + '4') 

TypeError: unsupported operand type(s) for +: 'int' and 'str' 
1
2
3
4
5
6
7
8
9
# Explicit
# str -> int
print(type(int('4')))

# float -> int
print(int(4.5))

# int -> float
print(float(4))

8. Literals

Literals

# integer
a = 0b1010 # Binary Literals
b = 100 # Decimal Literal
c = 0o310 # Octal Literal
d = 0x12c # Hexadecimal Literal

#float Literal
float_1 = 10.5
float_2 = 1.5e2 #1.5 * 1o^2  --> Very big
float_3 = 1.5e-3 #1.5 * 1o^-3 -->Very small

# complex Literal
x = 3.14j

print(a, b, c, d)
print(float_1, float_2, float_3)
print(x, x.imag, x.real)
Output
1
2
3
10 100 200 300
10.5 150.0 0.0015
3.14j 3.14 0.0

string = 'This is Python' # single quotation
strings = "This is Python" # double quotation

char = 'Python'
multieple_line = '''Hello Data Points'''
raw_str = r"raw \n string"

unicode = u"\U0001F642\u2764\U0001F606"

print(string)
print(strings)
print(char)
print(multieple_line)
print(raw_str)
print(unicode)
Output
1
2
3
4
5
6
This is Python
This is Python
Python
Hello Data Points
raw \n string
🙂❤😆

# Unicode characters

smile = "\U0001F642"  # 🙂
laugh = "\U0001F602"  # 😂
laughing = "\U0001F606"  # 😆
clapping = "\U0001F44F"  # 👏
dancing = "\U0001F483"  # 💃
heart = "\u2764"  # ❤️

print("Smile:", smile)
print("Laugh:", laugh)
print("Laughing:", laughing)
print("Clapping:", clapping)
print("Dancing:", dancing)
print("Heart:", heart)
Output
1
2
3
4
5
6
Smile: 🙂
Laugh: 😂
Laughing: 😆
Clapping: 👏
Dancing: 💃
Heart: 

1
2
3
4
5
6
7
# internal python think True = 1 & False = 0

a = True + 4
b = False + 10

print("a:", a)
print("b:", b)
Output
a: 5
b: 10

R = None
print(R)
Output
None