Python Basics
Python Basics
In other programming languages like C, C++, and Java, you will need to declare the type of
variables but in Python you don’t need to do that. Just type in the variable and when values
will be given to it, then it will automatically know whether the value given would be an int,
float, or char or even a String.
Also, Python allows assigning a single value to several variables simultaneously with
“=” operators.
For example:
a = b = c = 10
print(a)
print(b)
print(c)
Lists are just like arrays, declared in other languages which is an ordered collection
of data. It is very flexible as the items in a list do not need to be of the same type.
Just like a list, a tuple is also an ordered collection of Python objects. The only
difference between a tuple and a list is that tuples are immutable i.e. tuples cannot
be modified after it is created. It is represented by a tuple class.
Expressions in Python
An expression is a combination of operators and operands that is interpreted to produce
some other value. In any programming language, an expression is evaluated as per the
precedence of its operators. So that if there is more than one operator in an expression,
their precedence decides which operation will be performed first. We have many different
types of expressions in Python.
1. Constant Expressions: These are the expressions that have constant values only.
Example:
x = 15 + 1.3 Output
print(x) 16.3
3. Integral Expressions: These are the kind of expressions that produce only integer
results after all computations and type conversions.
Example: Output
a = 13 25
b = 12.0
c = a + int(b)
print(c)
4. Floating Expressions: These are the kind of expressions which produce floating point
numbers as result after all computations and type conversions.
Example: Output
a = 13 2.6
b = 5
c = a / b
print(c)
Example: Output
a = 21 True
b = 13
c = 40
d = 37
p = (a + b) >= (c - d)
print(p)
It’s a quite simple process to get the result of an expression if there is only one operator in
an expression. But if there is more than one operator in an expression, it may give different
results on basis of the order of operators executed. To sort out these confusions, the
operator precedence is defined. Operator Precedence simply defines the priority of
operators that which operator is to be executed first. Here we see the operator precedence
in Python, where the operator higher in the list has more precedence or priority:
1 Parenthesis ()[]{}
2 Exponentiation **
8 Bitwise XOR ^
9 Bitwise OR |
11 Equality Operators == !=
12 Assignment Operators = += -= /= *=
Example: Output:
a = 10 + 3 * 4 22
print(a)
b = (10 + 3) * 4 52
print(b)
c = 10 + (3 * 4) 22
print(c)
Python Operators
In Python programming, Operators in general are used to perform operations on values
and variables. These are standard symbols used for the purpose of logical and arithmetic
operations. In this article, we will look into different types of Python operators.
2. Comparison Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
Subtraction: subtracts
– x–y
two operands
Multiplication:
* x*y
multiplies two operands
1. P – Parentheses
2. E – Exponentiation
4. D – Division
6. S – Subtraction
Example: The code performs basic arithmetic operations with the values of ‘a’ and ‘b’. It
adds (‘+’), subtracts (‘-‘), multiplies (‘*’), computes the remainder (‘%’), and raises a to the
power of ‘b (**)’. The results of these operations are printed.
a=9
b=4
add = a + b
sub = a - b
mul = a * b
mod = a % b
p = a ** b
print(add)
print(sub)
print(mul)
print(mod)
print(p)
2) Comparison Operators in Python
In Python Comparison of Relational operators compares the values. It either returns True or
False according to the condition.
| Bitwise OR x|y
~ Bitwise NOT ~x
Performs Bitwise OR on
|= operands and assign a|=b a=a|b
value to left operand
Example: The code starts with ‘a’ and ‘b’ both having the value 10. It then performs a
series of operations: addition, subtraction, multiplication, and a left shift operation on ‘b’.
The results of each operation are printed, showing the impact of these operations on the
value of ‘b’.
Input and Output in Python
Sometimes a developer might want to take user input at some point in the program. To do
this Python provides an input() function.
Syntax:
input('prompt')
where prompt is an optional string that is displayed on the string at the time of taking
input.
Note: Python takes all the input as a string input by default. To convert it to any other data
type we have to convert the input explicitly.
Take the elements of the List/Set one by one and use the append() method in the case of
List, and add() method in the case of a Set, to add the elements to the List / Set.
List = list()
Set = set()
l = int(input("Enter the size of the List : "))
s = int(input("Enter the size of the Set : "))
print("Enter the List elements : ")
for i in range(0, l):
List.append(int(input()))
print("Enter the Set elements : ")
for i in range(0, s):
Set.add(int(input()))
print(List)
print(Set)
Using map() and list() / set() Methods
We know that tuples are immutable, there are no methods available to add elements to
tuples. To add a new element to a tuple, first type cast the tuple to the list, later append
the element to the list, and again type cast list back to a tuple.
T = (2, 3, 4, 5, 6)
print("Tuple before adding new element")
print(T)
L = list(T)
L.append(int(input("Enter the new element : ")))
T = tuple(L)
print("Tuple After adding the new element")
print(T)
Python Functions
Python Functions is a block of statements that return the specific task. The idea is to put
some commonly or repeatedly done tasks together and make a function so that instead of
writing the same code again and again for different inputs, we can do the function calls to
reuse code contained in it over and over again.
def fun():
print("Welcome to GFG")
After creating a function in Python we can call it by using the name of the function
followed by parenthesis containing parameters of that particular function.
def fun():
print("Welcome to GFG")
fun()
Python Function with Parameters
print(num3)
add(2,3)