[Python!] Basics

Hailey Park·2021년 11월 3일
0

Python!

목록 보기
2/11
post-thumbnail

Python Indentation

Indentation refers to the spaces at the beginning of a code line.

Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.

Python uses indentation to indicate a block of code as below.

if 5 > 2:
  print("Five is greater than two!")

Variable Names

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables:

  • A variable name must start with a letter or the underscore character

  • A variable name cannot start with a number

  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

  • Variable names are case-sensitive (age, Age and AGE are three different variables)

  • Multi words variable names :
    Camel case (myVariableName = "John"),
    Pascal case (MyVariableName = "John"),
    Snake case (my_variable_name = "John")

Data Type

Python has the following data types built-in by default, in these categories:

  • Text Type: str
  • Numeric Types: int, float, complex
  • Sequence Types: list, tuple, range
  • Mapping Type: dict
  • Set Types: set, frozenset
  • Boolean Type: bool
  • Binary Types: bytes, bytearray, memoryview

Python Arithmetic Operators

+	Addition	x + y	
-	Subtraction	x - y	
*	Multiplication	x * y	
/	Division	x / y	
%	Modulus	x % y	
**	Exponentiation	x ** y	
//	Floor division	x // y

Python Assignment Operators

=	x = 5	x = 5	
+=	x += 3	x = x + 3	
-=	x -= 3	x = x - 3	
*=	x *= 3	x = x * 3	
/=	x /= 3	x = x / 3	
%=	x %= 3	x = x % 3	
//=	x //= 3	x = x // 3	
**=	x **= 3	x = x ** 3	
&=	x &= 3	x = x & 3	
|=	x |= 3	x = x | 3	
^=	x ^= 3	x = x ^ 3	
>>=	x >>= 3	x = x >> 3	
<<=	x <<= 3	x = x << 3

Python Comparison Operators

==	Equal	x == y	
!=	Not equal	x != y	
>	Greater than	x > y	
<	Less than	x < y	
>=	Greater than or equal to	x >= y	
<=	Less than or equal to	x <= y

Python Logical Operators

and 	Returns True if both statements are true	x < 5 and  x < 10	
or	Returns True if one of the statements is true	x < 5 or x < 4	
not	Reverse the result, returns False if the result is true	not(x < 5 and x < 10)

Python Identity Operators

is 	Returns True if both variables are the same object	x is y	
is not	Returns True if both variables are not the same object	x is not y

Python Membership Operators

in 	Returns True if a sequence with the specified value is present in the object	x in y	
not in	Returns True if a sequence with the specified value is not present in the object	x not in y

Python Bitwise Operators

& 	AND	Sets each bit to 1 if both bits are 1
|	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

Order of Arithmetic Operators

  1. ( )
  2. **
  3. *, / , 그리고 %
  4. +-

Concatenating Text Strings

String concatenation means add strings together.
Use the + character to add a variable to another variable:

x = "Python is "
y = "awesome"
z =  x + y
print(z)

Literal String Interpolation

1. %-Formatting

2. Str.format()

3. f-strings

n1 = 'Hello'
n2 = 'GeeksforGeeks'
 
print(f"{n1}! This is {n2}")

4. String Template Class

Resources

https://www.w3schools.com/python/default.asp

profile
I'm a deeply superficial person.

0개의 댓글