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

Pythoncheatsheet

The document provides descriptions and code examples for common Python basics including comments, strings, data types, operators, and string methods. It covers topics such as concatenation, indexing, length, case changing, printing, and replacing substrings.

Uploaded by

itsmeil012024
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
31 views3 pages

Pythoncheatsheet

The document provides descriptions and code examples for common Python basics including comments, strings, data types, operators, and string methods. It covers topics such as concatenation, indexing, length, case changing, printing, and replacing substrings.

Uploaded by

itsmeil012024
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

11/12/23, 8:32 AM

Module 1 Cheatsheet: Python Basics


Package/Method Description Code Example
1. 1

Comments are lines of text that are ignored by the Python interpreter when 1. # This is a comment
Comments
executing the code<./td>
Copied!

Syntax:
1. 1

1. concatenated_string = string1 + string2

Copied!

Concatenation Combines (concatenates) strings.


Example:

1. 1

1. result = "Hello" + " John"</td>

Copied!

Example:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
Data Types - Integer - Float - Boolean - String 1. x=7
2. # Integer Value
3. y=12.4
4. # Float Value
5. is_valid = True
6. # Boolean Value
7. is_valid = False
8. # Boolean Value
9. F_Name = "John"
10. # String Value

Copied!

Example:
1. 1
2. 2
Indexing Accesses character at a specific index. 1. my_string="Hello"
2. char = my_string[0]

Copied!

Syntax:
1. 1

1. len(string_name)

Copied!

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

1. 1
2. 2

1. my_string="Hello"
2. length = len(my_string)

Copied!

Example:
1. 1
2. 2
lower() Converts string to lowercase. 1. my_string="Hello"
2. uppercase_text = my_string.lower()

Copied!

Example:
1. 1
2. 2

https://author-ide.skills.network/render?token=eyJhbGciOiJIUzI…iOjE3MDEwODk3NzB9.h-MgIUtPWg6zgydWci8VMfs8NPsBYLcE2Artbs7Ou5s Page 1 of 3
11/12/23, 8:32 AM

print() Prints the message or variable inside `()`. 1. print("Hello, world")


2. print(a+b)

Copied!

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

Copied!

Example:
1. 1
2. 2
replace() Replaces substrings. 1. my_string="Hello"
2. new_text = my_string.replace("Hello", "Hi")

Copied!

Syntax:
1. 1

1. substring = string_name[start:end]

Copied!

Slicing Extracts a portion of the string.


Example:
1. 1

1. my_string="Hello" substring = my_string[0:5]

Copied!

Example:
1. 1
2. 2
split() Splits string into a list based on a delimiter. 1. my_string="Hello"
2. split_text = my_string.split(",")

Copied!

Example:
1. 1
2. 2
strip() Removes leading/trailing whitespace. 1. my_string="Hello"
2. trimmed = my_string.strip()

Copied!

Example:
1. 1
2. 2
upper() Converts string to uppercase. 1. my_string="Hello"
2. uppercase_text = my_string.upper()

Copied!

Syntax:
1. 1

1. variable_name = value

Copied!

Variable Example:
Assigns a value to a variable.
Assignment
1. 1
2. 2

1. name="John" # assigning John to variable name

https://author-ide.skills.network/render?token=eyJhbGciOiJIUzI…iOjE3MDEwODk3NzB9.h-MgIUtPWg6zgydWci8VMfs8NPsBYLcE2Artbs7Ou5s Page 2 of 3
11/12/23, 8:32 AM

2. x = 5 # assigning 5 to variable x

Copied!

© IBM Corporation. All rights reserved.

https://author-ide.skills.network/render?token=eyJhbGciOiJIUzI…iOjE3MDEwODk3NzB9.h-MgIUtPWg6zgydWci8VMfs8NPsBYLcE2Artbs7Ou5s Page 3 of 3

You might also like