¡GoConqr estrena diseño! Cuéntanos qué te parece.

Apuntes

Python

Hajira Hussain Computer Science Year 10 Inglés 1 página

Contenido del apunte (1)

Página 1

Strings in python are surrounded by either single quotation marks or double quotation marks.

'hello' is the same as "hello".

You can display a string literal with the print() function

Assigning a string to a variable is done with the variable name followed by an equal sign and the string: Example --> a = "Hello" --> print(a)

You can assign a multiline string to a variable by using three quotes (meaning that a = a quote that has the quotation marks placed 3 times)

Like many other popular programming languages, strings in Python are arrays of bytes representing Unicode characters.

However, Python does not have a character data type, a single character is simply a string with a length of 1.

Square brackets can be used to access elements of the string.

Since strings are arrays, we can loop through the characters in a string, with a for loop. (for x in "banana": print(x)

To get the length of a string, use the len() function. It returns the length of the string.

a = "Hello, World!"

print(len(a))

To check if a certain phrase or character is present in a string, we can use the keyword in.

txt = "The best things in life are free!"

print("free" in txt)

or can be written as...

txt = "The best things in life are free!"

if "free" in txt:

print("Yes, 'free' is present.")

The opposite of the above is

txt = "The best things in life are free!"

print("expensive" not in txt)

or

txt = "The best things in life are free!"

if "expensive" not in txt:

print("Yes, 'expensive' is NOT present.")

-----------------------------------------------------------------------------------------------------------------------------------------------

These are mostly used in if statements

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

Python relies on indentation (whitespace at the beginning of a line) to define scope in the code.

The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition".

The else keyword catches anything which isn't caught by the preceding conditions.

If you have only one statement to execute, you can put it on the same line as the if statement.

if a > b: print("a is greater than b")

If you have only one statement to execute, one for if, and one for else, you can put it all on the same line

a = 2

b = 330

print("A") if a > b else print("B")

The and keyword is a logical operator and is used to combine conditional statements

The or keyword is a logical operator and is used to combine conditional statements

You can have if statements inside if statements, this is called nested if statements.

if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.

a = 33

b = 200

if b > a:

pass

---------------------------------------------------------------------------------------------------------------------

Operators are used to performing operations on variables and values.

Arithmetic operators, Assignment operators, Comparison operators, Logical operators, Identity operators, Membership operators and Bitwise operators

Arithmetic operators

% --> Modules

** --> Exponentation

// --> Floor division

Comparison operators

== -> Equal

!= -> Not equal

Logical operators

and (Returns True if both statements are true)

or (Returns True if one of the statements is true)

not ( Reverse the result, returns False if the result is true)

Identity operators

is (if two are the same)

is not (if two aren't the same)

Membership operators

in (Returns True if a sequence with the specified value is present in the object)

not in (Returns True if a sequence with the specified value is not present in the object)

Bitwise operators

AND -> &

| OR (Sets each bit to 1 if one of two bits is 1)

^ XOR (Sets each bit to 1 if only one of two bits is 1)

~ NOT (Inverts all the bits)

<< Zero fill left shift (Shift left by pushing zeros in from the right and let the leftmost bits fall off)

>> Signed right shift (Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off)

------------------------------------------------------------------------------------------------------------------------

A for loop is used for iterating over a sequence

fruits = ["apple", "banana", "cherry"]

for x in fruits:

print(x)

Even strings are iterable objects, they contain a sequence of characters

With the break statement we can stop the loop before it has looped through all the items

fruits = ["apple", "banana", "cherry"]

for x in fruits:

print(x)

if x == "banana":

break

With the continue statement we can stop the current iteration of the loop, and continue with the next

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

The range() function defaults to 0 as a starting value, however it is possible to specify the starting value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6)

The range() function defaults to increment the sequence by 1, however it is possible to specify the increment value by adding a third parameter: range(2, 30, 3)

The else keyword in a for loop specifies a block of code to be executed when the loop is finished

for x in range(6):

print(x)

else:

print("Finally finished!")

A nested loop is a loop inside a loop.

The "inner loop" will be executed one time for each iteration of the "outer loop"

for loops cannot be empty, but if you for some reason have a for loop with no content, put in the pass statement to avoid getting an error.

---------------------------------------------------------------------------------------------------------------------------------------

Información

Descripción
First 15% of python notes only
Creado por
Hajira Hussain
Visitas
1
Valoración

Escríbenos

¿Sobre qué es tu mensaje?

¿Qué te parece el nuevo diseño?

¿Te gusta?