BASICS OF PYTHON:
Eg1: To print Helloworld:
print("Hello World")
Eg2: To print the sum of 2 numbers
a=10
b=20
print("The Sum:",(a+b))
3.printing all keywords at once using "kwlist()"
import keyword
print("The list of keywords is : ")
print(keyword.kwlist)
4.Input From User
val = input("Enter your value: ")
print(val)
5.Input multiple values at a time.
a=list(map(int,input().split()))
print(a)
6.comments in python
single line: print("Hello, World!") #This is a comment
multi line: """
This is a comment
written in
than just one line
"""
print("Hello, World!")
7.variables
x = 5
y = "John"
print(x)
print(y)
8.casting:
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
9.Type of variable:
x = 5
y = "John"
print(type(x))
print(type(y))
10.IF_ELSE:
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
11.FOR LOOP:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
a=[1,2,3,4,5]
for i in range(len(a)):
print(i)
12:WHILE LOOP:
i = 1
while i < 6:
print(i)
i += 1
13: Break Statement
-> the break statement we can stop the loop even if the while condition is true
i = 1
while i < 6:
if (i == 3):
break
print(i) #prints 1 and 2.
i += 1
14: Continue Statement
->the continue statement we can stop the current iteration, and continue with the next:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i) #skip the value 3.
15. Functions
->No parameters in function:
def my_function():
print("Hello from a function")
my_function()
->passing parameters in function:
def my_function(food):
for x in food:
print(x) #prints apple,banana and cheery.
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
16.Lambda function
->Single variable
x = lambda a : a + 10
print(x(5)) #prints 15
->Multiple variables:
x = lambda a, b, c : a + b + c
print(x(5, 6, 2)) #prints 13.
No comments:
Post a Comment