File extensions of python
python file --> .py
jupyter notebook -> .ipynb (interactive python notebook)
In [ ]:
Python Programming Components
1. literals
2. variables
3. identifiers
4. Reserved words
5. Statments and Expressions
6. Blocks and Indentation
7. Comments
In [4]: print("Hello World!")
Hello World!
1. Literals - any value that we use in programming lan is called as literals
Different types of literals
1. int - any value without a decimal point eg 10, -99, 100, 200
2. float - any value with a decimal point eg 10.0, -99.99, 100.23
3. string - anything that is written inside a '..' , "..." , """...."""
4. None - null
5. Bool - True/False
6. complex - any value that can be represented in the form of a+bj where a is real and b is imaginary
In [1]: print(10)
Out [1]: 10
In [3]: print(-100.2)
-100.2
In [5]: print(True)
Out [5]: True
In [6]: print(11+2j)
(11+2j)
In [28]: my_str="It's a cat"
print(my_str)
It's a cat
In [31]: my_str="""1. Abbas
2. Huzefa"""
print(my_str)
1. Abbas
2. Huzefa
In [34]: a=10.2
In [35]: print(type(a))
<class 'float'>
Literals and Varibales
a = 10
a -> var
= -> operator
10 -> literal/values
In [9]: a=10
print(a)
b=20.2
print(b)
b=20
print(b)
10
20.2
20
2. Varibales and 3. Indentifiers
variable is a subset of identifier
identifier - it is used in python for naming varibales, class name, function name, object name
Rules
1. it consists of only A-Z, a-z, 0-9, _
2. we cannot use a special character
3. we cannot start with a number
4. are case senstive
5. It can be of any length
6. we cannot use reserved (keywords) words
In [10]: abc123=10
print(abc123)
10
In [11]: abc*123=10
print(abc*123)
Cell In[11], line 1
abc*123=10
^
SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
In [12]: 123abc=10
print(123abc)
Cell In[12], line 1
123abc=10
^
SyntaxError: invalid decimal literal
In [15]: addp=10
Addp=20
print(addp)
print(Addp)
10
20
4. Reserved words (keywords)
words that are already taken by python for some purpose are called as reserved words
In [17]: import keyword
print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', '
In [ ]: True, False, None, and, or
In [19]: True_1=10
print(True_1)
10
5. Statments and Expressions
Expressions - any valid combination of literals, variables and operators that will give you output
Statments - unit of code that performs an action and will not give you output
In [20]: 10+20+30
Out [20]: 60
In [21]: a=10
6. Blocks and Indentation
In [24]: print('1')
print('2')
if 10<10.2:
print('3')
print('4')
if 15<10:
print("Hello from inner if!")
print('5')
print('6')
1
2
3
4
5
6
7. comments
1. it can be used to explain the python code
2. it can make code more readable
In [ ]: pi = 3.14
r = 11
a = r*r
print(a)
In [ ]: # code to find the area of sqaure
pi = 3.14
r = 11 #side of sqaure
a = r*r #area of sqaure
print(a)
In [25]: """
Hello Abbas
Hello again
"""
a=10
print(a)
10
In [ ]: