Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
304 views

1-What Is Asyntax Error?: Basics

This document contains a summary of Python basics including data types, variables, strings, operators, control flow, and some example programs. It discusses syntax errors, variables, using triple quotes for multi-line strings, string indexing and slicing, built-in string methods like title(), find(), replace(), checking if a string contains a substring, arithmetic operators, boolean operators, conditional statements, range(), and four short programs to demonstrate FizzBuzz, speeding points, odd/even printing, and asterisk pyramid printing based on user input.

Uploaded by

abdh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
304 views

1-What Is Asyntax Error?: Basics

This document contains a summary of Python basics including data types, variables, strings, operators, control flow, and some example programs. It discusses syntax errors, variables, using triple quotes for multi-line strings, string indexing and slicing, built-in string methods like title(), find(), replace(), checking if a string contains a substring, arithmetic operators, boolean operators, conditional statements, range(), and four short programs to demonstrate FizzBuzz, speeding points, odd/even printing, and asterisk pyramid printing based on user input.

Uploaded by

abdh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Basics

1- What is asyntax error?


when the Python parser is unable to understand a line of code.

2- What is the result of this expression: "*" *10


Repeated the * ten times (**********)
Primitive type
3- What is a variable ?
Variables are containers for storing data values. As like
X=10
N=”name”

4- When should use tripe quotes to define String?


if the string have quotes “work’s ” or in multi line Comments

5- Assuming name =”John Smith” what does name[1] return?


Return o

6- What about name[-2];


Return t

7- What about name[1:-1];


Return ohn Smit

8- How to get length of name?


len(name)

9- what are the escape sequences in python?

\newline Backslash and newline ignored


\\ Backslash (\)
\' Single quote (')
\" Double quote (")
10- Given name = “john smith” what will name.title return?
Return John Smith (Converts the first character of each word to upper
case)

11- Given name = “john smith” what will name.strip return?


Return john smith (a trimmed version of the string)

12- What will name.find(“Smith”)?


(Searches the string for a specified value and returns the position of
where it was found)

Return 5 if specified value and -1 if not

13- What will be the value of name after we call name.replace(“j”,”k”)?


Return kohn smith (Returns a string where a specified value is replaced
with a specified value)

14- How can I check to see if name contains john?


name.find(name.title(“Johan”))
if return -1 not found other number return the position of word
control Flow
15- What is the difference between 10/3 and 10//3?

/ division with the decimal EX: 9 // 5 is 1.8

// Floor division  the digits after the decimal point are removed. EX: 15 / 7 is 2

16- What is the result of 10**3?


Exponentiation 10*10*10 = 1000

17- Given x = 1 what will be the value of after we run x +=2 ?


Print 3  x = x+2

18- What is result of float(1);


float(1)  1.0 convert from int to float

19- what is the result of bool("false")?

True  Any string is True, except empty strings.

20- What are the falsy values in python?


They do not necessarily have to be part of a larger expression to evaluate to a truth
value because they already have one that has been determined by the rules

 Values that evaluate to False are considered Falsy.


 Values that evaluate to True are considered Truthy.

21- What is the result of 10 =="10"?


False not equal

22- What is the result of "bag">"apple"?


True  because Python compares the first letter of each word. And b is greater than a
23- What is the result of not(True or False)?
True or False  true then not(true) false

24- Under what circumstances does the expression 18 <= age < 65 ?
If age = 18 until age less than 65 and not equal 65

25- What does range (1,10,2) return?


[1, 3, 5, 7, 9]
Parameter Description
start An integer number specifying at which position to start. Default is 0
stop An integer number specifying at which position to stop (not included).
step An integer number specifying the incrementation. Default is 1
PROGRAM 1
p
theNumber = input("Enter Number:")
if theNumber % 3 == 0 and theNumber % 5 != 0:
print "Fizz"
elif theNumber % 5 == 0 and theNumber % 3 != 0:
print "Buzz"
elif theNumber % 3 == 0 and theNumber % 5 == 0 :
print "FizzBuzz"
else:
print theNumber

PROGRAM 2

txt = "{} Point"


SpeedNumber = input("Enter SPeed:")
diffSpeed = SpeedNumber - 70
pointSpeed = diffSpeed / 5
if diffSpeed <=0 :
print "OK"
elif pointSpeed <= 12 :
print txt.format(pointSpeed)
else:
print "License Suspended " + txt.format(pointSpeed)
PROGRAM 3

limitNumber = input("Enter Limit:")


txtODD = "{} ODD"
txtEVEN = "{} EVEN"
for x in range(0,limitNumber+1,1):
if x%2 == 0 :
print txtEVEN.format(x)
elif x%2 != 0 :
print txtODD.format(x)
PROGRAM 4

Rows = input("Enter Rows:")


if Rows == 5 :
for x in range(1, Rows + 1, 1):
print "*"*x

You might also like