Python Cheat Sheet

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Data type

boolean 1 bit
char 2 bytes
int 4 bytes
float 4 bytes
double 8 bytes
Date_time 4 bytes

// Java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
//python
print("Hello World") 

access specifier/modifiers (public,private, protected)

Variables and Data Structures


In other programming languages like C, C++ and Java, you will need to declare the type of variables but in Python you don’t need to do that. Just type in the variable
and when values will be given to it, then it will automatically know whether the value given would be a int, float or char or even a String

# Python program to declare variables


num1 = 3
print(num1)
  
num2 = 4.5
print(num2)
  
num3 ="helloworld"
print(num)

num="hello"
num2="komal"
print(num,num2)
print(num,num2,"from insta")
print("ist str = {0} , iind str= {1} ". format(num,num2))

public class Simple{


public static void main(String[] args){
int a=10;
int b=10;
int c=a+b;
System.out.println(c);
}
}

//c++
#include <iostream>
using namespace std;

int main()
{
int a, b, c;
cout << "Enter two integers: ";
cin >> a >> b;
c = a+ b;
cout << a << " + " << b<< " = " <<c;
return 0;
}

num1 = 1.5
num2 = 6.3
a=10
b=20

# Add two numbers


sum = num1 + num2
c = a+b

# Display the sum


print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
print('\nThe sum of \na and b = ',a,b,c)

Comment
Comments starts with a # (for single line), and Python will ignore them:
Comments starts with a """ ……………… """ (for multiple line), and Python will ignore
them:

#This is a comment create by me


print("Hello, World!")
_____________________
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")

Input and Output


In this section, we will learn how to take input from the user and hence manipulate it or simply
display it. input() function is used to take input from the user.

Example of string input from user in java


import java.util.Scanner; // Import the Scanner class
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");

String userName = myObj.nextLine(); // Read user input


System.out.println("Username is: " + userName); // Output user input
}
}

The following example allows user to read an integer form the System.in.

import java.util.*;
class UserInputDemo
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in); //System.in is a standard input stream
System.out.print("Enter first number- ");
int a= sc.nextInt();
System.out.print("Enter second number- ");
int b= sc.nextInt();
System.out.print("Enter third number- ");
int c= sc.nextInt();
int d=a+b+c;
System.out.println("Total= " +d);
}
}

string firstName;
string fullName;
cout << "Type your first name: ";
cin >> firstName; // get user input from the keyboard
cout << "Your name is: " << firstName;

cout << "Type your full name: ";


cin >> fullName;
cout << "Your name is: " << fullName;
// Type your first name: John
// Your name is: John

#include <iostream>
int main() {
int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
return 0;
}
//python (getting input from user )
#simple string
name = input("Enter your name: ") # get user input from the keyboard
print("hello", name)

# input integer from the user


num1 = int(input("Enter num1: "))  # get int input from the keyboard
num2 = int(input("Enter num2: ")) # get int input from the keyboard
num3 = num1 * num2
print("Product is: ", num3)

# Program to check input  # type in Python


  
num = input ("Enter number :")
print(num)
name1 = input("Enter name : ")
print(name1)

# Printing type of input value


print ("type of number", type(num))
print ("type of name", type(name1))

# Printing type of input value(int,float,no.(string), name(string)


num1 = int(input("Enter num1 any number: "))
num2 = float(input("Enter num2 any float: "))
num3 = input ("Enter number in num3:")
name4 = input("Enter name in num4: ")

print ("type of number", type(num1))


print ("type of name", type(num2))
print ("type of number", type(num3))
print ("type of name", type(name4))

You might also like