Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
76 views

Python Cheat Sheet

Python provides basic syntax like comments, printing, variables, and data types. Functions allow reusable blocks of code and take parameters. Lists can store multiple elements that can be accessed by index and modified using methods. Strings are arrays of characters that can be sliced, formatted, concatenated, and converted between cases. Conditionals and loops control program flow. Random numbers can be generated.

Uploaded by

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

Python Cheat Sheet

Python provides basic syntax like comments, printing, variables, and data types. Functions allow reusable blocks of code and take parameters. Lists can store multiple elements that can be accessed by index and modified using methods. Strings are arrays of characters that can be sliced, formatted, concatenated, and converted between cases. Conditionals and loops control program flow. Random numbers can be generated.

Uploaded by

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

Python Cheat Sheet

Basic Syntax Functions Syntax Lists

Comments: Function syntax: Creating a list:


# This is a comment def <name> (<parameters>): my_list = [1, 2, 3, 4, 5]
# inside the function
Writing to terminal: <function code> Accessing elements of a list:
print() # not inside the function my_list[0] # Returns 1

Variable syntax: A variable is created the moment you ‘def’ is a keyword defining that this is a function. Negative Indexing: Negative indexing means
first assign a value to it. Variables do not need to be beginning from the end.
declared with any particular type and can even change There are no return types for functions. my_list[-1] # Returns 5
type after they have been set. You can add a return statement and return any value.
variableName = value Looping through a list:
Function call: for x in my_list:
Data types: function_name() print(x)
Typ Description Examples
e Example: List slicing:
my_list[1:3] # Returns [2, 3]
int Integer (whole 103, - 12, def add_numbers(number1, number2): my_list[:-1] # Returns [1, 2, 3, 4]
result = number1 + number2
numbers)
return result Check if an item is in a list:
float 32 bit decimals (7 digits) 3.14 if 3 in my_list:
def greeting(): # Code for if condition is true
bool Boolean true/false print("Good morning!”)
Adding an item to a list:
str String "Hello" Working with Strings my_list.append(6) # Adds 6 to the end of my_list

Creating a string: Removing an item in a list:


Get data type: The type() function will return the type my_string = “Hello World” # Removes this element from the list (not using index)
of the given variable. my_list.remove(3)
x = 5 Accessing the characters: by referring to its index # Removes last item or the item at the specified index
print(type(x)) # Prints ‘int’ my_list.pop()
number inside square brackets [].
my_string[4]
Variable assignment example: Sorting a list:
x = 5 # Variable ‘x’ created my_list.sort() # Sorts my_list in ascending order
Splitting: Splits the string into substrings if it finds
x = “hello” # ‘x’ is type string
instances of the separator Reversing a list:
x = True # ‘x’ is type boolean
my_string.split(“ “)
Python Cheat Sheet

my_list.reverse() # Reverses my_list in place


conditionals: String formatting: Writing to console
if condition1: print(my_string) Reference Links
# code if condition1 is true
elif condition2: Concatenation (joining strings): - Python documentation
# code if condition2 is true # Combine two strings - Python Tutorial - W3Schools
else: new_str = str1 + str2 - Python Tutorial - TutorialsPoint
# code if conditions are false
# Returns the string with all the elements in the list joined
while loops: # together with spaces between them.
while condition: “ ”.join(str_list)
# code
Converting to uppercase or lowercase: returns the
for loops: uppercase/lowercase of the string.
for x in list: txt = "Hello World"
# code for each member of list # "HELLO WORLD"
OR print(txt.upper())
for i in range(10): # "hello world"
# code to repeat 10 times print(txt.lower())

Type Casting: String contains string:


str = "This is test"
Converting to an integer: int(string)
if “test” in str:
Converting to a string: str(object) print("Word 'test' was found.")
Converting to a float: float(string)

Generating a random integer number:


Syntax: random.randint(start, stop)
Example: print(random.randint(2,8))
- This example generates a random integer
between 2 and 8 (both numbers included)

You might also like