Python:
Variables:
Legal:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
Illegal:
2myvar = "John"
my-var = "John"
my var = "John"
x=1
print(x)
Data Types:
integer,string,float etc
x=1
print(x)
x="Hi"
print(x)
x = 5
y = "John"
print(type(x))
print(type(y))
Type casting:
x = str(5)
y = "John"
print(x + y)
Operators and Operands:
z=x+y
Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Input :
num = input ("Enter number :")
print(num)
name1 = input("Enter name : ")
print(name1)
# Printing type of input value
print ("type of number", type(num))
print ("type of name", type(name1))
input_b = int(input())
Control statements:
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Strings:
a = "Hello, World!"
print(len(a))
a = "Hello, World!"
print(a[1])
Loops:
i = 1
while i < 6:
print(i)
i += 1
for x in "banana":
print(x)
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
for x in range(6):
print(x)
range(6) is not the values of 0 to 6, but the values 0 to 5.
for x in range(2, 6):
print(x)
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)
Break:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
Continue:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
Lists:
thislist = ["apple", "banana", "cherry"]
print(thislist)
list1 = ["abc", 34, True, 40, "male"]
print(list1)
functions:
def my_function():
print("Hello from a function")
my_function()
def my_function(fname):
print("This function is called by" + fname)
my_function("Emil")
my_function("Tobias")
my_function("Linus"
0 Comments