Вы находитесь на странице: 1из 6

Python can be used on a server to create web applications (server-side)

Single line Comments start with a #


"""This is a multiline doc string"""

There are three numeric types in Python:

 Int
 Float: Float can also be scientific numbers with an "e" to indicate the
power of 10.
 complex: x = 3+5j , y = 5j

CASTING:

y = int(2.8) # y will be 2

x = float(1) # x will be 1.0

z = float("3") # z will be 3.0

z = str(3.0) # z will be '3.0'

STRING:

a = "Hello, World!"

print(a[1]) # returns e

print(b[2:5]) # returns llo

print(a.strip()) # returns "Hello, World!" The strip() method removes any


whitespace from the beginning or the end:

print(len(a)) # returns 13

print(a.lower()) # returns hello, world!

print(a.upper()) # returns HELLO, WORLD!

print(a.replace("H", "J")) # returns Jello, World!


IF ELSE:

a = 200
b = 33
if b > a: m,
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")

if a > b and c > a:


print("Both conditions are True")

if a > b or a > c:
print("At least one of the conditions is True")

There are four collection(ARRAY) data types in the Python programming


language:

 List is a collection which is ordered and changeable. Allows duplicate


members.
 Tuple is a collection which is ordered and unchangeable. Allows duplicate
members.
 Set is a collection which is unordered and unindexed. No duplicate
members.
 Dictionary is a collection which is unordered, changeable and indexed.
No duplicate members.

LIST: A list is a collection which is ordered and changeable. In Python lists


are written with square brackets.
eg: thislist = ["apple", "banana", "cherry"]

ARRAY: Arrays are used to store multiple values in one single variable
thislist = ["Ford", "Volvo", "BMW"]
x = thislist [0] # returns Ford
thislist [0] = "Toyota" # returns ['Toyota', 'Volvo', 'BMW']
thislist.append("Honda") # returns ['Ford', 'Volvo', 'BMW', 'Honda']
thislist.pop(1) # returns ['Ford', 'BMW']
thislist.remove("Volvo") # returns ['Ford', 'BMW']
thislist.insert(1, "Toyota") # returns ['Ford', 'Toyota','BMW']
Tuple:
A tuple is a collection which is ordered and unchangeable (we can’t add or
delete value after once it’s initialize). In Python tuples are written with round
brackets.

thistuple = ("apple", "banana", "cherry")

example:
thistuple[1] = "blackcurrant"
# The values will remain the same:
print(thistuple) # returns ('apple', 'banana', 'cherry')

SETS:
A set is a collection which is unordered and unindexed. In Python sets are
written with curly brackets. You cannot access items in a set by referring to an
index, since sets are unordered the items has no index.

thisset = {"apple", "banana", "cherry"}


Examples:

for x in thisset: # returns cherry banana apple

print("banana" in thisset) # Check if "banana" is present in the set:

# returns TRUE

LOOP:

Python has two primitive loop commands:

 while loops : With the while loop we can execute a set of statements as
long as a condition is true.
 for loops : A for loop is used for iterating over a sequence (that is either
a list, a tuple, a dictionary, a set, or a string).

i = 1
while i < 6:
print(i)
if i == 3:
break # it could be continue or it could neither any of these 2
i += 1

FOR Loop Through a List


NESTED FOR LOOP:

FOR Loop Through a Tuple


thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)

Вам также может понравиться