100% found this document useful (1 vote)
126 views2 pages

Python Cheat Sheet

Python has basic functions like print() for output and input() for user input. It supports common data types like integers, floats, strings, booleans, lists, tuples, and dictionaries. Conditional statements and loops like if/else, while, and for are used to control program flow. Functions can be defined to reuse code, take parameters, and return values. Typecasting converts between data types.

Uploaded by

Harsh Vartak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
100% found this document useful (1 vote)
126 views2 pages

Python Cheat Sheet

Python has basic functions like print() for output and input() for user input. It supports common data types like integers, floats, strings, booleans, lists, tuples, and dictionaries. Conditional statements and loops like if/else, while, and for are used to control program flow. Functions can be defined to reuse code, take parameters, and return values. Typecasting converts between data types.

Uploaded by

Harsh Vartak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 2

Python Cheat Sheet

Basic Functions in Python: Typecasting is a method to convert one


Print function: python data type to another
Print function is used to display any and ● int(): Converts any data type to
everything that's put inside it. integer(for string only if whole string
Syntax: consists of integers)
print(“Hello World”) ● float(): Converts any data type to
This line of python code will print out Hello float data type
World on the screen ● str(): Converts any data type to
Input Function: string
It is used to take data from user and assign
it to a variable automatically Lists in python
Syntax: A list stores a series of items in a particular
var=input() order.
var is a variable that will have the value Let the name of list be name.
input by the user. Anything inside the Defining a list:
bracket will be printed on the screen before name=[ ]
taking input. name=[‘john’,’harry’,’doe’,’chris’,’joe’,’peter’]
Data Types in Python List element access:
1. Integer name[ 2 ], where 2 is the index of element;
Integer data type, will store integer The element at index 2 is doe
only Indexing in list starts at 0
2. Float Adding element in a list:
Floating data type will store non name.append(‘String’)
integer numbers Deleting element from List:
3. String del(name[ ‘harry’ ]) this deletes harry from
It will store collection of symbols the list.
unlike integer, and each character
will have an index Dictionary in python
4. Boolean A dictionary in python stores value with a
It can have only 2 values either true specific key. For eg name: Jake, is a way
or false. Conditional statements dictionary stores values.
return boolean values For eg. let dictionary be alien
Defining a dictionary:
Conditional statements: alien={ ‘name’:’swrf’,’color’:’green’ }
a==b, gives true when a and b are equal Dictionary element access:
a<b, gives true when b is greater than a print(alien[‘color’] ),where color is key, the
a>b, gives true when a is greater than b element printed is green
a<=b, gives true when b is greater than or Adding element in dictionary:
equal to b alien[‘speed’]=25
a>=b, gives true when a is greater than or It will add another value with key in the
equal to a dictionary.
Deleting a key-value pairL:
Typecasting: del alien[‘color’] The value with color along
with key color will be deleted.
In python, one can make functions that
Tuple in python: perform a task or handle a unique problem.
A tuple is like a list, except you can't change Such a function in python can be used
the values in a tuple once it's again and again .
defined.Tuples are good for storing Defining a function
information that shouldn't be changed def keyword is used to define a function in
throughout the life of python
a program. Syntax:
def functionName():
{ Your code here }
If-else condition: For eg.
If statements are used to test for particular def my_func():
conditions and respond appropriately. print(“Hello”)
Syntax of if else: If the function my_func is called it will print
if conditional statement: Hello
{ your code here } Calling of Functions:
elif conditional statement: To call a function, we need to type its name
{ your code here } with parentheses
else : For eg.
{ your code here } my_func()
This will call the function my_func that
While loops: executes the code inside the function
While loops run as long as certain Parameters in Function:
conditions remain true. You can use while Information can be supplied to a function to
loops to let your programs run as long as complete a specific task
your users want them to. For eg.
Syntax of while loop: Def my_func(fname):
while conditional statement: print(fname)
{ your code here } When called like my_func(Joe), it will print
Joe on screen.
For Loops: Return values:
For loops in python are used to go over a To let a function return value back to main
sequence (that is either a list, dictionary set, program, we use return statement
string,range etc.) For eg.
For loops for specific range: Def my_func(x):
for i in range( range ): Return 5*x
{ Your code here } print(my_func(3))
In this, range inside the range function This function will take parameter x as 3 and
should be a positive integer. rertun 3*5 which is 15
For loops for list etc.:
for i in (Data type here(can be list)):
{ Your code here }

User-defined Functions in Python:

You might also like