0% found this document useful (0 votes)
9 views3 pages

Cheat Sheet - Python Basics

This document is a cheat sheet for Python basics, covering essential concepts such as comments, data types, string manipulation methods, and operators. It provides code examples for each concept, including string concatenation, indexing, and various string functions like lower(), upper(), and split(). The document serves as a quick reference for beginners to understand fundamental Python programming elements.

Uploaded by

viegaz.guitarra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Cheat Sheet - Python Basics

This document is a cheat sheet for Python basics, covering essential concepts such as comments, data types, string manipulation methods, and operators. It provides code examples for each concept, including string concatenation, indexing, and various string functions like lower(), upper(), and split(). The document serves as a quick reference for beginners to understand fundamental Python programming elements.

Uploaded by

viegaz.guitarra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Module 1 Cheat Sheet: Python Basics

Package/Method Description Code Example

# This is a comment

Comments are lines of text that are ignored by the Python interpreter when
Comments
executing the code<./td>

Syntax:
concatenated_string = string1 + string2

Concatenation Combines (concatenates) strings.


Example:
result = "Hello" + " John"</td>

Example:
x=7
# Integer Value
y=12.4
# Float Value
is_valid = True
# Boolean Value
is_valid = False
# Boolean Value
F_Name = "John"
Data Types - Integer - Float - Boolean - String # String Value

Example:
my_string="Hello"
char = my_string[0]

Indexing Accesses character at a specific index.

len() Returns the length of a string. Syntax:


len(string_name)
Example:

my_string="Hello"
length = len(my_string)

Example:
my_string="Hello"
uppercase_text = my_string.lower()

lower() Converts string to lowercase.

Example:
print("Hello, world")
print(a+b)

print() Prints the message or variable inside `()`.

Example:
x = 9 y = 4
result_add= x + y # Addition
result_sub= x - y # Subtraction
result_mul= x * y # Multiplication
- Addition (+): Adds two values together. result_div= x / y # Division
- Subtraction (-): Subtracts one value from another. result_fdiv= x // y # Floor Division
- Multiplication (*): Multiplies two values. result_mod= x % y # Modulo</td>
Python Operators - Division (/): Divides one value by another, returns a float.
- Floor Division (//): Divides one value by another, returns the quotient as
an integer.
- Modulo (%): Returns the remainder after division.

Example:
my_string="Hello"
new_text = my_string.replace("Hello", "Hi")

replace() Replaces substrings.

Slicing Extracts a portion of the string. Syntax:


substring = string_name[start:end]

Example:
my_string="Hello" substring = my_string[0:5]
Example:
my_string="Hello"
split_text = my_string.split(",")

split() Splits string into a list based on a delimiter.

Example:
my_string="Hello"
trimmed = my_string.strip()

strip() Removes leading/trailing whitespace.

Example:
my_string="Hello"
uppercase_text = my_string.upper()

upper() Converts string to uppercase.

Syntax:
variable_name = value

Variable
Assigns a value to a variable.
Assignment Example:
name="John" # assigning John to variable name
x = 5 # assigning 5 to variable x

© IBM Corporation. All rights reserved.

You might also like