0% found this document useful (0 votes)
10 views11 pages

Module 1 Cheat Sheet Python

The document provides a comprehensive overview of Python basics, including comments, string manipulation methods, data types, and operators. It also covers string formatting techniques such as f-strings, str.format(), and the % operator, along with the concept of raw strings. Additionally, a glossary of key terms related to Python programming is included for reference.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views11 pages

Module 1 Cheat Sheet Python

The document provides a comprehensive overview of Python basics, including comments, string manipulation methods, data types, and operators. It also covers string formatting techniques such as f-strings, str.format(), and the % operator, along with the concept of raw strings. Additionally, a glossary of key terms related to Python programming is included for reference.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Module 1 Cheat Sheet: Python Basics

Package/Method Description Code Example

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

Syntax:

1. 1

1. concatenated_string = string1 +
string2

Concatenation Combines (concatenates) strings. Copied!Wrap Toggled!

Example:

1. 1

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

Copied!Wrap Toggled!

Data Types - Integer - Float - Boolean - String Example:

1. 1

2. 2

3. 3

4. 4

5. 5

6. 6

7. 7

8. 8

9. 9

10. 10

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!Wrap Toggled!

Example:

1. 1

2. 2
Indexing Accesses character at a specific index.
1. my_string="Hello"

2. char = my_string[0]

Copied!Wrap Toggled!

Syntax:

1. 1

1. len(string_name)

Copied!Wrap Toggled!

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

2. 2

1. my_string="Hello"

2. length = len(my_string)

Copied!Wrap Toggled!

lower() Converts string to lowercase. Example:

1. 1

2. 2

1. my_string="Hello"

2. uppercase_text =
my_string.lower()
Copied!Wrap Toggled!

Example:

1. 1

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

2. print(a+b)

Copied!Wrap Toggled!

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
- Division (/): Divides one value by another,
Python Operators 1. x = 9 y = 4
returns a float.
- Floor Division (//): Divides one value by 2. result_add= x + y # Addition
another, returns the quotient as an integer.
- Modulo (%): Returns the remainder after 3. result_sub= x - y # Subtraction
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!Wrap Toggled!

replace() Replaces substrings. Example:

1. 1

2. 2

1. my_string="Hello"

2. new_text =
my_string.replace("Hello", "Hi")
Copied!Wrap Toggled!

Syntax:

1. 1

1. substring =
string_name[start:end]

Copied!Wrap Toggled!
Slicing Extracts a portion of the string.
Example:

1. 1

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

Copied!Wrap Toggled!

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!Wrap Toggled!

Example:

1. 1

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

2. trimmed = my_string.strip()

Copied!Wrap Toggled!

upper() Converts string to uppercase. Example:

1. 1

2. 2

1. my_string="Hello"

2. uppercase_text =
my_string.upper()
Copied!Wrap Toggled!

Syntax:

1. 1

1. variable_name = value

Copied!Wrap Toggled!

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

1. name="John" # assigning John to


variable name

2. x = 5 # assigning 5 to variable x

Copied!Wrap Toggled!

© IBM Corporation. All rights reserved.

……………………………………………………………………………………………………………………………………………..

Reading: Format Strings in Python

Estimated effort: 5 mins

Format strings are a way to inject variables into a string in Python. They are used to format strings
and produce more human-readable outputs. There are several ways to format strings in Python:

String interpolation (f-strings)

Introduced in Python 3.6, f-strings are a new way to format strings in Python. They are prefixed with
'f' and use curly braces {} to enclose the variables that will be formatted. For example:

1. 1

2. 2

3. 3

1. name = "John"

2. age = 30

3. print(f"My name is {name} and I am {age} years old.")

Copied!Wrap Toggled!
This will output:

1. 1

1. My name is John and I am 30 years old.

Copied!Wrap Toggled!

[Link]()

This is another way to format strings in Python. It uses curly braces {} as placeholders for variables
which are passed as arguments in the format() method. For example:

1. 1

2. 2

3. 3

1. name = "John"

2. age = 50

3. print("My name is {} and I am {} years old.".format(name, age))

Copied!Wrap Toggled!

This will output:

1. 1

1. My name is John and I am 50 years old.

Copied!Wrap Toggled!

% Operator

This is one of the oldest ways to format strings in Python. It uses the % operator to replace variables
in the string. For example:

1. 1

2. 2

3. 3

1. name = "Johnathan"

2. age = 30

3. print("My name is %s and I am %d years old." % (name, age))

Copied!Wrap Toggled!

This will output:

1. 1

1. My name is Johnathan and I am 30 years old.

Copied!Wrap Toggled!
“My name is %s and I am %d years old.“: This is a string that includes format specifiers:

 %s: This is a placeholder for a string.

 %d: This is a placeholder for an integer.

% (name, age): This is a tuple containing the variables name and age. The values of these variables
will replace the placeholders in the string.

Each of these methods has its own advantages and use cases. However, f-strings are generally
considered the most modern and preferred way to format strings in Python due to their readability
and performance.

Additional capabilities

F-strings are also able to evaluate expressions inside the curly braces, which can be very handy. For
example:

1. 1

2. 2

3. 3

1. x = 10

2. y = 20

3. print(f"The sum of x and y is {x+y}.")

Copied!Wrap Toggled!

This will output:

1. 1

1. The sum of x and y is 30.

Copied!Wrap Toggled!

Raw String (r’’)

In Python, raw strings are a powerful tool for handling textual data, especially when dealing with
escape characters. By prefixing a string literal with the letter ‘r’, Python treats the string as raw,
meaning it interprets backslashes as literal characters rather than escape sequences.

Consider the following examples of regular string and raw string:

Regular string:

1. 1

2. 2

1. regular_string = "C:\new_folder\[Link]"

2. print("Regular String:", regular_string)

Copied!Wrap Toggled!
This will output:

1. 1

2. 2

1. Regular String: C:

2. ew_folderile.txt

Copied!Wrap Toggled!

In the regular string regular_string variable, the backslashes (\n) are interpreted as escape sequences.
Therefore, \n represents a newline character, which would lead to an incorrect file path
representation.

Raw string:

1. 1

2. 2

1. raw_string = r"C:\new_folder\[Link]"

2. print("Raw String:", raw_string)

Copied!Wrap Toggled!

This will output:

1. 1

1. Raw String: C:\new_folder\[Link]

Copied!Wrap Toggled!

However, in the raw string raw_string, the backslashes are treated as literal characters. This means
that \n is not interpreted as a newline character, but rather as two separate characters, \ and n.
Consequently, the file path is represented exactly as it appears.

Author: Abhishek Gagneja

…………………………………………………………………………………………………………………………………………………………

Glossary: Python Basics

Welcome! This alphabetized glossary contains many of the terms you'll find within this course. This
comprehensive glossary also includes additional industry-recognized terms not used in course videos.
These terms are important for you to recognize when working in the industry, participating in user
groups, and participating in other certificate programs.
Term Definition

AI (artificial intelligence) is the ability of a digital computer or computer-controlled robot to


AI
perform tasks commonly associated with intelligent beings.

Application development, or app development, is the process of planning, designing,


Application
creating, testing, and deploying a software application to perform various business
development
operations.

Arithmetic operations are the basic calculations we make in everyday life like addition,
Arithmetic
subtraction, multiplication and division. It is also called as algebraic operations or
Operations
mathematical operations.

Set of numbers or objects that follow a pattern presented as an arrangement of rows and
Array of numbers
columns to explain multiplication.

Assignment Assignment operator is a type of Binary operator that helps in modifying the variable to its
operator in Python left with the use of its value to the right. The symbol used for assignment operator is "=".

Asterisk Symbol "* " used to perform various operations in Python.

A backslash is an escape character used in Python strings to indicate that the character
Backslash immediately following it should be treated in a special way, such as being treated as
escaped character or raw string.

Denoting a system of algebraic notation used to represent logical propositions by means of


Boolean
the binary digits 0 (false) and 1 (true).

A colon is used to represent an indented block. It is also used to fetch data and index
Colon
ranges or arrays.

Concatenate Link (things) together in a chain or series.

Data engineers are responsible for turning raw data into information that an organization
Data engineering can understand and use. Their work involves blending, testing, and optimizing data from
numerous sources.

Data Science is an interdisciplinary field that focuses on extracting knowledge from data
Data science sets which are typically huge in amount. The field encompasses analysis, preparing data for
analysis, and presenting findings to inform high-level decisions in an organization.

Data type refers to the type of value a variable has and what type of mathematical,
Data type
relational or logical operations can be applied without causing an error.
Term Definition

Double quote Symbol “ “ used to represent strings in Python.

An escape sequence is two or more characters that often begin with an escape character
Escape sequence
that tell the computer to perform a function or command.

An expression is a combination of operators and operands that is interpreted to produce


Expression
some other value.

Python float () function is used to return a floating-point number from a number or a string
Float
representation of a numeric value.

Forward slash Symbol “/“ used to perform various operations in Python

Foundational Denoting an underlying basis or principle; fundamental.

Immutable Objects are of in-built datatypes like int, float, bool, string, Unicode, and tuple.
Immutable
In simple words, an immutable object can’t be changed after it is created.

An integer is the number zero (0), a positive natural number (1, 2, 3, and so on) or a
Integer
negative integer with a minus sign (−1, −2, −3, and so on.)

Is the process of modifying a string or creating a new string by making changes to existing
Manipulate
strings.

Mathematical A mathematical convention is a fact, name, notation, or usage which is generally agreed
conventions upon by mathematicians.

Mathematical Expressions in math are mathematical statements that have a minimum of two terms
expressions containing numbers or variables, or both, connected by an operator in between.

Mathematical The mathematical “operation” refers to calculating a value using operands and a math
operations operator.

Allows you to access elements of a sequence (such as a list, a string, or a tuple) from the
Negative indexing
end, using negative numbers as indexes.

Operands The quantity on which an operation is to be done.

Operators in
Operators are used to perform operations on variables and values.
Python

Parentheses Parentheses is used to call an object.


Term Definition

Replicate To make an exact copy of.

Sequence A sequence is formally defined as a function whose domain is an interval of integers.

Single quote Symbol ‘ ‘ used to represent strings in python.

Slicing in Python Slicing is used to return a portion from defined list.

A special character is one that is not considered a number or letter. Symbols, accent marks,
Special characters
and punctuation marks are considered special characters.

Stride is the number of bytes from one row of pixels in memory to the next row of pixels in
Stride value
memory.

Strings In Python, Strings are arrays of bytes representing Unicode characters.

Substring A substring is a sequence of characters that are part of an original string.

The process of converting one data type to another data type is called Typecasting or Type
Type casting
Coercion or Type Conversion.

Data types are the classification or categorization of data items. It represents the kind of
Types in Python
value that tells what operations can be performed on a particular data.

Variables Variables are containers for storing data values.

You might also like