By Aarush
MYP 2
SUMMARY
PYTHON
PROGGRAMMING
What are strings?
STRINGS AND How does string indexing work
in python?
VARIABLES
Strings are any characters, numbers, symbols and
generally anything inclosed within a single quote, One crucial aspect when completing the
double quotes, or triple quotes , which are used for process of indexing or slicing, you require
multiline code
square brackets, also known as the indexing
and slicing operator. The order goes
What are variables? String indexing, and slicing
print(variable[numeral position]). In the
slicing operator, it requires an extra step,
Variables are small conatainer to store data which is the use of colons. The order for the
inside python, for example an example of a String indexing refers to an element in a string slicing operator goes: print(variable[start:
variable can be my_name = ‘Aarush’. A variable based upon a numerical position, which is stop: step]). The first slot is to add the index
is made by the assignment operator, which is accessible, whereas slicing is accessing a starting point, the second slot is to indicate
‘=’, the equal sign, then you put your desired sequence or part of that string using its the index stopping point, and the last slot is
values. numerical positions. to indicate the increment.
In string indexing as said before each character is assigned a numerical position. There are
two types of indexing, known as positive indexing and negative indexing. Positive indexing
increments the index from zero and from left to right. This means that the index starts
from index number 0 and goes up, for example, if we had a string called python, with no
spaces, p would be the index position number 0, and y would be 1, and so on. Negative
indexing decreases the index from -1 running from right to left. This means that the
negative indexing starts from the end of the string and goes right to left, and decreases
from numerical position -1 to -2 and so on, for example in the same string, python f we were
to find the negative index -1, it would be n, next would be o which would be -2.
String Methods STRINGS 2
In Python there are many different string methods, but I only want to discuss some only:
.upper() - used to change all the characters in a string in uppercase form
.lower() - used to change all the characters in the string in lowercase form
.title() - use this to change the first letter of each word in the string uppercase, just as in real titles
.capitalize() - use to capitalize the first letter of the first word in a string
.strip() - used to remove leading and trailing spaces in a string
.lstrip() - used to remove leading spaces in a string
.rstrip() - used to remove trailing spaces in a string
.count() - used to find the first occurrence of a specific character in a string
.find() - used to find the index position of a specific character in a string
.replace() - used to replace occurrences of a specified substring within a string with another substring
other common string methods are: .isalpha(), .isdigits(), .index(), and .split()
Here are some general built-in function in Python:
Len () - use this to count the number of characters in a given string
type() - use this to find the type of data, whether that is str, flo, or even int, there are many more.
besides len(), and type(), there are many other built in function such as sum(), min(), and max().
CONDITIONAL STATEMENTS
Conditional statements
A conditional statement is a decision-making structure in
programming that allows certain code to run only if a specific
condition is true. It's usually written with if, else if, and else. The if
condition will run if the if condition is true, and the elif condition
will run if previous condition was False but this one is True, and
finally the else condition runs if all conditions above are false.
num = input(“Please enter a number: “) temperature = 25
if num > 0:
print("Positive number") if temperature > 30:
elif num < 0: print("It's really hot outside!")
elif temperature > 20:
print("Negative number")
print("The weather is warm.")
else:
else:
print("Zero")
print("It's a bit chilly outside.")
CONTROLLED STATEMENTS
What are controlled statements
In programming, a controlled statement bases
numbers = [1, 2, 3, 4, 5] choices on predetermined criteria. If-else
for iter in numbers: statements determine whether a given assertion is
true and take appropriate action. Actions are
print(iter)
numbers = [1, 2, 3, 4, 5] repeated a certain number of times using a for loop.
While break ends the loop entirely, continue skips
for iter in numbers:
the current loop step and advances to the next.
if iter == 3: These aid in regulating the program's actions and
continue For and while loops behavior.
elif iter == 4:
break A for loop iterates over a sequence, such as
print(iter) a list, by repeating a block of code a
predetermined number of times. As long as a
condition is true, a while loop keeps running.
With the use of conditions and a fixed range,
both loops aid in automating repeated
operations.
THANK YOU