Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Python with Artificial Inteligent, Cheat Sheet of Software Engineering

you can lean here python with artificial intaligent.

Typology: Cheat Sheet

2023/2024

Uploaded on 05/15/2025

devarsh-patel-3
devarsh-patel-3 🇮🇳

1 document

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python Fundamentals
Variables and Data Types in Python
What are Variables?
Variables are used to store data that can be used and manipulated in a
program.
A variable is created when you assign a value to it using the = operator.
Example:
Variable Naming Rules
Variable names can contain letters, numbers, and underscores.
Variable names must start with a letter or underscore.
Variable names are case-sensitive.
Avoid using Python keywords as variable names (e.g., print , if , else ).
Best Practices
Use descriptive names that reflect the purpose of the variable.
Use lowercase letters for variable names.
Separate words using underscores for readability (e.g., first_name ,
total_amount ).
name ="Alice"
age =25
height =5.6
pf3
pf4
pf5

Partial preview of the text

Download Python with Artificial Inteligent and more Cheat Sheet Software Engineering in PDF only on Docsity!

Python Fundamentals

Variables and Data Types in Python

What are Variables?

Variables are used to store data that can be used and manipulated in a program.

A variable is created when you assign a value to it using the = operator.

Example:

Variable Naming Rules

Variable names can contain letters, numbers, and underscores. Variable names must start with a letter or underscore. Variable names are case-sensitive. Avoid using Python keywords as variable names (e.g., print , if , else ).

Best Practices

Use descriptive names that reflect the purpose of the variable. Use lowercase letters for variable names. Separate words using underscores for readability (e.g., first_name , total_amount ).

name = "Alice" age = 25 height = 5.

Data Types in Python

Python supports several built-in data types:

Integers ( int ): Whole numbers (e.g., 10 , -5 ). Floats ( float ): Decimal numbers (e.g., 3.14 , -0.001 ). Strings ( str ): Text data enclosed in quotes (e.g., "Hello" , 'Python' ). Booleans ( bool ): Represents True or False. Lists: Ordered, mutable collections (e.g., [1, 2, 3] ). Tuples: Ordered, immutable collections (e.g., (1, 2, 3) ). Sets: Unordered collections of unique elements (e.g., {1, 2, 3} ). Dictionaries: Key-value pairs (e.g., {"name": "Alice", "age": 25} ).

Checking Data Types

Use the type() function to check the data type of a variable.

Typecasting in Python

What is Typecasting?

Typecasting is the process of converting one data type to another. Python provides built-in functions for typecasting: int() : Converts to integer. float() : Converts to float. str() : Converts to string. bool() : Converts to boolean.

print(type(10)) # Output: <class 'int'> print(type("Hello")) # Output: <class 'str'>

Comments, Escape Sequences & Print Statement

Comments

Comments are used to explain code and are ignored by the Python interpreter.

Single-line comments start with #.

Multi-line comments are enclosed in ''' or """.

Escape Sequences

Escape sequences are used to include special characters in strings.

Common escape sequences:

\n : Newline \t : Tab \ : Backslash " : Double quote ' : Single quote

Example:

Print Statement

The print() function is used to display output.

This is a single-line comment

''' This is a multi-line comment '''

print("Hello\nWorld!") print("This is a tab\tcharacter.")

You can use sep and end parameters to customize the output.

Operators in Python

Types of Operators

Arithmetic Operators:

  • (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulus), ** (Exponentiation), // (Floor Division).

Example:

Comparison Operators:

== (Equal), != (Not Equal), > (Greater Than), < (Less Than), >= (Greater Than or Equal), <= (Less Than or Equal).

Example:

Logical Operators:

and , or , not.

Example:

print("Hello", "World", sep=", ", end="!\n")

print(10 + 5) # Output: 15 print(10 ** 2) # Output: 100

print(10 > 5) # Output: True print(10 == 5) # Output: False

print(True and False) # Output: False print(True or False) # Output: True print(not True) # Output: False