Python Keywords🐍🔑
Tags: python, beginners, programming, tutorial
Published: 2022-11-02 01:05:47

* All programming languages reserve certain words to have
a special meaning.
* These words are called **keywords**.
* With keywords, the programmer can issue commands to
the compiler or interpreter.
* They let you tell the computer what to do.
* Without keywords, the computer could not make sense from the seemingly random text in your code file.
* Note that as keywords are reserved words, you
cannot use them as variable names.

### Python Keywords 🗝️
```
False True and or

not break continue class

def if elif else

for while in is

None lambda return
```

### Description And Code Example


##### False & True

- Data values from the data type Boolean
```
False == ( 1 > 2 )
True == ( 2 > 1 )
```

##### and & or & not

- Logical operators
- (x and y) → both x and y must be True
- (x or y)  →  either x or y must be True
- ( not x) → x must be false
```
x, y = True , False

# AND

(x and y) == False 👉 True

# OR

(x or y) == True  👉 True

# NOT

( not y) == True  👉 True 
```

##### break
- Ends loop prematurely
```
while ( True ):
    break # no infinite loop
print( "hello world" )

```
##### continue
- Finishes current loop iteration
```
while ( True ):
    continue
    print( "43" ) # dead code
```
##### if & elif & else
- Conditional program execution
- program starts with “if” branch, tries “elif” branches, and finishes with “else” branch (until one evaluates to True). 
```
x = int(input( "your val: " ))

if x > 3 : 
    print( "Big" )
elif x == 3 :
    print( "Medium" )
else:
    print( "Small" )   
```

##### def 
- A function is a block of code which only runs when it is called.
- You can pass data, known as parameters, into a function.
- A function can return data as a result.
 
```
# Void function

def my_function():
  print("Hello from a function")

my_function()


# Void function with parameters

def hello(first_name,last_name,age):
    print("hello",first_name ,last_name)
    print("your",age,"years old")
    print("nice to meet you")

hello("Bro","Code","21")


# Function with return & parameter & argument 

def my_function(x):
  return 5 * x

print(my_function(3))
```
 
##### class
- Defines a new class a real-world concept (object oriented programming) or [OOP](https://dev.to/mahmoudessam/learn-python-oop-with-lecture-playlist-book-cheat-sheet-29l6).
- Defines a new function or class method.
- For latter, first parameter **self** points to the class object.
- When calling class method, first parameter is implicit.
```
class Car:

    # class attribute
    wheels = 4

    # initializer / instance attributes
    def __init__(self, color, style):
        self.color = color
        self.style = style

    # method 1
    def showDescription(self):
        print("This car is a", self.color, self.style)

    # method 2
    def changeColor(self, color):
        self.color = color

c = Car('Black', 'Sedan')

# call method 1
c.showDescription()
# Prints This car is a Black Sedan

# call method 2 and set color
c.changeColor('White')

c.showDescription()
# Prints This car is a White Sedan
```

##### for
- A **for** loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
```
fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
```

##### while loops
- With the while loop we can execute a set of statements as long as a condition is true.
```
i = 1
while i < 6:
  print(i)
  i += 1
```

##### in
- Checks whether element is
in sequence
```
42 in [ 2 , 39 , 42 ] 👉 True
``` 


##### is
- Checks whether both
elements point to the same
object
```
y = x = 3

x is y          👉 True
[ 3 ] is [ 3 ]  👉 False
```
##### None
- The None keyword is used to define a null value, or no value at all.
- None is not the same as 0, False, or an empty string. None is a data type of its own (NoneType) and only None can be None.
- Empty value constant
```
x = None

if x:
  print("Do you think None is True?")
elif x is False:
  print ("Do you think None is False?")
else:
  print("None is not True, or False, None is just None...")e
```

##### lambda
- Function with no name (anonymous)
```
(lambda x: x + 3)(3)  👉 returns 6
```

##### return
- Terminates function execution and passes the execution flow to the caller.
- An optional value after the return keyword specifies the
result.
```
def incrementor (x) :
     return x + 1
incrementor( 4 ) 👉 returns 5
```

### Connect with Me 😊

#### 🔗 Links

[![linkedin](https://img.shields.io/badge/linkedin-0A66C2?style=for-the-badge&logo=linkedin&logoColor=white)](https://www.linkedin.com/in/mahmoud-el-kariouny-822719149/)

[![twitter](https://img.shields.io/badge/twitter-1DA1F2?style=for-the-badge&logo=twitter&logoColor=white)](https://twitter.com/Mahmoud42275)