Hi 🙏. My name is Shravan. This article 👦🏾 will cover the basic Python topics that are important. These topics will be important for beginners as well as a revision for interviews too. If you find this article useful please like and comment about it.
1. Print(“Hello, World”)
In the above line, we just printed directly the string. Alternatively, we can store the string as a variable and use the variable to print it.
Note: Print is a function
Like a=Hello, World
print("Hello, World")
#output
Hello, World
When we use print(a). Output is Hello, World
a="Hello, World"
print(a)
#output
Hello, World
2. If — else Statements
These are conditional statements and are used for making decisions. Condition if is executed if condition satisfies. Otherwise, else is executed.
a=10
b=20
if a>b:
print("a is greater")
else:
print("b is greater")
#output
b is greater
3. Arithmetic Operations
The operations like addition(+), subtraction(-), multiplication(*) and division(/)can be performed on the integers.
4. For loop and While loop
- For loop
For loop can be used for iterating the list:[ ], set: ( ), tuple: ( ), dictionary: {key: value} and string: ' ' too.
arr=['redmi','realme','oneplus']
for i in arr:
print(i)
#Outpput
redmi
realme
oneplus
While loop
It is executed until the condition is true
i=0 while i<2: print(i) i+=1 #Output 0 1
In loops there are continue and break statements.
5. Split and join in strings
#Split in strings
a="hi everyone"
print(a.split(''))
#output
['hi','everyone']
#Join in Strings
a="hi everyone"
a=a.split('')
print("-".join(a))
#output
hi-everyone
6. We know that lists are mutable and tuples are immutable. So for changing values in a tuple. We need to convert them into a list and next complete the operations and join it.
Another way is to slice the string and join them back by completing the operations.
7. List comprehensions are like shorter syntax means giving the code in a single line instead of many lines.
array=[]
for i in range(4):
array.append(i)
print(array)
#Output
[0,1,2,3]
We can convert the above three lines to single lines using list comprehension
print([i for in in range(4)])
#Output
[0,1,2,3]
8. Map function
list(map(int, input().split())
print(list(map(int, input().split()))
#output
[]
only map prints only map object
print(map(int, input().split()))
#output
<map object at 0x1002b2400>
9. Precision Handling
There are still other methods too but I discussed only one method.
b=3.6666
print("{0:.2f}".format(b))
#output
3.66
10. Access Dictionary
dict={key: value}
#To access value
#1
print(dict.get(key))
#2
print(dict[key])
# Access key
print(dict.keys())
Overall In this article, we covered basic Python topics and In the next article, more topics are covered. If you find my article informative like, share and comment down your thoughts for encouragement.