0% found this document useful (0 votes)
124 views

CheatSheet Python 1 Keywords1

The document provides a cheat sheet of key Python keywords, their descriptions, and code examples. It covers basic keywords like True, False, and, or, not for logical operations and conditionals. It also covers keywords that control program flow like break, continue, if/elif/else, for, while. Additional keywords covered include class for object-oriented programming, def for defining functions, in for membership checks, is for object identity, None as an empty value, lambda for anonymous functions, and return to terminate a function.

Uploaded by

Dario R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views

CheatSheet Python 1 Keywords1

The document provides a cheat sheet of key Python keywords, their descriptions, and code examples. It covers basic keywords like True, False, and, or, not for logical operations and conditionals. It also covers keywords that control program flow like break, continue, if/elif/else, for, while. Additional keywords covered include class for object-oriented programming, def for defining functions, in for membership checks, is for object identity, None as an empty value, lambda for anonymous functions, and return to terminate a function.

Uploaded by

Dario R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Python Cheat Sheet: Keywords 

“​A puzzle a day to learn, code, and play​” → Visit ​finxter.com 

Keyword  Description  Code example 

False​, ​True  Data values from the data type Boolean  False​ == (​1 ​> ​2​), ​True​ == (​2 ​> ​1​)

and​, ​or​, ​not  Logical operators:  x, y = ​True​, ​False


(x ​and​ y)​ → both x and y must be True  (x ​or​ y) == ​True​ ​# True
(x ​or​ y)​ → either x or y must be True  (x ​and​ y) == ​False​ ​ True
#
(​not​ x)​ → x must be false  (​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

class Defines a new class → a real-world concept   class​ ​Beer​:


(object oriented programming)  ​def​ ​__init__​(self)​:
  self.content = ​1.0
def  Defines a new function or class method. For latter,  ​def​ ​drink​(self)​:
first parameter (“self”) points to the class object.  self.content = ​0.0
When calling class method, first parameter is implicit. 
becks = Beer() ​# constructor - create class
becks.drink() ​# beer empty: b.content == 0

if​, ​elif​, ​else  Conditional program execution: program starts with  x = int(input(​"your value: "​))
“if” branch, tries the “elif” branches, and finishes with  if​ x > ​3​: print(​"Big"​)
“else” branch (until one branch evaluates to True).  elif​ x == ​3​: print(​"Medium"​)
else​: print(​"Small"​)

for​, ​while  # For loop declaration # While loop - same semantics


for​ i ​in​ [​0​,​1​,​2​]: j = ​0
print(i)  while​ j < ​3​:
print(j)
j = j + ​1

in  Checks whether element is in sequence  42​ ​in​ [​2​, ​39​, ​42​] ​# True

is  Checks whether both elements point to the same  y = x = 3


object  x​ ​is​ ​y​ ​# True
[​3​] ​is​ [​3​] ​# False

None  Empty value constant  ​ ​()​:


def​ f
x = ​2
f() ​is​ ​None​ ​# True

lambda  Function with no name (anonymous function)  (lambda​ x: x + ​3)(3)​ ​# returns 6

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

You might also like